In the current dynamics CRM organization, we have 5 different business unit ( each BU is segregated based on the region and is responsible for its own business). Inside sales entity, we have an optionset field to specify the type of product that our company is trying to sell. Since our business is being operated in 5 different regions, based on each region, we try to sell various products. The total number of options we have as part of the product optionset field are around 80.
Our business wanted us to customize this optionset field so that we display only respective products to logged in users based on the BU they belong to. For example, if the logged in user belong to a BU of united state region, then under the product optionset field, they should be able to see only 15 products that are available in USA region. Similarly, if logged in user is from canada, he should be able to see only 12 products that we sell in canada.
The high level business requirement is based on the BU to which logged in user belong to, we need to show/hide respective options in product field. This requirement can easily be met using java script written on form on-load trigger and the requirement can be partitioned into 2 parts: First is get the user GUID and make WebAPI call to CRM server to retrieve the GUID of business unit to which he belong to, second is based on the BU GUID hide options under product field.
The JS code is:
function hideoptionset()
{
debugger;
var ownerid = new Array();
ownerid = Xrm.Page.getAttribute("ownerid").getValue();
if (ownerid != null && ownerid.length > 0) {
var id = ownerid[0].id;
var odataSelect = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId&$filter=SystemUserId eq guid'" + id + "'";
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", odataSelect, false);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () {
if (retrieveReq.readyState == 4) {
if (retrieveReq.status == 200) {
var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
var retrievedValue = retrieved.results[0].BusinessUnitId;
var id = retrievedValue.Id;
var name = retrievedValue.Name;
//alert(id + " " + name);
}
}
};
retrieveReq.send();
}
if(id == Business unit1 GUID)
{
Xrm.Page.getControl("caseorigincode").removeOption(1); //code to hide a particular option in product field
Xrm.Page.getControl("caseorigincode").removeOption(3986);
.
.
}
else if(id == Business unit2 GUID)
{
Xrm.Page.getControl("caseorigincode").removeOption(43);
Xrm.Page.getControl("caseorigincode").removeOption(6106);
.
.
}
}
USING SYSTEM CONFIGURATION ENTITY
This is a cleaner way of achieving our business requirement where we don’t hard-code the optionset values & label in our code. Instead we store all the changes in a configuration entity and we retrieve them by making WebAPI calls in our JS. The system configuration entity has 2 fields: Name field (single line text) where we input appropriate name, Value field where we input the actual optionset metadata.
You can enter the respective options metadata in following format:
Seller|1,Media|2,Linkedin|3,Other|10
The optionset label is separated by its value using ‘|’ and each optionset is separated by ‘,’. After we make WebAPI call to CRM server, we get an object optionSetResults from which we retrieve the value field of system configuration entity:
new_value = optionSetResults.value[i][“contoso_value”]
var _results = Xrm.Page.getControl(“leadsourcecode”); // leadsourcecode is the schema name of optionset field
_results.clearOptions(); //clear all options
var newOptionSetPair = new_value.split(',');
for (var i = 0; i < newOptionSetPair.length; i++) {
var singleOptionSetPair = newOptionSetPair[i].split('|');
var singleOptionSetNumber = parseInt(singleOptionSetPair[1]);
_results.addOption({ text: singleOptionSetPair[0], value: singleOptionSetNumber });
}

Like
Report
*This post is locked for comments