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 :
Microsoft Dynamics CRM (Archived)

How to enable/disable two lookup fields on a form

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    Hi Kathleen,

    You can explore several options:

    - Via Business Rules

    - Via JavaScript registered on change events for the lookup fields

    The logic this should follow is following

    both fields should be visible on form load.

    if field a contains data - then clear and hide field b

    if field b contains data - then clear and hide field a

    unfortunately you can't register any event handlers on the set focus event. The only events that support custom event handlers in CRM are : load, change and save.

    Here are some links to help you get started:

    msdn.microsoft.com/.../hh771584.aspx

    docs.microsoft.com/.../create-business-rules-recommendations-apply-logic-form

    Hope this helps

    Radu

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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);

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

    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

  • Mahendar Pal Profile Picture
    45,095 on at

    Share your full code here for enquiry 

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    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

  • Community Member Profile Picture
    on at

    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

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    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
    on at

    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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans