Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Conditional Statement on a lookup field in JS

Posted on by 32

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. 

  • Suggested answer
    RE: Conditional Statement on a lookup field in JS

    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

    }

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Conditional Statement on a lookup field in JS

    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
    Ray Profile Picture
    Ray 1,505 on at
    RE: Conditional Statement on a lookup field in JS

    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.

  • RE: Conditional Statement on a lookup field in JS

    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
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Conditional Statement on a lookup field in JS

    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/.../

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans