Skip to main content

Notifications

Announcements

No record found.

Customer Service forum

Prepopulate from field with Contact Name based on Mobile Number

Posted on by 65

Hi guys

So I have seen similar threads but couldn't quite get this right as I think my requirement is slightly different. Basically I have an entity WhatsApp on this entity I have a field "icon_mobile" i am trying to write a script that searches the contact entity for the mobile number which sits in "icon_mobile" by searching the "mobilephone" field in the contact entity, then prepopulates the "from" field on the whatsapp entity. Here is my crack thus far, script is running but using the debugger there seemed to be an issue with my send();

function contactPrePopulation() {
debugger;
var mobile = Xrm.Page.getAttribute("icon_mobile").getValue();
//var mobileNumber = "27" + mobile.substr(1);


if(mobile === null) {
return;
}

var serverUrl = Xrm.Page.context.getClientUrl();
var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/contact?&select=contactid&$filter=mobilephone eq guid' " + mobile[0].id + "'";

var retriveReq = new XMLHttpRequest();
retriveReq.open("GET", oDataSelect, false);
retriveReq.setRequestHeader("Accept", "application/son");
retriveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retriveReq.onreadystatechange = function() {
GetContactData(this);
};
retriveReq.send();
}

function GetContactData(retriveReq) {
if(retriveReq.readyState == 4) {
if(retriveReq.status == 200) {
var retrieved = JSON.parse(retriveReq.responseText).d;
Xrm.Page.getAttribute("from").setValue(retrieved.results[0].mobile);
}
}
alert("Hello!");
}
function contactPrePopulation() {
debugger;
var mobile = Xrm.Page.getAttribute("icon_mobile").getValue();
//var mobileNumber = "27" + mobile.substr(1);


if(mobile === null) {
return;
}

var serverUrl = Xrm.Page.context.getClientUrl();
var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/contact?&select=contactid&$filter=mobilephone eq guid' " + mobile[0].id + "'";

var retriveReq = new XMLHttpRequest();
retriveReq.open("GET", oDataSelect, false);
retriveReq.setRequestHeader("Accept", "application/son");
retriveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retriveReq.onreadystatechange = function() {
GetContactData(this);
};
retriveReq.send();
}

function GetContactData(retriveReq) {
if(retriveReq.readyState == 4) {
if(retriveReq.status == 200) {
var retrieved = JSON.parse(retriveReq.responseText).d;
Xrm.Page.getAttribute("from").setValue(retrieved.results[0].mobile);
}
}
alert("Hello!");
}
Categories:
  • Sean Kelly 1994 Profile Picture
    Sean Kelly 1994 65 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    Great thanks, I will give this a try

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    The code after the statement req.send() will move to next statement where you are using contacted. By this time, the contacted will not be defined that the code to retrieve will asynchronously.

    refer below sample code generated from the same tool. Notice that the result is an array

    ===========================

    var mobile = Xrm.Page.getAttribute("icon_mobile").getValue();

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts?$filter=mobilephone eq '" + mobile + "'", 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=\"*\",odata.maxpagesize=1");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var contactid = results.value[i]["contactid"];

                    alert(contactid );

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    ==============

    Hope this helps.

  • Sean Kelly 1994 Profile Picture
    Sean Kelly 1994 65 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    Hi Ravi,

    Is this not correct?

    var contactid = result["contactid"];

    line 19 - and yes WhatsApp is an activity entity and the from field i am trying to populate is on the whatsapp form and then trying to retrive the contact from the contact entity using the mobile number

    here is updated code

    function contactPrePopulation() {

       debugger;

       var mobile = Xrm.Page.getAttribute("icon_mobile").getValue();

       if(mobile === null) {

           return;

       }

       var req = new XMLHttpRequest();

       req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/contacts()?$select=contactid&$filter=mobilephone eq '" + mobile + "'", 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 result = JSON.parse(this.response);

                   var contactid = result["contactid"];

                   setName();

               } else {

                   Xrm.Utility.alertDialog(this.statusText);

               }

       }

       function setName() {

           Xrm.Page.getAttribute("from").setValue(contactid);

       }

       }

       req.send();

       alert("hello", contactid, " and ", mobile);

    };

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    Is this your entity WhatsApp is an activity? And this field "From" is the activity from field?

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    The error message is self explainatory. Where is contactid variable defined?

  • Sean Kelly 1994 Profile Picture
    Sean Kelly 1994 65 on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    Hi Andrew, thanks for getting in touch. I have made use of the solution thanks very much, so i am now sitting with the below, but it keeps giving me the error "contactid not defined at contactPrepopulation"

    function contactPrePopulation() {

       debugger;

       var mobile = Xrm.Page.getAttribute("icon_mobile").getValue();

       if(mobile === null) {

           return;

       }

       var req = new XMLHttpRequest();

       req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/contacts()?$select=contactid&$filter=mobilephone", 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 result = JSON.parse(this.response);

                   var contactid = result["contactid"];

                   setName();

               } else {

                   Xrm.Utility.alertDialog(this.statusText);

               }

       }

       function setName() {

           Xrm.Page.getAttribute("from").setValue(contactid);

       }

       }

       req.send();

       //Test

       alert("hello", contactid, " and ", mobile);

    };

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Prepopulate from field with Contact Name based on Mobile Number

    Hello,

    Make your life easier - prepare your scripts using CrmRestBuilder - github.com/.../2.6.0.0

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... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans