Based on a post that I found here, I wrote the below code to automatically look at the product fields and display them on the opportunity product form.
everything is working fine, except for the product type field which doesn't give me the correct output for some reason.
The product type field and the license type field are both option set fields with three options each ( identical fields).
If I put one product that has a product type A, then it displays it on the opportunity product as B
and so on.
This worked fine when I was working on one option but since we would like to have more options ( I edited the fields to have a third option), it stopped being able to give me the correct output.
I would really appreciate if someone could tell me what I am doing wrong here.
//retrieve data based on primary entity id
function retrieveProductFamily() {
//read lookup value
if (Xrm.Page.getAttribute("productid").getValue() != null && Xrm.Page.getAttribute("productid").getValue()[0].id != null) {
var getProduct = Xrm.Page.getAttribute("productid").getValue()[0].id;
getProduct = getProduct.replace(/[{}]/g, "");
//pass entity, fields, we can use expand to get related entity fields
Xrm.WebApi.retrieveRecord("product", getProduct, "?$select=new_testfamily,new_licensetype,new_licenseperiod,new_descriptionenglish,new_descriptionfrench,new_descriptiongerman,new_descriptionitalian,new_descriptionspanish,new_descriptionchinese,new_descriptionportuguese").then(
function success(result) {
if (result != null) {
//set text field
if (result.new_testfamily != null)
Xrm.Page.getAttribute("new_productfamily").setValue(result.new_testfamily);
if (result.new_licensetype != null)
Xrm.Page.getAttribute("new_producttype").setValue(result.new_licensetype);
if(Xrm.Page.getAttribute("new_licenseperiod").getValue()==null)
if (result.new_licenseperiod != null)
Xrm.Page.getAttribute("new_licenseperiod").setValue(result.new_licenseperiod);
if (result.new_descriptionenglish != null)
Xrm.Page.getAttribute("new_descriptionen").setValue(result.new_descriptionenglish);
if (result.new_descriptionfrench != null)
Xrm.Page.getAttribute("new_descriptionfr").setValue(result.new_descriptionfrench);
if (result.new_descriptiongerman != null)
Xrm.Page.getAttribute("new_descriptionde").setValue(result.new_descriptiongerman);
if (result.new_descriptionitalian != null)
Xrm.Page.getAttribute("new_descriptionit").setValue(result.new_descriptionitalian);
if (result.new_descriptionspanish != null)
Xrm.Page.getAttribute("new_descriptiones").setValue(result.new_descriptionspanish);
if (result.new_descriptionchinese != null)
Xrm.Page.getAttribute("new_descriptioncn").setValue(result.new_descriptionchinese);
if (result.new_descriptionportuguese != null)
Xrm.Page.getAttribute("new_descriptionpt").setValue(result.new_descriptionportuguese);
}
},
function(error) {
alert(error.message);
}
);
}
}