Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Java Script for a form to update a Case field with an Owner's field

(0) ShareShare
ReportReport
Posted on by 125

I am certainly not a developer and struggle with JScript.

What I need to do is to update a field on the Case form with the Owner's information.

If I am correct I place a web resource on the form that grabs the owner's information.  Then grabs the system user field value and drops it into the field on the Case form.

Incident field = new_ownersalsbusinessstream (look up field)

SystemUser field = new_alsbusinessstream (look up field)

Can someone please assist?

  • ScottColbourne Profile Picture
    ScottColbourne 125 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Thank you Adrian - I have now ticked the box.  The issue now is it does not seem to do anything.

    I presume I have something wrong in my code.

    Basically I only changed where it stated fieldName to new_alsbusinessstream and for the destination field on the form - new_ownersalsbusinessstream.

    Isn't that correct?

    Here it is:

    function getValueFromOwnerLookUp(executionContext){

       var formContext=executionContext.getFormContext();

       //get lookup field

       var Lookup_Cat=formContext.getAttribute("ownerid");

       if(Lookup_Cat!=null){

           //get lookup field record id

           var value=Lookup_Cat.getValue();

           var id=value[0].id;

           //use record id to retrieve other field value

           Xrm.WebApi.retrieveRecord("systemuser",id,"?$select=new_alsbusinessstream").then(

               function success(result){

                   //get the systemuser field value and set it to field on case

                   var fieldValue=result.new_alsbusinessstream;

                   formContext.getAttribute("new_ownersalsbusinessstream").setValue(fieldValue);

               },

               function (error){

                   console.log(error.message);

               }

           )

       }

    }

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,019 Super User 2025 Season 1 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Hi Scott Colbourne,

    You need to pass execution context as the first parameter in your JavaScript function.

    clientapi_2D00_passexecutioncontext.png

  • ScottColbourne Profile Picture
    ScottColbourne 125 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Thank you Leo.

    Unfortunately I get this error:

    TypeError: Unable to get property 'getFormContext' of undefined or null reference

      at getValueFromOwnerLookUp (dynamicssasandbox.crm6.dynamics.com/.../alssc_caseALSBusinessStreamComplete:2:5)

      at sp.prototype.executeFunction (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at sp.prototype.execute (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at tp.prototype._executeIndividualEvent (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at tp.prototype._executeEventHandler (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at execute (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at O.prototype._executeSyncAction (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at O.prototype._executeSync (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at O.prototype.executeAction (dynamicssasandbox.crm6.dynamics.com/.../app.js

      at t.dispatch (dynamicssasandbox.crm6.dynamics.com/.../app.js

    This was on a new or existing case.

    Any ideas?

  • LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Hi partner,

    If you want to get the system user information by owner lookup field and set the data to fields on case, you could use the web api provided by D365.

    Sample:

    function getValueFromOwnerLookUp(executionContext){
        var formContext=executionContext.getFormContext();
        //get lookup field
        var Lookup_Cat=formContext.getAttribute("ownerid");
        if(Lookup_Cat!=null){
            //get lookup field record id
            var value=Lookup_Cat.getValue();
            var id=value[0].id;
            //use record id to retrieve other field value
            Xrm.WebApi.retrieveRecord("systemuser",id,"?$select=fieldname").then(
                function success(result){
                    //get the systemuser field value and set it to field on case
                    var fieldValue=result.fieldname;
                    formContext.getAttribute("fieldname").setValue(fieldValue);
                },
                function (error){
                    console.log(error.message);
                }
            )
    
        }
        
    }

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord

    Hope it helps.

    Best Regards,

    Leo

  • ScottColbourne Profile Picture
    ScottColbourne 125 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    jscript makes sense as I do need it to update right away, before the record is created.

    Your second option is what I have now, but it is cuasing filtering issues on a dependent lookup, as this only works once the record is saved.

    I will look at your link and try and put together the script to adapt it to a case and the owner's (system user) custom field.

  • erhan.keskin Profile Picture
    erhan.keskin 2,253 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Hi Scott,

    There is a couple of ways to achieve that. However, it all depends on what you exactly need.

    For instance;

    • If you need to populate the value(s) on the form straight away, then jscript might make sense. Please check this out; https://carldesouza.com/set-retrieve-id-lookup-value-using-xrmwebapi/
    • If you don't mind the field(s) is populated after saving the record,
      • then you can create a WF to populate the field(s) on the form with the information of owner
      • or you can populate the field(s) in a plugin

    Regards,

  • ScottColbourne Profile Picture
    ScottColbourne 125 on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Sorry Andrew, but I need the field on the Case record to use it for filtering another lookup field.

    Therefore unfortunately a quick view form does not achieve what I need.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Java Script for a form to update a Case field with an Owner's field

    Hello Scott,

    I believe you have no need in any development. You should consider usage of Quick View forms - docs.microsoft.com/.../create-edit-quick-view-forms

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans