Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer Service forum

Populate fields from a lookup field on Dyn CRM 365 - HELP

(0) ShareShare
ReportReport
Posted on by 107

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();
}

Categories:
  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Populate fields from a lookup field on Dyn CRM 365 - HELP

    Great. Can you share the details of your changes and marl that as a verified answer so that it can help other as well?

  • Verified answer
    Shiongo Profile Picture
    107 on at
    RE: Populate fields from a lookup field on Dyn CRM 365 - HELP

    Thanks my friends i do some corrections to my code and works

  • Suggested answer
    Waqar Sohail Profile Picture
    on at
    RE: Populate fields from a lookup field on Dyn CRM 365 - HELP

    Hi Seem like you are selecting wrong field names, please verify field names in select. as some start from _mme and some mme.

    _mme_ciudadmunicipio_value,_mme_departamento_value,mme_numerodeidentificacion,_mme_pais_value,mme_tipodedocumento,mobilephone

    Regards

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Populate fields from a lookup field on Dyn CRM 365 - HELP

    Hi,

    You can refer the below blog on the same topic but it appears that you are already using the correct way (haven't looked into it in detail though). Can you check if you are getting any error in browser console (F12).

    community.dynamics.com/.../get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm

    You can also debug the script and see where exactly it is failing.-

    docs.microsoft.com/.../debug-javascript-code

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,325 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans