Search code examples
javascriptarraysdynamics-crm

How to calculate array values in XRM?


I have a problem in xrm webapi, my scenario is i need to get all those quotes whose status is won and get their total amount as well, suppose quote a total amount is 1 and quote b has total amount 2 and quote c has total amount 3, it should add totalsum as = 6 but i'm getting error 6[object object]. My code is as follow

    function onSaveOpportunity(executionContext)

{
    debugger;
    var formContext = executionContext.getFormContext();
    var opporId = formContext.data.entity.getId();
    var attrWonQuotes = formContext.getAttribute("dstmda_wonquotestotalamount").getValue();
    Xrm.WebApi.online.retrieveMultipleRecords("quote","?$select=totalamount&$filter=(statuscode eq 4 and opportunityid/opportunityid eq "+ opporId +")").then(
    function success(result) 
    {
        debugger;
        var totalAmount;
        for (var i = 0; i < result.entities.length; i++) 
        {   
            var totalAmount = 0;
            console.log(result.entities[i]);
            var results = result.entities[i];
            totalAmount = results["totalamount"];
            totalAmount=totalAmount+results;
        }
            attrWonQuotes.setValue(totalAmount);
            alert("pass");
                            
        // perform additional operations on retrieved records
    },
    function (error) 
    {
        console.log(error.message);
        // handle error conditions
    });

} 

Solution

  • your problem lies here

    var attrWonQuotes = formContext.getAttribute("dstmda_wonquotestotalamount").getValue();
    

    in variable attrWonQuotes you are not getting attribute object rather it's value.

     attrWonQuotes.setValue(totalAmount);
    

    This is not correct rather it should be

    formContext.getAttribute("dstmda_wonquotestotalamount").setValue(totalAmount);