Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Contact Option Set field upgrade trigger phone call activity

Posted on by 102

Hi Experts,


The scenario is like this:


I have an Option Set field in the Contact entity, named "Rank", values are rank1, rank2,rank3, rank4, and rank5 from the lowest level to the highest level.
Once the Rank UPGRADE (that means only going up, not go down), trigger a Phone Call Activity.


Phone Call Activity's Subject must involve upgraded Rank value (e.g. If one Contact's level is Rink3 before upgrading, the Subject must be: Your Rank have upgraded to Rank4).


Can you provide your suggestion of the best approach?
Any response should be welcome and appreciated.

  • PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Hi, CRMKEY.

    If another systems is triggering this implementation, then the Javascript provided won't work. remember that all Javascript implementation it's just for client side interaction means(An user must make a change in the field and the form you configured). So a possible work around for this task should be a custom Plug-in listening for the Rank field. If you have a specific doubt in working with plugins, be free on create new threads.

    Regards.

  • DATAGREE Profile Picture
    DATAGREE 102 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    One more question:

    The Rank upgrade triggered by other system automatically, so the Event Handler I need to select Control: Rank, Event: OnChange, right?  Thanks.

  • DATAGREE Profile Picture
    DATAGREE 102 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Thanks Pablo and Leah. You give me excellent suggestions and approaches.

    Now there are some new questions, can you pls give me a hand?

    1.We have the global option set Rank and local option set Rank. For history and integration reason, the option terms sequence doesn't consistent with the order of their values, that say Rank5=8, Rank4=1, Rank3=2, Rank2=3, Rank1=2, Base=1, Start=7. Now, how do compare their rank level? Need to use Array?

    2.In the Phone Call record, how to populate the Start Date Field?

    3.I have made a mistake, the custom field tts_role is an option set field, I need to set its value as RoleType, how should I do?

    Appreciate you time and help. 

  • Verified answer
    PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Hi, CRMKEY,

    Addressing your first doubt I'd validate the scenarios where I don't want it to trigger the Activity, let's say (start and base).

    then write this before the if statement.

    var exceptions = ["start","base"];
    if(exceptions.indexOf(Rank.toLowerCase())!== -1) //Means you have an exception
        return;
    //Create phone call when upgrade
    if (Rank > OldRank)
    //...

    Now for your next doubt
    1. Assuming you are setting From as system user and To as contact .

    2. Date time scheduledend  should change

    3. RoleType also should change

    full code below whit some annotations

    function compareValue(executionContext) {
      var formContext = executionContext.getFormContext();
      var contactID = formContext.data.entity.getId().replace('{', '').replace('}','');
      var ownerID = formContext.data.entity.getId().replace('{', '').replace('}','');
      var Rank = formContext.getAttribute("new_rank").getValue();//new rank value.
      var RankText = form.getAttribute("new_rank").getText();//new rank text.
      var OldRank = form.getAttribute("new_oldrank").getValue();//old rank value.
    
        //Validate some exceptions where should'n trigger
        var exceptions = ["start","base"];
        if(exceptions.indexOf(Rank.toLowerCase())!== -1) //Means you have an exception
            return;
        //Create phone call when upgrade
        if (Rank > OldRank) {
        var entity = {};
        entity.subject = "Your Rank have upgraded to " RankText;//set phone call subject with new rank text
        entity["regardingobjectid_contact@odata.bind"] = "/contacts(" contactID ")";//set phone call regardng field with current contact
        
        //FROM and TO fields are ActivityParty fields so...
        var parties = [];
        var sender = {};
        sender["partyid_systemuser@odata.bind"] = "/systemusers("   ownerID   ")";
        sender["participationtypemask"] = 1; //From
        var receiver1 = {};
        receiver1["partyid_contact@odata.bind"] =  "/contacts("   contactID   ")"; 
        receiver1["participationtypemask"] = 2; //To
        parties.push(sender,receiver1);
        entity["phonecall_activity_parties"]=parties;
        
        //Set Due Date is 7 calendar days from current date
        var dueDay = formContext.data.entity.getAttribute("scheduledend");//<-- KEEP AN EYE ON THIS, YOU WANT THE ATTRIBUTE scheduledend(SEEMS LIKE OOB FIELD BUT IN CONTACT DOES NOT EXIST) BUT REMEMBER THAT THIS SCRIPT IS WORKING ON CONTACT FORM. SO ENSURE WHAT DATE FIELD YOUR ARE ACCESSING
        var now = new Date();
        var endDate = new Date().setDate(now.getDate()   7);
        dueDay.setValue(endDate);
        entity["scheduledend"] = new Date(endDate); //Set phone call 7 days due.
        
        //Set custom lookup field tts_role with value RoleType
        var role = formContext.data.entity.getAttribute("tts_role").getValue()[0].id;//<-- KEEP AN EYE ON THIS WHEN IT'S NULL CODE WILL BREAKE --- VALIDATE IT
        entity["tts_role@odata.bind"] = "tts_roles("   role   ")";//<-- KEEP AN EYE ON THIS, INSTEAD phonecall PLACE THE ENTITY's NAME(Plural) WHERE THE FIELD tts_role TARGETS.I'M SUPPOSING  IT'S tts_roles 
    
        Xrm.WebApi.online.createRecord("phonecall", entity).then(
          function success(result) {
            var newEntityId = result.id;
            Xrm.Utility.alertDialog("Phonecall created: " newEntityId);
          },
          function (error) {
            Xrm.Utility.alertDialog(error.message);
          }
        );
      }
    }

    please consider marking as an answer if it was helpful
  • DATAGREE Profile Picture
    DATAGREE 102 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Waiting for your kindly response.

  • DATAGREE Profile Picture
    DATAGREE 102 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Hi Leah Ju,

    Thank you for you great response.

    Now I have a new question, pls help for analyzing.

    The Rank option set actually have other two values: Start and Base.  But phone call activity trigger doesn't apply to this two values. This way hoe should I do?

    And based on your JS code, I also followed the requirement to extend it as below. Pls correct it, I believe it absolutely contain some mistakes.

    function compareValue(executionContext) {
      var formContext = executionContext.getFormContext();
      var contactID = formContext.data.entity.getId().replace('{', '').replace('}','');
      var ownerID = formContext.data.entity.getId().replace('{', '').replace('}','');
      var Rank = formContext.getAttribute("new_rank").getValue();//new rank value.
      var RankText = form.getAttribute("new_rank").getText();//new rank text.
      var OldRank = form.getAttribute("new_oldrank").getValue();//old rank value.
    
      //Create phone call when upgrade
      if (Rank > OldRank) {
    
        //Create phone call
        var entity = {};
        entity.subject = "Your rank have been upgraded to "   RankText; //Set phone call subject with new rank text
        entity["regardingobjectid_contact@odata.bind"] = "/contacts("   contactID   ")"; //Set phone call regarding field with current contact
        entity["from_contact@odata.bind"] = "/contacts("   ownerID   ")"; //Set phone call Form.
        entity["to_contact@odata.bind"] = "/contacts("   contactID   ")"; //Set phone call To.
    
        //Set Due Date is 7 calendar days from current date
        var dueDay = formContext.data.entity.getAttribute("scheduledend");
        var now = new Date();
        var endDate = new Date().setDate(now.getDate()   7);
        dueDay.setValue(endDate);
        entity["scheduledend_phonecall@odata.bind"] = "phonecall("   dueDay   ")"; //Set phone call 7 days due.
    
        //Set custom lookup field tts_role with value RoleType
        var role = formContext.data.entity.getAttribute("tts_role").getValue()[0].name;
        role.setValue(RoleType);
        entity["tts_role@odata.bind"] = "phonecall("   role   ")";
    
        Xrm.WebApi.online.createRecord("phonecall", entity).then(
          function success(result) {
            var newEntityId = result.id;
          },
          function (error) {
            Xrm.Utility.alertDialog(error.message);
          }
        );
      }
    }

    Thank you for your time.

    Looking forward to hearing from you.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Hi CRMKEY,

    You need create workflow and js together to achieve it, workflow is used to capture original rank value, js is used to compare new rank and original rank value and create phone call.

    1.Create a global option set field and create two local option set fields that using the global option set field in contact entity.

    (1)Global option set field:

    Set Rank1-Rank5 value are 1-5, which make compare value in the later steps easily.

    pastedimage1611908383784v1.png

    (2)Local option set fields:

    pastedimage1611908892584v2.png

    pastedimage1611908944345v3.png

    2.Add two local option set fields to the contact form, then save and publisj.

    3.Create a real-time workflow

    Make sure to uncheck “Run this workflow in the background (Recommended)” option.

    pastedimage1611909332087v4.png

    Save and Active it.

    3.Write js code and add it to web Resource.

    function CompareValue(executionContext) {
        var formContext = executionContext.getFormContext();
        var contactID = formContext.data.entity.getId().replace('{', '').replace('}', '');//current contact id
        var Rank = formContext.getAttribute("new_rank").getValue();//new rank value
        var RankText = formContext.getAttribute("new_rank").getText();//new rank text
        var OldRank = formContext.getAttribute("new_oldrank").getValue();//old rank value
        //create phone call when rank upgrade
        if (Rank > OldRank) {
    
            //Create Phone Call
            var entity = {};
            entity.subject = "Your Rank have upgraded to " RankText;//set phone call subject with new rank text
            entity["regardingobjectid_contact@odata.bind"] = "/contacts(" contactID ")";//set phone call regardng field with current contact
    
            Xrm.WebApi.online.createRecord("phonecall", entity).then(
                function success(result) {
                    var newEntityId = result.id;
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );
        }
    else
    {
        alert("Rank not upgrate");
    }
    }

    pastedimage1611912889186v6.png

    4.Add it to contact onSave event.

    pastedimage1611913007419v7.png

    5.Test:

    Before:

    pastedimage1611910400033v5.png

    After:

    Change Rank field to 'Rank3', then click save button.

    oldRank field will be filled with original value and phone call will be created automatically.

    pastedimage1611913124909v8.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • DATAGREE Profile Picture
    DATAGREE 102 on at
    RE: Contact Option Set field upgrade trigger phone call activity

    Looking forward to hearing from you.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans