Wednesday, March 4, 2020

How to validate mobile with country code in Dynamic CRM

One of my clients requested to validate mobile phone number along with country code.

In India mobile number format should be like  +91 1234567890 where +91 is a country code and actual contact number is of 10 digits.

In general, for other country as well we should have minimum of 10-digit validations (citation needed)

Here are some testing screen shots for you to understand and visualize.
Images are self-explanatory.

For formatting a number in US format refer here









JavaScript code to validate for such a requirement.

updatePhoneNumberFormat: function (executionContext, phoneFieldName, phoneLabel)
{
var phoneCleaned;
var phoneNumber = base.attr.getValue(executionContext, phoneFieldName);
if (phoneNumber != null) {
    if (phoneNumber.substr(0, 1) != '+') {
        base.attr.getControl(executionContext, phoneFieldName).clearNotification();
        base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' should be formatted with country code +XX');
        return;
    }
    else {
        if (phoneNumber.substr(0, 3) == '+91') {
            phoneCleaned = phoneNumber.replace(/[^0-9]/g, "");
            if (phoneCleaned.length != 12) {
                base.attr.getControl(executionContext, phoneFieldName).clearNotification();
                base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' for India country code should be of 10 digit excluding +91');
                return;
            }
        }
        else {
            phoneCleaned = phoneNumber.replace(/[^0-9]/g, "");
            if (phoneCleaned.length < 11) {
                base.attr.getControl(executionContext, phoneFieldName).clearNotification();
                base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' seems to be too short');
                return;
            }
        }
    }
    base.attr.getControl(executionContext, phoneFieldName).clearNotification();
}
}

I hope you find this as useful.

You can reach out to me for such validation for your dynamic CRM Project.


Thanks.
Vipin Jaiswal
vipinjaiswal12@gmail.com 







Tuesday, March 3, 2020

Generic way to Refresh Rollup field in Dynamic CRM

Opportunity Roll-up fields




Opportunity Products Grid




Roll-up fields Definition





Generic method to re-calculate and refresh the Rollup field.

refreshRoleupField: function (executionContext, entityName, entityId, rollup_fieldName)
{
 debugger;
 var formContext = executionContext.getFormContext();           
 var clientUrl = formContext.context.getClientUrl();

 // Method Calling and defining parameter
 var rollupAPIMethod = "/api/data/v9.1/CalculateRollupField(Target=@tid,FieldName=@fn)";

 // Passing Parameter Values
 rollupAPIMethod += "?@tid={'@odata.id':'" + entityName + "(" + entityId + ")'}&@fn='" +   rollup_fieldName + "'";

 var req = new XMLHttpRequest();
 req.open("GET", clientUrl + rollupAPIMethod, false);
 req.onreadystatechange = function ()
 {
    if (this.readyState === 4) 
    {
        req.onreadystatechange = null;
        if (this.status === 200) 
        {
          console.log("Field Recalculated successfully");                       
        }
    }
 };

 req.send();           
 formContext.data.entity.save();
}

Syntax for calling the method

onLoad: function (executionContext)
{
    var entityId = formContext.data.entity.getId().replace('{''').replace('}''');
    opty.Functions.refreshRoleupField(executionContext, 'opportunities', entityId, 'new_totalunitcost');
    opty.Functions.refreshRoleupField(executionContext, 'opportunities', entityId, 'new_sumextendedcost');

}

I hope you find this useful.

Thanks.