web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript to add value of different fields based on a condition

(0) ShareShare
ReportReport
Posted on by 160

Hello All,

As you can see below screenshot, I am having a custom entity with Status fields (5 of them) and CPDU fields (5 of them) next to it with different values. I want to update the other CPDU field (marked in red) which will be sum of all CPDU fields below, but only if the Status field next to has value "Attended". Can someone help with the Javascript?

5826.CRM.PNG

Thanks,

Praveen.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Praveen,

    I dont think you need a javascript for this - you can do this using a business rule. ( try and let me know if this works)

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Addition to this : Please also explore the possibility of Calculate field.

  • Praveen Kumar Ellappa Profile Picture
    160 on at

    Hi Rawish,

    Thanks for your response.

    I couldn't use the Calculate field cos, there are multiple conditions which are based on Status field.

    I am not sure how we can calculate value using business rules with multiple conditions.

    Regards,

    Praveen.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello Praveen,

    What is the field type of CPDU? is it single line text ?

    You can set the value using javascript , lets say you have following status reason Declined (10000) ,Accepted (10001),Attended(10004). So you can register function onchange event of every field like below .

    function OnChangeOfStatus1(FieldName)  // Register onchange event of Status field 1
    {
        OnChangeOfStatus("new_Status1");
    
    }
    
    function OnChangeOfStatus1(FieldName)// Register onchange event of Status field 2
    {
        OnChangeOfStatus("new_Status2");
    
    }
    
    function OnChangeOfStatus1(FieldName)// Register onchange event of Status field 3
    {
        OnChangeOfStatus("new_Status3");
    
    }
    
    function OnChangeOfStatus(FieldName)
    {
        
        var calculatedStatus = "";
    
        if(Xrm.Page.getAttribute(FieldName).getValue() == 10000  )
        {
            calculatedStatus = "Declined";
         }
        else if(Xrm.Page.getAttribute(FieldName).getValue() == 10001  )
        {
            calculatedStatus = calculatedStatus +"," + "Accepted"; // Change as per your business logic
        }
        else if(Xrm.Page.getAttribute(FieldName).getValue() == 10002  )
        {
            calculatedStatus = calculatedStatus + "," + "Attended"; // Change as per your business logic
        }
    
        Xrm.Page.getControl("header_CPDU").getAttribute().setValue("calculatedStatus"); //Make sure you added "header" if the field resides in header
     
    }
    


    Hope this helps.

  • Praveen Kumar Ellappa Profile Picture
    160 on at

    Thanks Goutam. CPDU field is a Whole number field.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Ok then you can calculate as per your requirement. Have a look my code just you need to calculate instead of concatenate.

  • Praveen Kumar Ellappa Profile Picture
    160 on at

    Thanks, Goutam. Let me try.

    Regards,

    Praveen.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans