Hi i trying to retrieve data from lookup field and populate another fields, buts my code do nothing. Any idea? Thanks
function RequestContact(executionContext, campoLookUp)
{
var formContext = executionContext ? executionContext.getFormContext() : Xrm.Page; // get formContext
var datoLookup = formContext.getAttribute(campoLookUp).getValue();
if(datoLookup != null) { var fullnombre = datoLookup[0].name; } else {return;}
var serverUrl = formContext.context.getClientUrl();
var oDataSelect = serverUrl + "/api/data/v8.2/contacts?$select=_mme_ciudadmunicipio_value,_mme_departamento_value,mme_numerodeidentificacion,_mme_pais_value,mme_tipodedocumento,mobilephone,telephone1&$filter=fullname eq '"+fullnombre+"'";
var req = new XMLHttpRequest();
req.open("GET", serverUrl, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function()
{
if (this.readyState === 4)
{
req.onreadystatechange = null;
if (this.status === 200)
{
var results = JSON.parse(req.responseText).d;
for (var i = 0; i < results.value.length; i++)
{
var pais_value = results.value[i]["_mme_pais_value"];
var pais_value_formatted = results.value[i]["_mme_pais_value@OData.Community.Display.V1.FormattedValue"];
var pais_value_lookuplogicalname = results.value[i]["_mme_pais_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
var departamento_value = results.value[i]["_mme_departamento_value"];
var departamento_value_formatted = results.value[i]["_mme_departamento_value@OData.Community.Display.V1.FormattedValue"];
var departamento_value_lookuplogicalname = results.value[i]["_mme_departamento_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
var ciudadmunicipio_value = results.value[i]["_mme_ciudadmunicipio_value"];
var ciudadmunicipio_value_formatted = results.value[i]["_mme_ciudadmunicipio_value@OData.Community.Display.V1.FormattedValue"];
var ciudadmunicipio_value_lookuplogicalname = results.value[i]["_mme_ciudadmunicipio_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
var tipodedocumento = results.value[i]["mme_tipodedocumento"];
var tipodedocumento_formatted = results.value[i]["mme_tipodedocumento@OData.Community.Display.V1.FormattedValue"];
var numerodeidentificacion = results.value[i]["mme_numerodeidentificacion"];
var numerodeidentificacion_formatted = results.value[i]["mme_numerodeidentificacion@OData.Community.Display.V1.FormattedValue"];
var mobilephone = results.value[i]["mobilephone"];
var telephone1 = results.value[i]["telephone1"];
formContext.getAttribute("mme_numerodeidentificacion").setValue(numerodeidentificacion_formatted);
alert(formContext.getAttribute("mme_numerodeidentificacion").getValue());
formContext.getAttribute("mobilephone").setValue(mobilephone);
formContext.getAttribute("telephone1").setValue(telephone1);
formContext.getAttribute("mme_pais").setValue(pais_value_formatted);
formContext.getAttribute("mme_departamento").setValue(departamento_value_formatted);
formContext.getAttribute("mme_ciudadmunicipio").setValue(ciudadmunicipio_value_formatted);
}
} else
{
formContext.ui.setFormNotification(this.statusText, "WARNING", "2");
}
}
};
req.send();
}