Search code examples
javascriptdynamics-crmcrm

On Dynamics Quick Create Form make 'Email' field mandatory using JavaScript?


Hope all is well everyone!

Im practicing CRM Dynamics development, on the 'Contact' quick create form, I'm trying to achieve making the emailaddress1 field mandatory/setrequired only.

Currently my function below is making all of the form fields mandatory on the 'Contact' quick create form which I don't want it to do, I just want to make 'emailaddress1' field setrequired only, any suggestions?

function SetRequiredLevel()    
{
var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes) {
attributes[i].setRequiredLevel("required");
}
}

//Xrm.Page.getAttribute("emailaddress1").getValue();

Screen shot is off all the form fields mandatory on the 'Contact' quick create


Solution

  • XRM.Page is deprecated. You should use this form:

    function displayName(executionContext)
    {
        var formContext = executionContext.getFormContext(); // get formContext
        // use formContext instead of Xrm.Page  
        formContext.getAttribute("emailaddress1").setRequiredLevel("required");  
    }