Setting Default Unit in Product form using Javascript


I have seen this question in CRM development forum, where user asked how to set ‘Default Unit’ using javascript, so I wrote below function to do the same

function SetDefaultUnit()

{

if(Xrm.Page.ui.getFormType() == 1) //Check form type

{

var number = 1;
var filter = “/UoMScheduleSet?$select=Name,UoMScheduleId&$filter=Name eq ‘Default Unit’”;   //Make sure you have created ‘Default Unit’ record
RetrieveUnit(filter);
}

}
function GetODataPath() {

return Xrm.Page.context.getServerUrl() + “/xrmservices/2011/organizationdata.svc”;
}
function RetrieveUnit(filter) {

var retrieveRecordsReq = new XMLHttpRequest();
retrieveRecordsReq.open(“GET”, GetODataPath() + filter, false);
retrieveRecordsReq.setRequestHeader(“Accept”, “application/json”);
retrieveRecordsReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
var temp= retrieveRecordsReq.send();

//check status
if (retrieveRecordsReq.readyState == 4 && retrieveRecordsReq.status == 200) {
var retrievedRecords = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
var Result = retrievedRecords.results[0];

//Set lookup
var lookup = new Object();
var lookupValue = new Array();
lookup.id = Result.UoMScheduleId;
lookup.entityType = “uomschedule”;
lookup.name = Result.Name;
lookupValue[0] = lookup;
Xrm.Page.getAttribute(“defaultuomscheduleid”).setValue(lookupValue);
}
}

Just create a webresource and attach that webresource to product form and call SetDefaultUnit function onload of product form.

Hope it will help somebody !!

Advertisements

3 Comments

Filed under MS CRM 2011

3 responses to “Setting Default Unit in Product form using Javascript

  1. Pavan

    Good One It helped me…

  2. Capa

    Thanks a lot. But there’s a problem. When the Unit group was set to ‘Default Unit’ , the Defaut Unit field was disabled at the same time, we have to switch over to another unit group to make it enabled… do you know how to solve this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s