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

Community site session details

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

how to get the attributes of a record in lookup field using JS

(0) ShareShare
ReportReport
Posted on by 20
function retrievestatus(executionContext) {

    // Access the field on the form
    var field = executionContext.getFormContext().getAttribute("ctp_ctpapplicantsappversion");


        // Get its field value; Returns the Lookup object
        var value = field.getValue();

        // To get the Id, Name and Entity Name (account/contact)
        var record_status = field.getValue()[0].statuscode;
   

    if(record_status == "inactive")
       
    {
        formContext.ui.setFormNotification("There is an update for cTP Applicants App");

    }
}
I have the same question (0)
  • Verified answer
    Mohamed GRAIB Profile Picture
    2,504 Moderator on at
    RE: how to get the attributes of a record in lookup field using JS

    Hi, 

    Try to use this code 

    async function Getlookupvalue(executionContext){

    var formContext = executionContext.getFormContext();
    var XValue = formContext.getAttribute("column_name").getValue();


    var sXId = XValue[0].id.slice(1,-1);
    var Xentitytype = XValue[0].entityType;
    var resultaccount = await Xrm.WebApi.retrieveRecord(siteentitytype ,XId,"?$select=_Column_name_value");
    var lookupValuetable_name = new Array();
    lookupValuetable_Name[0] = new Object();
    lookupValuetable_name[0].id = "{"+ resultaccount["_Column_name_value"] +"}";
    lookupValueAccount[0].name = resultaccount["_parentaccountid_value@OData.Community.Display.V1.FormattedValue"];
    lookupValuetable_name[0].entityType = siteentitytype;
    formContext.getAttribute("yourcolumn").setValue(lookupValue table_name);


    }

    X : name of your variable 

    Column name : The field or colum that you want to get 

    Your column : The column where you will get the data

    Table name : Table from where you get data. 

    I hope that's helpful for you. 

    Please mark  the answer as verified if that's resolve your problem. 

  • Verified answer
    Eiken Profile Picture
    on at
    RE: how to get the attributes of a record in lookup field using JS

    The Lookup field is stored as an Array and each item in the array has three parameters(GUID, Name, Entity Name).

    The following link will show more details about Lookup field.

    How to set lookup field value using JavaScript Dynamics CRM - Microsoft Dynamics 365 Community

    After looking at your code, did you want to set the FormNotification when the Look up filed shows “ inactive”?

    Now I try to modify your code.

    function getLookUp(e){
        var formContext = e.getFormContext();
        //get the Array of lookup field value
        var lookUpValue= formContext.getAttribute("ctp_ctpapplicantsappversion").getValue()
        //get the name of currently lookup field
        var isValue=lookUpValue[0].name;
        if(isValue=="inactive"){
        //setFormNotification
            formContext.ui.setFormNotification("There is an update for cTP Applicants App","INFO");
        }
        }

    pastedimage1671687691034v1.png

  • Verified answer
    CRMJetty Profile Picture
    3,512 on at
    RE: how to get the attributes of a record in lookup field using JS

    Hello Manth,

    If you are using to get lookup details through JavaScript , Here is example :

    if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) {

    var CustomerId = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

    var CustomerName = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].name;

    var CustomerType = Xrm.Page.data.entity.attributes.get("feildname ").getValue()[0].entityType;

    }

    //Display the lookup name

    alert(CustomerName);

    I hope this helps you replace fieldname with your lookup field name in Form

    let me know if you find any errors.

    Please mark the answer as verified if that's helpful for you.

  • Manth Profile Picture
    20 on at
    RE: how to get the attributes of a record in lookup field using JS

    function displaynotification(executionContext){

    var formContext = executionContext.getFormContext();

    debugger;

    var conatt = formContext.getAttribute("ctp_ctpapplicantsappversion");

    var conatt1 = formContext.getAttribute("ctp_ctpapplicantsappversion").getValue();

       if( conatt && conatt1 != null){

       var conID = conatt1[0].id;  

       var infoID = "info";

       var warningid = "warning";

       Xrm.WebApi.retrieveRecord("ctp_ctpapplicantsappversion", "cad0116f-ee80-ed11-81ad-000d3a565815", "?$select=statecode").then(

           function success(result) {

               var res = result.statecode().getValue();

               if(res == "active" ){

                   formContext.ui.setFormNotification("Record ID of CTP Applicants APP is" + conID, "INFO", infoID);

               }        

           }

       );      

     }

    }

    What wrong in this code when i try to load this script on form onload i am getting below error

    ReferenceError: Web resource method does not exist: displaynotification

       at y._handleMethodNotExistError (ctp.crm.dynamics.com/.../app.js

       at y.execute (ctp.crm.dynamics.com/.../app.js

       at ctp.crm.dynamics.com/.../app.js

       at i (ctp.crm.dynamics.com/.../app.js

       at ee._executeIndividualEvent (ctp.crm.dynamics.com/.../app.js

       at ee._executeEventHandler (ctp.crm.dynamics.com/.../app.js

       at Object.execute (ctp.crm.dynamics.com/.../app.js

       at N._executeSyncAction (ctp.crm.dynamics.com/.../app.js

       at N._executeSync (ctp.crm.dynamics.com/.../app.js

       at N.executeAction (ctp.crm.dynamics.com/.../app.js

       at t.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at dispatch (ctp.crm.dynamics.com/.../app.js

       at It (ctp.crm.dynamics.com/.../17.js

       at ctp.crm.dynamics.com/.../24.js

    Error Details:

    Event Name: onload

    Function Name: displaynotification

    Web Resource Name: ctp_configstatus

    Solution Name: Active

    Publisher Name: DefaultPublisherorg524de1c3

    Please help me resolve it

  • Manth Profile Picture
    20 on at
    RE: how to get the attributes of a record in lookup field using JS

    function displaynotification(executionContext){

    var formContext = executionContext.getFormContext();

    debugger;

    var conatt = formContext.getAttribute("ctp_ctpapplicantsappversion");

    var conatt1 = formContext.getAttribute("ctp_ctpapplicantsappversion").getValue();

       if( conatt && conatt1 != null){

       var conID = conatt1[0].id;  

       var infoID = "info";

       var warningid = "warning";

       Xrm.WebApi.retrieveRecord("ctp_ctpapplicantsappversion", "cad0116f-ee80-ed11-81ad-000d3a565815", "?$select=statecode").then(

           function success(result) {

               var res = result.statecode().getValue();

               if(res == "active" ){

                   formContext.ui.setFormNotification("Record ID of CTP Applicants APP is" + conID, "INFO", infoID);

               }        

           }

       );      

     }

    }

    What wrong in this code when i try to load this script on form onload i am getting below error

    ReferenceError: Web resource method does not exist: displaynotification

       at y._handleMethodNotExistError (ctp.crm.dynamics.com/.../app.js

       at y.execute (ctp.crm.dynamics.com/.../app.js

       at ctp.crm.dynamics.com/.../app.js

       at i (ctp.crm.dynamics.com/.../app.js

       at ee._executeIndividualEvent (ctp.crm.dynamics.com/.../app.js

       at ee._executeEventHandler (ctp.crm.dynamics.com/.../app.js

       at Object.execute (ctp.crm.dynamics.com/.../app.js

       at N._executeSyncAction (ctp.crm.dynamics.com/.../app.js

       at N._executeSync (ctp.crm.dynamics.com/.../app.js

       at N.executeAction (ctp.crm.dynamics.com/.../app.js

       at t.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at dispatch (ctp.crm.dynamics.com/.../app.js

       at It (ctp.crm.dynamics.com/.../17.js

       at ctp.crm.dynamics.com/.../24.js

    Error Details:

    Event Name: onload

    Function Name: displaynotification

    Web Resource Name: ctp_configstatus

    Solution Name: Active

    Publisher Name: DefaultPublisherorg524de1c3

    Please help me resolve it

  • Manth Profile Picture
    20 on at
    RE: how to get the attributes of a record in lookup field using JS

    function displaynotification(executionContext){

    var formContext = executionContext.getFormContext();

    debugger;

    var conatt = formContext.getAttribute("ctp_ctpapplicantsappversion");

    var conatt1 = formContext.getAttribute("ctp_ctpapplicantsappversion").getValue();

       if( conatt && conatt1 != null){

       var conID = conatt1[0].id;  

       var infoID = "info";

       var warningid = "warning";

       Xrm.WebApi.retrieveRecord("ctp_ctpapplicantsappversion", "cad0116f-ee80-ed11-81ad-000d3a565815", "?$select=statecode").then(

           function success(result) {

               var res = result.statecode().getValue();

               if(res == "active" ){

                   formContext.ui.setFormNotification("Record ID of CTP Applicants APP is" + conID, "INFO", infoID);

               }        

           }

       );      

     }

    }

    What wrong in this code when i try to load this script on form onload i am getting below error

    ReferenceError: Web resource method does not exist: displaynotification

       at y._handleMethodNotExistError (ctp.crm.dynamics.com/.../app.js

       at y.execute (ctp.crm.dynamics.com/.../app.js

       at ctp.crm.dynamics.com/.../app.js

       at i (ctp.crm.dynamics.com/.../app.js

       at ee._executeIndividualEvent (ctp.crm.dynamics.com/.../app.js

       at ee._executeEventHandler (ctp.crm.dynamics.com/.../app.js

       at Object.execute (ctp.crm.dynamics.com/.../app.js

       at N._executeSyncAction (ctp.crm.dynamics.com/.../app.js

       at N._executeSync (ctp.crm.dynamics.com/.../app.js

       at N.executeAction (ctp.crm.dynamics.com/.../app.js

       at t.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at Object.dispatch (ctp.crm.dynamics.com/.../app.js

       at dispatch (ctp.crm.dynamics.com/.../app.js

       at It (ctp.crm.dynamics.com/.../17.js

       at ctp.crm.dynamics.com/.../24.js

    Error Details:

    Event Name: onload

    Function Name: displaynotification

    Web Resource Name: ctp_configstatus

    Solution Name: Active

    Publisher Name: DefaultPublisherorg524de1c3

    Please help me resolve it

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans