Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Verified answer
    necsa Profile Picture
    necsa 3,455 on at
    RE: JavaScript Web Resource that sets field to "Required" is not working -- Need help debugging

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

    }

    }

  • Anton Kazlouski Profile Picture
    Anton Kazlouski 140 on at
    RE: JavaScript Web Resource that sets field to "Required" is not working -- Need help debugging

    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.

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: JavaScript Web Resource that sets field to "Required" is not working -- Need help debugging

    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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript Web Resource that sets field to "Required" is not working -- Need help debugging

    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

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans