Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Auto Refresh After Flow Run & Field Value Change

Posted on by 70

Hi,

I have created a flow in Power Automate which will set the value of a field based on changed value of a Lookup field. Now the problem is since flows are asynchronous, I can see the change in my field value only after I save and refresh the page. So if I change my Lookup value, I dont see a corresponding change in my other field unless I refresh the page. But I want the change in the other field to show as soon as my Lookup value changes. So I tried using the data.refresh JS code that is supposed to refresh without loading the page, on OnLoad & OnChange event of my Lookup field, but somehow my function is not working. Here is my code...

function onLookUpChange(executionContext) {

var formContext = executionContext.getFormContext();

formContext.data.refresh(save).then(successCallback, errorCallback);

}

Can someone please tell me what is wrong with my code? Or if you know any other way of loading the field value as soon as the Look Up value changes, please let me know.

Thanks!

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Auto Refresh After Flow Run & Field Value Change

     

    Wow my code is working perfectly fine now, thanks a lot   !! 

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Refresh After Flow Run & Field Value Change

    Hi Partner,

    You should change two places if you want to check null for lookup field:

    1.'if (lookupPersonnel != null) {' behind 'var lookupPersonnel = formContext.getAttribute("new_personnelname").getValue();'

    2. Move the following code outside the if() synax:

    // get the Designation field
    var designation = formContext.getAttribute("new_designation");

    Full code:

    function setDesignation(executionContext) {
        var formContext = executionContext.getFormContext();
    
        // get the Designation field
        var designation = formContext.getAttribute("new_designation");
    
        // get Lookup details
        var lookupPersonnel = formContext.getAttribute("new_personnelname").getValue();
        if (lookupPersonnel != null) {
            var lookupId = lookupPersonnel[0].id;
            var lookupName = lookupPersonnel[0].name;
            var lookupEntityName = lookupPersonnel[0].entityType;
    
            //Get contact's designation
            Xrm.WebApi.online.retrieveRecord("contact", lookupId, "?$select=new_designation").then(
                function success(result) {
                    console.log(result);
                    // Columns
                    var contactid = result["contactid"]; // Guid
                    var new_designation = result["new_designation"]; // Text
    
                    //Set current designation with value from contact
                    designation.setValue(new_designation);
    
                },
                function (error) {
                    console.log(error.message);
                }
            );
        }
        else {
            designation.setValue("");
        }
    }
    

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Auto Refresh After Flow Run & Field Value Change

       

    Wow thank you so much Leah for taking the effort to try out my code, really appreciate it! And thanks for the Dataverse Rest Builder tool suggestion, I will check it out. I tried your code, and it worked mostly. The only one small issue I have is while checking for the null value. Without checking the null condition when I'm writing this code, I'm able to get the right Designation for each contact, but when the Lookup field is empty, the Designation field still shows the designation of the previous person. So I tried using the null condition to set the Designation field as empty if the Lookup field is empty. I put the Designation setting code inside the 'If' condition, ie., if (lookupPersonnel != null) and the code is working perfectly fine for this part of the condition, however, when the 'else' part comes (i.e., if lookupPersonnel == null), I'm getting an error message 'Cannot read properties of null (reading '0')'. So the 'If' part is working perfectly but somehow the 'else' part is not. So I have been trying different things to see if it works (like trying !lookupPersonnel instead of !=, etc.) but I'm just not able to get the else part of the code to work. So I'm still trying to figure it out. This is the entire code including the null condition...

    function setDesignation(executionContext) {

    var formContext = executionContext.getFormContext();

    // get Lookup details
    var lookupPersonnel = formContext.getAttribute("new_personnelname").getValue();
    var lookupId = lookupPersonnel[0].id;
    var lookupName = lookupPersonnel[0].name;
    var lookupEntityName = lookupPersonnel[0].entityType;

    // get the Designation field
    var designation = formContext.getAttribute("new_designation");

    if (lookupPersonnel != null) {

       //Get contact's designation
       Xrm.WebApi.online.retrieveRecord("contact", lookupId, "?$select=new_designation").then(
       function success(result) {
         console.log(result);
         // Columns
         var contactid = result["contactid"]; // Guid
         var new_designation = result["new_designation"]; // Text

         //Set current designation with value from contact
         designation.setValue(new_designation);

         },
       function (error) {
         console.log(error.message);
         }
      );
    }
    else {
       designation.setValue("");
      }
    }

    Thanks a lot!

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Refresh After Flow Run & Field Value Change

    Hi Partner,

    You need use Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback); to get field value from contact based on the GUID of the lookup field.

    And you can try to use Dataverse REST Builder tool, which is is an application to help you build code easily.

    Download solution: https://github.com/GuidoPreite/DRB/releases

    You can use it build code easily and execute it.

    (1)After install the solution, open it and New Collection:

    pastedimage1663307705268v6.png

    (2)New Request, configure it as following screenshot.

    pastedimage1664433465049v1.png

    (3)In ‘Crm.webapi’ tab, you can get web api code you need:

    pastedimage1664433588697v2.png

    Finally, you can use setvalue() to populate field in js.

    Example code:

    function setDesignation(executionContext) {
        var formContext = executionContext.getFormContext();
    
        // get Lookup details
        var lookupPersonnel = formContext.getAttribute("new_personnelname").getValue();
        var lookupId = lookupPersonnel[0].id;
        var lookupName = lookupPersonnel[0].name;
        var lookupEntityName = lookupPersonnel[0].entityType;
    
        // get the Designation field
        var designation = formContext.getAttribute("new_designation");
    
        //Get contact's designation
        Xrm.WebApi.online.retrieveRecord("contact", lookupId, "?$select=new_designation").then(
            function success(result) {
                console.log(result);
                // Columns
                var contactid = result["contactid"]; // Guid
                var new_designation = result["new_designation"]; // Text
    
                //Set current designation with value from contact
                designation.setValue(new_designation);
    
            },
            function (error) {
                console.log(error.message);
            }
        );
    }

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Auto Refresh After Flow Run & Field Value Change

    Hi Bipin Kumar ,

    Thank you for your response. I tried Real time workflow too, but there also you can see the changes only once you have saved the record. So I thought if there is a JS code that can refresh the values on onChange event of the lookup, the flow can run immediately which would be perfect, and thats how I saw the above mentioned code that is supposed to refresh the values as soon as the Lookup value changes. But I'm ok with using a JS code too instead. In my scenario, I have a lookup named 'Personnel Name' in which you can choose a person from the Contact entity. My requirement is - I have another field named 'Designation' after the Personnel Name lookup field, so based on the person selected in the lookup field, I want the designation of that person to automatically be populated in the 'Designation' field from the designation field in the Contact entity. 

    I tried writing a JS code, but I was able to write only upto the point of getting the Lookup field values. So can you please help me finish this code to populate the 'Designation' field from Contact entity...

    function setDesignation(executionContext) {

        var formContext = executionContext.getFormContext();

         // get Lookup details

        var lookupPersonnel = formContext.getAttribute("new_personnelname").getValue();

        var lookupId = lookupPersonnel[0].id;

        var lookupName = lookupPersonnel[0].name;

        var lookupEntityName = lookupPersonnel[0].entityType;

         // get the Designation field

        var designation = formContext.getAttribute("new_designation");

    }

    Thank you!

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Auto Refresh After Flow Run & Field Value Change

    Hi,

    You can use Real time workflow/ Javascript code to auto populate Fields based on your lookup field

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans