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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Conditional Statement on a lookup field in JS

(0) ShareShare
ReportReport
Posted on by 36

Hello, 

I am using js to display or hide buttons on a form. 

One snippet of the query is: 

if (attributes[i].getIsDirty()
&& attributes[i].getName() != "decision" && attributes[i].getName() != "decisiondate" ) {<do stuff>}

The condition is working correctly on the normal date field, however is not working on the 'decision' field which is a lookup field. 

Do I need different syntax to get this to evaluate a lookup field correctly? 

Thanks in advance. 

I have the same question (0)
  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Alex Br,

    Add alerts or debug the code in both conditions where it is working and where it is not working. The issue might be resolved by you.

    If I answer your question then please mark it as verified.

    Let me know if I can provide you with more details.

    Thanks
    Regards,

    Abdul Wahab
    Power Platform & Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp:+923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: www.linkedin.com/.../

  • Tamilarasu Arunachalam Profile Picture
    587 on at

    For lookups you have to get the name like below

    attributes.get("fieldname").getValue()[0].name

    reference

    community.dynamics.com/.../get-lookup-field-value-through-javascript

  • Suggested answer
    Ray Profile Picture
    1,537 on at

    Hi Alex Br,

    Can you give more context about the attributes array?

    And you should be able to debug by adding some console logs to see the return value of "getName()", sometime the name would be different, it is determined by how/where you get the attributes array.

  • Thomas David Dayman Profile Picture
    11,323 on at

    if (typeof CRM === 'undefined') CRM = {};

    if (typeof CRM.EntityName === "undefined") CRM.EntityName = {};


    CRM.EntityName.Onload = function (executionContext) {
    var formContext = executionContext.getFormContext();

    formContext.getAttribute("decision").addOnChange(function () { CRM.EntityName.Function(formContext); });
    formContext.getAttribute("decisiondate").addOnChange(function () { CRM.EntityName.Function(formContext); });

    }
    };

    CRM.EntityName.Function = function (formContext) {

    //Do Stuff
    };

    Above is an example of how to trigger logic based on onchange of specific fields. You can use this as reference

  • Suggested answer
    Muhammad Ehtisham Siddiqui Profile Picture
    on at

    First take value in a variable

    var abc=attributes.get("fieldname").getValue()[0].name

    Then Apply condition

    if (abc!=null && abc!=""){

    //Code for showing Button

    }else {

    //Code For Hide Button

    }

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

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans