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)

JavaScript Web Resource that sets field to "Required" is not working -- Need help debugging

(0) ShareShare
ReportReport
Posted on by

I have created a web resource that is supposed to set a field to "required" when the form is opened.  I have added the web resource to the OnLoad section of the form.  However, it is not setting the field to required. (I cannot simply use a business rule to do this because the Manufacturer field will then become required in QuoteWerks as well, which is where many of our Opportunity records are created.)

Below is my code:

function SetManufacturerToRequired() {

    var value = Xrm.Page.getAttribute("new_opportunityscope");
    if (value != null) {
        if (value == "Product") {
            Xrm.Page.getAttribute("new_manufacturer").setRequiredLevel("required");
        } else {
            Xrm.Page.getAttribute("new_manufacturer").setRequiredLevel("none");
        }
    }
else {
        Xrm.Page.getAttribute("new_manufacturer").setRequiredLevel("none");
    }
}

Any ideas why its not working and how I can fix it?

Thanks!

*This post is locked for comments

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

    Hi,

    var value = Xrm.Page.getAttribute("new_opportunityscope");

    I think you have to do Xrm.Page.getAttribute("new_opportunityscope").getValue() to actually get the value. With only getAttribute() you will only retrieve the object so the clause if (value == "Product") will always be false, probably that's why your required level is always as "none".

    Best regards

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi There,

    Have you tried to set required level directly from the console ?

    Simply try to debug on console form (Press F12 on CRM form) to set required level.

    It supposed to work

  • Anton Kazlouski Profile Picture
    140 on at

    First of all, Xrm.Page was deprecated. You have to use formContext in order to work with attributes correctly.

    In addition, getAttribute() returns attribute object for you, not a value. You should add getAttribute('field').getValue() in order to get field value.

  • Verified answer
    necsa Profile Picture
    3,455 on at

    Hi,

    Please try following code:

    function SetManufacturerToRequired(){

    var value = Xrm.Page.getAttribute("new_opportunityscope").getValue();

    if(value != null && value="Product"){

    Xrm.Page.getAttribute("new_manufacturer").SetRequiredLevel("required");

    }else{

    Xrm.Page.getAttribute("new_manufacturer").SetRequiredLevel("none");

    }

    }

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