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 !!

3 thoughts on “Setting Default Unit in Product form using Javascript

  1. 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?

    Reply
    1. mahenderpal Post author

      Hi, it seems there is another code running to disable that field, you should not face this issue without that.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *