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)

Create workflow to convert Option Sets data to number

(0) ShareShare
ReportReport
Posted on by

Dear Dynamics 365 community,

I am looking for a way to create a workflow to:

1. Convert options sets values (1,2,3,4 or 5) to numbers

2. Addition all the value from the option sets (there is 6 fields with all the same 1-5 option set)

With the final result, we want to have an indicator of the ''value'' for a prospect. Like if we set each 6 fields to 1, the value would be 6 wich is super low and we would not put efforts with this prospect. 

I heard I would need to convert the data from the option sets fields to some dummy fields (hidden on the form) and then I would be able to addition these values. The problem is: I don't have any idea on how to do this and what would the workflow look like. 

I'm turning to you guys to help me creating the workflow wich would convert the option sets to number and then addition all the dummy field to get the value of the prospects.

Hope you can help me with this,

Derek

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Derek,

    I am not sure if this can be achieved easily by workflows, you might need to write custom workflow activity.

    However this can be done by JavaScript fairly easily..

    you need to create a method which will trigger

    onchange of all the optionset field and within the method-

    1. Get values from all the optionset fields and put it into different variables (for each optionset)

    2. Calculate the some of all the variables

    3. Set the value to your total field.

  • Community Member Profile Picture
    on at

    Hi Ravi,

    Thanks for your reply ! Jscript would surely work as other script you gave me worked pretty well. 

    I would have something like this, maybe you could help me to complete?

    function OptionSetsSum() {
    var currentValue = Xrm.Page.getAttribute("Field1").getValue();
    if(currentValue != null)

    Xrm.Page.getAttribute("DummyField1").setValue(0);
    {
    Xrm.Page.getAttribute("DummyField1").setValue(value);
    }
    }

    {

    var Total = DummyField1 + DummyField2 + DummyField 3...

    Xrm.Page.getAttribute(‘OptionSetsFieldsSum’).setValue(Total)

    }

    I would write this code for each option sets fields I have (6 or 7). Or maybe there is another way to make it shorter?
    DummyField would be numeric data field that would be hidden from the form. 

    For the sum, I'm really not sure, do you have any idea other than this if it isn't correct?

    Thanks for your help,

    Derek

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Derek,

    I a bit late to respond you on this and you probably have already written the scripts. But in case you still need some help, you can refer the below sample for 2 option set field. You can extend this in the same way for more optionset fields.

    You need to ensure that you set the default value to '0' so that the fields always have some value. Also, as you see below, we are using the getValue, this will return the actual optionset value (and not the text) so ypu need to set the optionset values as 1,2,3... within the field customization properties.

    ---------------

    function CalculateTotal()

    {

    var value1 = Xrm.Page.getAttribute("new_primaryscore").getValue();

    var value2 = Xrm.Page.getAttribute("new_secondryscore").getValue();

    var total = value1 + value2;

    Xrm.Page.getAttribute("new_totalscore").setValue(total);

    }

    ---------------------

  • Community Member Profile Picture
    on at

    Hi Ravi,

    In fact, I had another idea wich was to use Business rules wich would turn the optionsets value to numbers and then create a ''Total'' field wich would calculate the values in the dummy field.

    I tried your code with my fields and OnLoad script wich looks like this:

    function TotalCoteClient()
    {
    var value1 = Xrm.Page.getAttribute("new_coteclient").getValue();
    var value2 = Xrm.Page.getAttribute("new_coteclient2").getValue();
    var value3 = Xrm.Page.getAttribute("new_coteclient3").getValue();
    var value4 = Xrm.Page.getAttribute("new_coteclient4").getValue();
    var value5 = Xrm.Page.getAttribute("new_coteclient5").getValue();
    var total = value1 + value2 + value3 + value4 + value5;
    Xrm.Page.getAttribute("new_total2").setValue(total);
    }

    However, I'm stuck with this error:
    Erreur_5F00_Script_5F00_Calculatedfields.PNG

    Hope you can help me with this as your option is way faster and easier if this works.

    Best regards,

    Derek

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Derek,

    This error occurs if the script is unable to find the attribute on the form or the field name is correct.

    Can you check if the field names are correct where we have used getValue()?

    Also, can you check if all the fields are added in the form?

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi Derek,

    You can also you a generic function to get the values of the optionsets:

    function TotalCoteClient()

    {

    var value1 = Xrm.Page.getAttribute("new_coteclient").getValue();

    var value2 = Xrm.Page.getAttribute("new_coteclient2").getValue();

    var value3 = Xrm.Page.getAttribute("new_coteclient3").getValue();

    var value4 = Xrm.Page.getAttribute("new_coteclient4").getValue();

    var value5 = Xrm.Page.getAttribute("new_coteclient5").getValue();

    var total = getOptionSetValue("new_coteclient") + getOptionSetValue("new_coteclient2") +

    getOptionSetValue("new_coteclient3")+ getOptionSetValue("new_coteclient4") + getOptionSetValue("new_coteclient5");

    Xrm.Page.getAttribute("new_total2").setValue(total);

    }

    function getOptionSetValue(fieldName) {

       var field = Xrm.Page.getAttribute(fieldName);

       if (field != null) {

           var fieldOption = field.getValue();

           if (fieldOption != null)

               return fieldOption;

           else

               return 0;

       }

    }

    Hope this can resolve your issue. Verify fields names just like Ravi suggested.

  • Community Member Profile Picture
    on at

    Hi Ravi and Aric, 

    Thanks a lot for both your responses, it helps me a lot ! However after double checking everything, I still have a problem, the script doesn't work. 

    For Aric script, it shows the following error:
    Erreur_5F00_InvalidType.PNG

    Maybe the problem is my ''Total'' field type? I currently have ''text'' ''simple'' has my total field type. I also tried with ''whole number'' field type and it didn't work.

    Regards,

    Derek

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Derek,

    It does look like field type issue.

    Can you try to refresh the browser/ clear cache?

    Using the initial script, You can also try to break down the code in junks and then try to identify the broken statement.

  • Community Member Profile Picture
    on at

    Hi Ravi and Aric,

    For Aric code, after I clear cache, here is the error I get:

    Erreur_5F00_Script_5F00_CalculatedFields2.PNG
    The code is the following:

    function TotalCoteClient()
    {
    var value1 = Xrm.Page.getAttribute("new_coteclient1").getValue();
    var value2 = Xrm.Page.getAttribute("new_coteclient2").getValue();
    var value3 = Xrm.Page.getAttribute("new_coteclient3").getValue();
    var value4 = Xrm.Page.getAttribute("new_coteclient4").getValue();
    var value5 = Xrm.Page.getAttribute("new_coteclient5").getValue();
    var total = getOptionSetValue("new_coteclient1") + getOptionSetValue("new_coteclient2") +
    getOptionSetValue("new_coteclient3")+ getOptionSetValue("new_coteclient4") + getOptionSetValue("new_coteclient5");
    Xrm.Page.getAttribute("new_total3").setValue(total);
    }
    function TotalCoteClient(fieldName) {
    var field = Xrm.Page.getAttribute(fieldName);
    if (field != null) {
    var fieldOption = field.getValue();
    if (fieldOption != null)
    return fieldOption;
    else
    return 0;
    }

    While with your script, Ravi, still the same problem, Invalid Type. If ever you have an answer for this, it would be awesome. As far as I searched for an answer on this, I couldn't find anything on this particular error. 

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi Derek,

    Syntax is wrong in your function.

    Copy like this

    function TotalCoteClient()

    {

    var value1 = Xrm.Page.getAttribute("new_coteclient1").getValue();

    var value2 = Xrm.Page.getAttribute("new_coteclient2").getValue();

    var value3 = Xrm.Page.getAttribute("new_coteclient3").getValue();

    var value4 = Xrm.Page.getAttribute("new_coteclient4").getValue();

    var value5 = Xrm.Page.getAttribute("new_coteclient5").getValue();

    var total = getOptionSetValue("new_coteclient1") + getOptionSetValue("new_coteclient2") +

    getOptionSetValue("new_coteclient3")+ getOptionSetValue("new_coteclient4") + getOptionSetValue("new_coteclient5");

    Xrm.Page.getAttribute("new_total3").setValue(total);

    }

    function TotalCoteClient(fieldName) {

    var field = Xrm.Page.getAttribute(fieldName);

    if (field != null) {

    var fieldOption = field.getValue();

    if (fieldOption != null)

    return fieldOption;

    else

    return 0;

    }

    }

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