web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Retrieve field value from lookup with Javascript

(0) ShareShare
ReportReport
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);
     
            });

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

    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;

  • smar.sus Profile Picture
    35 on at

    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

  • Suggested answer
    RodRodriguez Profile Picture
    on at

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

    This worked perfectly ! so simple!

    Thank you so much

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

News and Announcements

Season of Giving Solutions is Here!

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 > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 77 Super User 2025 Season 2

#3
TAHER Mehdi Profile Picture

TAHER Mehdi 46

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans