Search This Blog

Sunday, December 20, 2015

Dynamically add and remove value in the CRM optionset.

Dynamically add and remove value in the CRM optionset

If you have any doubt in the post please post comments. I will try to solve your problem.

Option set control is used for populating a group of static values in CRM. In option set there are two types.
  • Local Option set
  • Global Option set
we have to display some(dynamically add or remove) value in the option set based on the business unit.

In our example, we have two business unit("TAN","TAZ"), one business unit has different timezone and other business unit has different time zone. So  based on login user business unit we have to show/display some timezone value in the option set. In the timezone option set there 6 time zones is added by default.

//get the ownerid
var ownerid = Xrm.Page.getAttribute("ownerid").getValue();
//Get the business unit name based on user id
SDK.REST.retrieveRecord(
    ownerid,
    "SystemUser",
    null, null,
    function(objResult) {
        populateTimezone(objResult.BusinessUnitId);
    },
    errorHandler
);
//Populate timezone option set based on business unit.
function populateTimezone(dBusinessUnitId)
SDK.REST.retrieveRecord(
    dBusinessUnitId,
    "BusinessUnit",
    null, null,
    function(objBusinessUnit) {

        //Get all the value from option set.
        var arrTimeZone = Xrm.Page.getAttribute('new_timezone').getOptions();
        var objTimeZoneInstance = Xrm.Page.ui.controls.get('new_timezone');
        if (objBusinessUnit.Name == "TAN") {
            objTimeZoneInstance.clearOptions();
            objTimeZoneInstance.addOption(arrTimeZone[0]);
            objTimeZoneInstance.addOption(arrTimeZone[2]);
            objTimeZoneInstance.addOption(arrTimeZone[3]);
        } else {
            objTimeZoneInstance.clearOptions();
            objTimeZoneInstance.addOption(arrTimeZone[1]);
            objTimeZoneInstance.addOption(arrTimeZone[4]);
            objTimeZoneInstance.addOption(arrTimeZone[5]);
        }
    },
    errorHandler,
    function() {
        //OnComplete handler
    }
);
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.