Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Retrieve field value from lookup with Javascript

Posted on by 35

Hi all,

unfortunately I am stuck with this code here.

From a Project I create a Case via a custom button (+ New Ticket - IT), it works fine.

I now need to add in my following Javascript, to lookup the account in this project and pass it to the case form

field "customerid", when Form is opening.

This goes beyond my knowledge of javascript at the moment, I am still learning.

function openItTicketForm (primaryControl) {

        var formContext = primaryControl;
     
        var recordGuid = formContext.data.entity.getId().replace("{", "").replace("}", "");
     
        var recordName = formContext.getAttribute("pde_name").getValue();
     
        var recordType = "pde_project";
     
        var entityFormOptions = {};
     
        entityFormOptions["entityName"] = "incident";
     
        entityFormOptions["openInNewWindow"] = true;

        entityFormOptions["formId"] = "3962b5bd-1ad2-ed11-a7c7-000d3adf75ee";

        // Set lookup column
     
        var formParameters = {};
     
     
        formParameters["skt_pde_project"] = [{ id: recordGuid, name: recordName, entityType: recordType }];
        formParameters["skt_requestto"] = 921720001;

        // Open the form.
     
        Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
     
            function (success) {
     
                console.log(success);
     
            },
     
            function (error) {
     
                console.log(error);
     
            });

           
         
     
     }
  • smar.sus Profile Picture
    smar.sus 35 on at
    RE: Retrieve field value from lookup with Javascript

    This worked perfectly ! so simple!

    Thank you so much

  • Verified answer
    RodRodriguez Profile Picture
    RodRodriguez on at
    RE: Retrieve field value from lookup with Javascript

    You want to lookup the account in the project and pass it to the case form field “customerid” when the form is opening. To do that, you need to use the Xrm.Page.getAttribute method to get the value of the account lookup field in the project form, and then use the Xrm.Page.getAttribute method again to set the value of the customerid lookup field in the case form. You also need to specify the id, name and entityType of the lookup value.

    Here is an example of how you can modify your code:

           var formContext = primaryControl;

           var recordGuid = formContext.data.entity.getId().replace("{", "").replace("}", "");

           var recordName = formContext.getAttribute("pde_name").getValue();

           var recordType = "pde_project";

           // Get the account lookup value from the project form

           var account = formContext.getAttribute("pde_account").getValue(); // change pde_account to your actual account lookup field name

           var entityFormOptions = {};

           entityFormOptions["entityName"] = "incident";

           entityFormOptions["openInNewWindow"] = true;

           entityFormOptions["formId"] = "3962b5bd-1ad2-ed11-a7c7-000d3adf75ee";

           // Set lookup column

           var formParameters = {};

           formParameters["skt_pde_project"] = [{ id: recordGuid, name: recordName, entityType: recordType }];

           formParameters["skt_requestto"] = 921720001;

           // Set the customerid lookup value to the account value

           formParameters["customerid"] = account; // make sure account is not null or undefined

           // Open the form.

           Xrm.Navigation.openForm(entityFormOptions, formParameters).then(

               function (success) {

                   console.log(success);

               },

               function (error) {

                   console.log(error);

               });

        }

  • smar.sus Profile Picture
    smar.sus 35 on at
    RE: Retrieve field value from lookup with Javascript

    Thanks for your reply!

    Maybe I was not clear enough :) The above Code works fine. No need to change this.

    But I want to add to the above code following:

    The case form is linked to a project via a lookup, I want to retrieve the account from this project and prefill the Account field in the case form automatically as soon as the project is linked.

    2023_2D00_04_2D00_24-20_5F00_24_5F00_35_2D00_-Dynamics-365.png

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Retrieve field value from lookup with Javascript

    Hello,

    Try to replace line

    formParameters["skt_pde_project"] = [{ id: recordGuid, name: recordName, entityType: recordType }];

    with lines

    formParameters["skt_pde_project"] = recordGuid;

    formParameters["skt_pde_projectname"] = recordName;

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans