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 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?

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

  • ScottColbourne Profile Picture
    125 on at

    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.

  • erhan.keskin Profile Picture
    2,253 on at

    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
    125 on at

    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.

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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
    125 on at

    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?

  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi Scott Colbourne,

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

    clientapi_2D00_passexecutioncontext.png

  • ScottColbourne Profile Picture
    125 on at

    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);

               }

           )

       }

    }

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans