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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Setting required filed based on option set

(0) ShareShare
ReportReport
Posted on by

Hi experts, 

I am new to CRM and trying to learn. I am trying to customise the contact form. My case is as follows

I have created a date of birth type as option set with following 

Accurate - 1

Estimate - 2

Unknown - 3

Now on the form, i have date of birth field. If the date of birth type is either accurate (1) or estimate (2), then i want to make the date of birth as required. Otherwise date of birth is not required. Here is the script. But it is not working. Anticipating help

function DateOfBirthType() {


var DOB_Type =
Xrm.Page.getAttribute('new_dateofbirthtype').getValue();

if (DOB_Type == 1 || DOB_Type == 2){

Xrm.Page.getAttribute('new_dateofbirth').setRequiredLevel('required');
}

else {
Xrm.Page.getAttribute('new_dateofbirth').setRequiredLevel('none');

}

}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi Tashi,

    First thing you don't need to use code for this, you can simply create a business rule which will be easy to maintain even for a non technical user.

    And your code seems correct where are you calling this code ?? make sure your code is saved and published and you are using correct fields name

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi Tashi,

    Your code looks ok.

    How are you calling the DateOfBirthType function.

    You have two options:

    1. You can add the script web resource to the form properties, and set the change event of the new_dateofbirth field of the form.

    2. Add the addOnChange event to your attribute within your form script library:

    msdn.microsoft.com/.../gg334409.aspx

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi Aric and Mahender,

    Thanks for the prompt response. Yea, i can create the Business Rules. But i just wanted to do it with script. The script is added to the web resources and set to the change event of the new_dateofbirth. Still can't get it as expected

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Are you getting any error?

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    When the form loads, press F12 (Developer Tools).

    Find the web resource that you are using in the list of web resources (under debugger tab).

    Find the change event that you created, and put a breakpoint in the first line of the function.

    This will help you pinpoint the location of the problem.

    In your form, change the value of the Date of Birth field.

    If the debugger never reaches the breakpoint, it has something to do with the event.

    If it does, go line by line (using F10 and F11 keyboard buttons) until you receive an error, or see what the problem is.

    Hope this helps.

  • Mahendar Pal Profile Picture
    45,095 on at

    You can also put debugger key work in your code which will be helpful to debug and see if it's reaching to your code or not.

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    Your code seems ok. Just simplified a little:

    function DateOfBirthType() {
    	var dobType = Xrm.Page.getAttribute('new_dateofbirthtype').getValue();
    	var requiredLevel = dobType == 1 || dobType == 2 ? 'required' : 'none';
    	
    	Xrm.Page.getAttribute('new_dateofbirth').setRequiredLevel(requiredLevel); 
    }


    For registering this event, please don't forget to call this function OnLoad and onchange from new_dateofbirthtype:

    function Form_OnLoad(){
    	DateOfBirthType();
    }
    
    function DateOfBirthTypeOnChange(){
    	DateOfBirthType();
    }


  • Community Member Profile Picture
    on at

    Hi all,

              Thanks a lot. it worked!

     Temmy.....Thanks for the code!

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans