I have this request when conducting a user requirement, my client said that When a new Opportunity Form is opened, the default price list will be automatically fill in the 'Price List' field.
This post I will give you a sample design and also the code to to this, and it should be working both in CRM 2011 or CRM 2013.
This is what user wants :
Without having effort to choosing default price list, they want to it can be filled in automatically.
There are many ways to do it, for example using the hardcode, by filling the lookup value to Price List with the hardcode Guid retrieved from 'Default Price List' record ID, but it will be a problem if they want to change the Default Price List or if you create another organization which is to the Guid of that can be different.
You should have knowledge about Javascript Form Scripting and oData, but don't worry, I will give you the way.
So my Design is to create a Price List record to be set as Default Price List (dynamically), then only keep one default price list for each Organization.
Steps :
1. Customize the Price List entity, create new field radio button and then place it to the Opportunity form.
NB : I give field name :
"new_setasdefault"
2. Attach a Form OnSave function to Price List with JavaScript :
var isDuplicate = 0;
function PreventMultipleDoubleDefaultPriceList(context)
{
//if field set as default == True
if(Xrm.Page.getAttribute("new_setasdefault").getValue() == 1)
{
//check duplicate default Price List
checkDuplicateDefaultPriceListId();
if (isDuplicate == 1)
{
context.getEventArgs().preventDefault();
}
}
}
function checkDuplicateDefaultPriceListId()
{
var oDataOrgURL = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc");
var currentId = Xrm.Page.data.entity.getId();
var currentIdCondition = '';
if (currentId != null)
{
currentIdCondition = ' and PriceLevelId ne guid\'' + currentId + '\'';
}
//add filter
var oDataURL = oDataOrgURL + "/PriceLevelSet?&$filter=(new_SetAsDefault eq true)" + currentIdCondition ;
//call
var defaultPriceListRequest = new XMLHttpRequest();
defaultPriceListRequest.open("GET", oDataURL , false);
defaultPriceListRequest.setRequestHeader("Accept", "application/json");
defaultPriceListRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
defaultPriceListRequest.onreadystatechange = function () {
getDefaultPriceListIdCallBack(this);
};
defaultPriceListRequest.send(null);
}
function getDefaultPriceListIdCallBack(retrievedDefaultPriceList)
{
if (retrievedDefaultPriceList.readyState == 4 /* complete */)
{
if (retrievedDefaultPriceList.status == 200)
{
var retrievedRecords = eval('(' + retrievedDefaultPriceList.responseText + ')').d;
if(retrievedRecords.results.length > 0)
{
var defaultPriceList = retrievedRecords.results[0];
alert("There is already Price List set as default : '" + defaultPriceList.Name + "', you can only have one Default Price List at the same time.");
isDuplicate = 1;
}
}
else {}
//alert("Error occured");
}
}
function SetLookup(fieldName, idValue, textValue, typeValue)
{
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;
Xrm.Page.getAttribute(fieldName).setValue(value);
}
Then call your function for OnSave event handler.
This function is purposely used to prevent more than one default price list.
3. Customize the Opportunity form by attaching this Javascript onLoad :
function fillDefaultPriceListOnLoad()
{
//get form type
var formType = Xrm.Page.ui.getFormType();
if(formType == 1) //if on create
{
getDefaultPriceListId();
}
}
function getDefaultPriceListId()
{
var oDataOrgURL = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc");
//add filter
var oDataURL = oDataOrgURL + "/PriceLevelSet?$filter=new_SetAsDefault eq true";
//call
var defaultPriceListRequest = new XMLHttpRequest();
defaultPriceListRequest.open("GET", oDataURL, false);
defaultPriceListRequest.setRequestHeader("Accept", "application/json");
defaultPriceListRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
defaultPriceListRequest.onreadystatechange = function () {
getDefaultPriceListIdCallBack(this);
};
defaultPriceListRequest.send(null);
}
function getDefaultPriceListIdCallBack(retrievedDefaultPriceList)
{
if (retrievedDefaultPriceList.readyState == 4 /* complete */)
{
if (retrievedDefaultPriceList.status == 200)
{
var retrievedRecords = eval('(' + retrievedDefaultPriceList.responseText + ')').d; ;
if(retrievedRecords.results.length > 0)
{
var defaultPriceList = retrievedRecords.results[0];
SetLookup("pricelevelid", defaultPriceList.PriceLevelId, defaultPriceList.Name,
"pricelevel");
}
}
else
alert("Error occured");
}
}
function SetLookup(fieldName, idValue, textValue, typeValue)
{
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;
Xrm.Page.getAttribute(fieldName).setValue(value);
}
Then call the function on Opportunity Form onLoad event handler.
Then we can achieve what user wants without any hardcode, but using parameter and configuration.
This is the result in CRM 2013
I can give you the solution if you want.
Hope it helps!
Thanks.