web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer Service forum

How create a Contact record from a lookup field automatically?

(0) ShareShare
ReportReport
Posted on by 107

I need create a Contact record when this dont exits into a Dyn CRM 365. But the creation must be automatically. Let me show the situation and my code

evidence.png

This my code

function CreateUpdateFuncionario(executionContext)
{
var formContext = executionContext ? executionContext.getFormContext() : Xrm.Page; // get formContext

var requestedID = formContext.getAttribute("mme_numerodeidentificacion").getValue();

if(requestedID != null)
{
var serverUrl = formContext.context.getClientUrl();
var oDataSelect = serverUrl + "/api/data/v8.2/contacts?$select=mme_numerodeidentificacion, fullname&$filter=mme_numerodeidentificacion eq " + requestedID + ""; //Id exist?
var req = new XMLHttpRequest();

req.open("GET", oDataSelect , 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(this.response);
var exist = results.mme_numerodeidentificacion;
var nombrecompleto = results.fullname;

if(exist != null) { alert("Contact exist---->" + nombrecompleto); } else { alert("Dont exist, must be created"); }

} else
{
formContext.ui.setFormNotification("No se pudo ejecutar la consulta.", "WARNING", "2");
}
}
};
req.send();
}
else
{
var message = "Para crear, actualizar un ciudadano por favor ingrese un número válido de documento. De lo contrario use el usuario NO APORTA para poder guardar."
formContext.ui.setFormNotification(message, "WARNING", "2");
}
}

Categories:
I have the same question (0)
  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: How create a Contact record from a lookup field automatically?

    Hi Shiongo,

    If you fill in an un-exist record in a LookUp field, you will not be able to save the form.

    So I recommend to trigger this JS function at "on change" action of the LookUp filed instead of "On Save".

    WeChat-Screenshot_5F00_20181214104530.png

    According to your code, you just tested if the record existed. I suggest that you could refer to the solution:

    1.If the record not exists, use WebApi to create a new record.

    You could refer to the  sample code bellow from SDK.

    POST [Organization URI]/api/data/v9.0/contacts HTTP/1.1
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    Accept: application/json
    
    {
        "firstname": "Sample",
        "lastname": "Contact",
        "jobtitle": "jobtitle",
        "address1_line1": "address1_line1"
    }

    2.After you create the record, you could catch the record by webapi and set the value to your lookup field.

    You need GUID, Name, and Entity Name of the record which you want to set as a value to lookup field.

    var Lookup= new Array();
    Lookup[0] = new Object();
    Lookup[0].id = “GUID”; // GUID of the record
    Lookup[0].name = “Name”; // Name of the record
    Lookup[0].entityType = “contact”; //Entity name of the record
    Xrm.Page.getAttribute(“FieldName”).setValue(Lookup); // set your record as value to filed.


    Hope it helps.

    Best Regards

    Leo

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 > Customer Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans