Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to enable/disable two lookup fields on a form

Posted on by Microsoft Employee

hi everyone,


please help a newbie out who's just starting to use CRM Dynamics 2016.

this is the situation:
on a form I have multiple lookup fields, but I have to write some javascript to enable/disable two (2) of them
according to the choice of the user.
if one field is selected for data input, the other one should grey out and vice versa.
on form load all fields should be visible and ready for the user, it's just when the user sets focus on one of these two lookup fields, the other one cannot be used.
one field is a look up type, the other field is a party list type.

is there someone who can help me out? any help or code example is much appreciated!
thanx in advance
and have a nice day

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to enable/disable two lookup fields on a form

    thank you Radu,

    your code got me on the way to a solution.

    have a nice day

    Kath

  • Verified answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: How to enable/disable two lookup fields on a form

    Hi Kathleen,

    The code could look something like:

    ToggleFieldsVisibility : function ()
    {
        var lookup1 = Xrm.Page.getAttribute("lookup1");
        var lookup2 = Xrm.Page.getAttribute("lookup2");
    
        if(lookup1.getValue() != null && lookup2.getValue == null)
        {
            Xrm.Page.getControl("lookup2").setVisible(false);
        }
    
        else if(lookup1.getValue() == null && lookup2.getValue != null)
        {
            Xrm.Page.getControl("lookup1").setVisible(false);
        }
    }

    You would need to register this:

    - on change for both lookup fields

    - on load for existing records.

    Hope this helps

    Radu

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to enable/disable two lookup fields on a form

    Hi Radu

    Thank you for your reply.

    A Business Rule for my problem is not possible (please have a look at my other replies), however, the logical pseudo-code you suggested is what I'm looking for.

    'both fields should be visible on form load.

    if field A will contain data - then disable field B

    if field B will contain data - then disable field A '

    I've looked at the links you supplied, thanks.

    Could you be so kind to deliver me some code examples (or links to) to solve my problem? I'm aware the code I need has to be added to the onChange event of the form.

    Thanx, Kath

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How to enable/disable two lookup fields on a form

    Hi Kathleen,

    Below is the code i have kept on onchange event of the option set and on change of optionset value i show and hide fields

    function Hideshow()

    {

    var contactmethod=Xrm.Page.getAttribute("preferredcontactmethodcode"); // chage schema name with your schema name

    //option set text name

    if(contactmethod.getText()=='Email')

    {

    //Xrm.Page.getControl('fieldname1').setVisible(true);

    //Xrm.Page.getControl('fieldname2').setVisible(false);

    }

    else if(contactmethod.getText()=='Phone')

    {

    //Xrm.Page.getControl('fieldname2').setVisible(true);

    //Xrm.Page.getControl('fieldname1').setVisible(false);

    }

    else if(contactmethod.getText()=='Fax')

    {

    //show hide field

    }

    }

    If you find it helpful, Please mark it as verified answer.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to enable/disable two lookup fields on a form

    Hi Shahbaaz

    thanks for your reply.

    I'm struggling with the optionset solution you suggested (newbie here :-)

    more precise the if condition "if(objOptionInstance.getText()=='option set text name')"

    could you be so kind to share some guidance about this?

    because I can't find anything about it in the existing form where I should implement my code in.

    there are 8 fields on a form, I have to add a script that manipulates 2 of them.

    if 1 field gets selected by the user, the other field should be disabled. this has to work bothways.

    thanx, Kath

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How to enable/disable two lookup fields on a form

    Hi kathleen,

    Hi

    Create a javascript function to hide controls based on the optionset value and configure those function in then on change event of optionset field.

    Example:

    // get the field value on which you want to hide and show below two field

    var objOptionInstance=Xrm.Page.getAttribute("provide fieldSchemaName here");

    //option set text name

    if(objOptionInstance.getText()=='option set text name')

    {

    Xrm.Page.getControl('fieldname1').setVisible(true);

    Xrm.Page.getControl('fieldname2').setVisible(false);

    }

    else

    {

    Xrm.Page.getControl('fieldname2').setVisible(true);

    Xrm.Page.getControl('fieldname1').setVisible(false);

    }

    Thanks,

    Shahbaaz

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: How to enable/disable two lookup fields on a form

    Share your full code here for enquiry 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to enable/disable two lookup fields on a form

    Hi Vicky,

    Thanks for your reply.

    I already tried to solve it using Business Rules but for some reason one of the two fields (the party list type) does not appear in the 'field drop down list' making it impossible to enter a condition based on the two fields.

    I supposed Javascript was my only way to solve my problem.

    Thanx, Kath

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to enable/disable two lookup fields on a form

    hi Goutam,

    Thanks for your reply.

    I was aware of the method (.getControl) you suggested but it is actually the condition part of the code I am struggling with.  

    If a user selects one field for input, disable the other field and vice versa.

    Business rules don't seem to work because one of the 2 fields (the party list type) does not appear in my 'field drop down list'.

    thanx, Kath

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to enable/disable two lookup fields on a form

    Hi,

    You can achieve this by using business rule as well without writing any code.

    Here is the reference

    www.sherweb.com/.../configuring-business-rules-within-microsoft-dynamics-365-crm

    Using javascript also you can do this .

    For Enable

    Xrm.Page.getControl("fieldname").setDisabled(false);

    For Disable

    Xrm.Page.getControl("fieldname").setDisabled(true);

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans