How to set CRM javascript value to null?

Viewed 956

I have the following JavaScript code in a Dynmaics library, however the code does not set the value to null in the else condition any ideas to why? I believe my code is correct.

Please advise. My null setting isnt working I also tried removeAttribute();it does not work

    function validateSortCode(executionContext) {
        
        var formContext = executionContext.getFormContext();
        
        var sortcode = formContext.getAttribute("sortcodee").getValue();
        
        var errorId = "error";
        
        if(sortcode != "")
        
        {
            var sortcoderegex = /^(\d){2}-(\d){2}-(\d){2}$/;
            if(sortcoderegex.test(sortcode) == false)
            {
                formContext.ui.setFormNotification("Please ensure correct format of Sort Code i.e. 12-34-56", "ERROR", errorId);
            }
            else
            {
    
    formContext.getAttribute("sortcodee").setValue(null);
 //formContext.getAttribute("sortcodee").removeAttribute("sortcodee");

            }
       }
    }
1 Answers

You need to set the value after your notification of the error to null.

Try this.

function validateSortCode(executionContext) {
    
    var formContext = executionContext.getFormContext();
    
    var sortcode = formContext.getAttribute("sortcodee").getValue();
    
    var errorId = "error";
    
    if(sortcode != "")
    
    {
        var sortcoderegex = /^(\d){2}-(\d){2}-(\d){2}$/;
        if(sortcoderegex.test(sortcode) == false)
        {
            formContext.ui.setFormNotification("Please ensure correct format of Sort Code i.e. 12-34-56", "ERROR", errorId);

formContext.getAttribute("sortcodee").setValue("");

        }
        else
        {

formContext.ui.clearFormNotification(errorId);

        }
   }
}

The above should do the trick

Related