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

Announcements

No record found.

News and Announcements icon
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
    593 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 166

#2
ManoVerse Profile Picture

ManoVerse 149 Super User 2026 Season 1

#3
Zhilan Profile Picture

Zhilan 55

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans