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

how to set the Age Range according to the Age value entered by the user JavaScript

(0) ShareShare
ReportReport
Posted on by

I'm using following function to set the age range but it's giving me an error..

//get the value of integer age//
//set the value of age range//
//if age is between 18 & 25 then choose option 1//
//if age is between 26 & 35 then choose option 2//
//if age is between 36 & 55 then choose option 3//
//if age is between 56 & 65 then choose option 4//


function SetAgeRange()
{
var Age= Xrm.Page.getAttribute("new_age").getValue ();
if (Age>=18 && Age<=25)
{
Xrm.Page.getAttribute("new_age").setValue("100,000,000");
}
else if ( Age>=26 && Age<= 35)
{
Xrm.Page.getAttribute("new_age").setValue("100,000,001");
}
else if (Age>=36 && Age<=55)
{
Xrm.Page.getAttribute("new_age").setValue("100,000,002");
}

else if (Age>=56 && Age<= 65)
{
Xrm.Page.getAttribute("new_age").setValue("100,000,003");
}

var AgeRange=Xrm.Page.getAttribute("new_age").setValue();
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Both the fields you are setting and getting value are same.

    var Age= Xrm.Page.getAttribute("new_age").getValue ();

    Xrm.Page.getAttribute("new_age").setValue("100,000,000");

    Change it to correct field names like this:

    var Age= Xrm.Page.getAttribute("IntegerAgeFieldName").getValue ();

    Xrm.Page.getAttribute("PicklistAgeFieldName").setValue("100000000");

  • Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    What type of field is new_age? Is it an option set or number field?

    Ideally, two fields should be there. A number field new_age and an option set field  new_ageRange.

    Try the code below and register the assembly on change of new_age field value.

    function SetAgeRange()
    {
    var Age= Xrm.Page.getAttribute("new_age").getValue ();
    if (Age>=18 && Age<=25)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100,000,000");
    }
    else if ( Age>=26 && Age<= 35)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100,000,001");
    }
    else if (Age>=36 && Age<=55)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100,000,002");
    }
    else if (Age>=56 && Age<= 65)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100,000,003");
    }
    
    var AgeRange=Xrm.Page.getAttribute("new_ageRange").getValue();
    }

    Hope this helps.

  • Community Member Profile Picture
    on at

    Thank you Nithya,

    yes new_age is number field while new_agerange is option set, i tried this code still the same problem the range is always giving me 18-25 even if i entered 50 in the age field...

  • Community Member Profile Picture
    on at

    it didn't work..

    Thank you

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    Can you please replace the code with optionset values described below?

    function SetAgeRange()
    {
    var Age= Xrm.Page.getAttribute("new_age").getValue ();
    if (Age>=18 && Age<=25)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100000000");
    }
    else if ( Age>=26 && Age<= 35)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100000001");
    }
    else if (Age>=36 && Age<=55)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100000002");
    }
    else if (Age>=56 && Age<= 65)
    {
    Xrm.Page.getAttribute("new_ageRange").setValue("100000003");
    }
    
    var AgeRange=Xrm.Page.getAttribute("new_ageRange").getValue();
    }

    Remove the commas between the option set values. Hope this helps.

  • Suggested answer
    Alagunellaikumar Profile Picture
    6,212 on at

    Hi

    Please follow the below step and let u sknwo

    Comment the onchange code and place the below code

    Xrm.Page.getAttribute("new_ageRange").setValue("100000002");

    Check whether the value is set or not

    If it is set then remove the comma in the setValue.

  • Verified answer
    Community Member Profile Picture
    on at

    yes the value of each option is set in the fields.

    you mean to put it like following?

    function SetAgeRange()

    {

    var Age= Xrm.Page.getAttribute("new_agerange").getValue ();

    if (Age>=18 && Age<=25)

    {

    Xrm.Page.getAttribute("new_agerange").setValue("100,000,000")

    }

    else if ( Age>=26 && Age<= 35)

    {

    Xrm.Page.getAttribute("new_agerange").setValue("100,000,001")

    }

    else if (Age>=36 && Age<=55)

    {

    Xrm.Page.getAttribute("new_agerange").setValue("100,000,002")

    }

    else if (Age>=56 && Age<= 65)

    {

    Xrm.Page.getAttribute("new_agerange").setValue("100,000,003")

    }

    var AgeRange=Xrm.Page.getAttribute("new_agerange").getValue();

    }

  • Verified answer
    Community Member Profile Picture
    on at

    Use below code:

    function SetAgeRange()
    {
    var Age= Xrm.Page.getAttribute("new_age").getValue ();
    if (Age>=18 && Age<=25)
    {
    Xrm.Page.getAttribute("new_agerange").setValue("100000000")
    }
    else if ( Age>=26 && Age<= 35)
    {
    Xrm.Page.getAttribute("new_agerange").setValue("100000001")
    }
    else if (Age>=36 && Age<=55)
    {
    Xrm.Page.getAttribute("new_agerange").setValue("100000002")
    }
    else if (Age>=56 && Age<= 65)
    {
    Xrm.Page.getAttribute("new_agerange").setValue("100000003")
    }
    var AgeRange=Xrm.Page.getAttribute("new_agerange").getValue();
    }


  • Nithya Gopinath Profile Picture
    17,078 on at

    Hi Iman,

    This is the duplicate of the code which I mentioned in my second post.

  • Verified answer
    Alagunellaikumar Profile Picture
    6,212 on at

    HI

    Remove the comma in your code while set the value

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans