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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

(0) ShareShare
ReportReport
Posted on by 27

Hi All,

Did some searching and didn't really find an answer to this. To start with, here's the code I'm executing as a JS webresource onLoad of the and onChange of the customerid field on the Order entity.

Xrm.WebApi.retrieveRecord("account", <shipToCustomerGUID>, "?$expand=myLookupField($select=accountid,name)").then(
function success(result) {
if (result != null) {
if (verboseLogs) alert("Web API Called Successfully");
var myLookup = result.myLookupField;
if (verboseLogs) alert("myLookup: " + myLookup);
var myLookupName = result.myLookupField.name;
if (verboseLogs) alert("myLookupName:" + myLookupName);
var myLookupId = result.myLookupField.accountid;
if (verboseLogs) alert("myLookupId: " + myLookupId);
if (verboseLogs) alert("Setting to myLookup");
var myLookupArray = new Array();
myLookupArray[0] = new Object();
myLookupArray[0].id = myLookupId;
myLookupArray[0].name = myLookupName;
myLookupArray[0].entityType = "account";
Xrm.Page.getAttribute("customerid").setValue(myLookupArray);

}
else {
if (verboseLogs) alert("Clearing customerid");
Xrm.Page.getAttribute("customerid").setValue(myLookupArray);.setValue(null) && alert("Please choose a valid account");
}
},
function (error) {
if (verboseLogs) alert(error.message);
});

Now, when I pass a GUID of an account record that has myLookupField populated, everything goes swimmingly. I can turn on verbose logs and watch the whole thing unfold line by line.

However, if myLookupField does not contain data, it still appears as if the API is successfully called, but I don't get any alerts after if (verboseLogs) alert("myLookup: " + myLookup). It's almost like the function dies after that point. None of my remaining variables alert (at all), it doesn't try to build the array, nothing. I even have the whole thing nested in a try/catch, and it doesn't trigger the catch. Can anyone offer any insight as to how this is behaving? And how I might accomplishthe following:

If myLookupField (on the ShipToCustomer account record) contains a value, build an array from that and push it into customerid

if myLookupField is blank, clear out customerid on the order and display an alert

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    This is because when there is no values, the statement "result.myLookupField.name" will throw an error because result.myLookupField would be null. I have tried and changed your code as below. Please try this,

    ===========

    function GetParentAccount() {

       var verboseLogs = true;

       var account = Xrm.Page.getAttribute("new_account").getValue();

       if (account != null && account[0] != null) {

           var accountId = account[0].id.slice(1, -1);

           Xrm.WebApi.retrieveRecord("account", accountId, "?$expand=parentaccountid($select=accountid,name)").then(

               function success(result) {

                   if (result != null) {

                       if (verboseLogs) alert("Web API Called Successfully");

                       var myLookup = result.parentaccountid;

                       if (myLookup != null) {

                           if (verboseLogs) alert("myLookup: " + myLookup);

                           var myLookupName = result.parentaccountid.name;

                           if (verboseLogs) alert("myLookupName:" + myLookupName);

                           var myLookupId = result.parentaccountid.accountid;

                           if (verboseLogs) alert("myLookupId: " + myLookupId);

                           if (verboseLogs) alert("Setting to myLookup");

                           var myLookupArray = new Array();

                           myLookupArray[0] = new Object();

                           myLookupArray[0].id = myLookupId;

                           myLookupArray[0].name = myLookupName;

                           myLookupArray[0].entityType = "account";

                           Xrm.Page.getAttribute("parentaccountid").setValue(myLookupArray);

                       }

                       else {

                           Xrm.Page.getAttribute("parentaccountid").setValue(null);

                       }

                   }

                   else {

                       if (verboseLogs) alert("Clearing customerid");

                       Xrm.Page.getAttribute("parentaccountid").setValue(null);

                       alert("Please choose a valid account");

                   }

               },

               function (error) {

                   if (verboseLogs) alert(error.message);

               });

       }

    }

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

  • Mr. Dynamics Profile Picture
    27 on at

    Ravi,

    I've updated my code with your suggestions and I'm still observing the same behavior. The script still alerts "Web API Called Successfully", but nothing happens after that. No further alerts, no data is updated. Any thoughts?

  • necsa Profile Picture
    3,455 on at

    Hi,

    please just to clearify. myLookupField is on the Account entity or Order?

  • Mr. Dynamics Profile Picture
    27 on at

    Hi Necdet,

    myLookupField is on the Account entity. The script runs on the Order, gets the GUID of Account A (selected in a lookup on the Order), and then use the expand to grab the accountid and name from Account B, which is selected in myLookupField on Account A. The issue occurs when myLookupField is blank, but I have no way of testing that before running the API call, do I?

  • necsa Profile Picture
    3,455 on at

    Hi,

    just to be sure. You have 'customerid' fields as Potential Customer on the Order. You retrieve the Account over this field and check myLookupField. If it has a value you want to replace same value on the Potential Customer ('customerid') field and if it is blank you want have alert that user must select an account for myLookupField and WebAPI fill the 'customerid'

    Am I correct understand?

  • Mr. Dynamics Profile Picture
    27 on at

    You're very close. I want to retrieve the account in a "ship to" field, and based on the contents of the myLookupField on that Account, I want to update the customerid field or clear customerid out and alert. Like this:

    WebAPIIssue.png

  • Suggested answer
    Aditya Profile Picture
    260 on at

    You should put a validation after this step var myLookup = result.myLookupField;

    if(myLookup !=null)

    {

    if (verboseLogs) alert("myLookup: " + myLookup);
    var myLookupName = result.myLookupField.name;
    if (verboseLogs) alert("myLookupName:" + myLookupName);
    var myLookupId = result.myLookupField.accountid;
    if (verboseLogs) alert("myLookupId: " + myLookupId);
    if (verboseLogs) alert("Setting to myLookup");
    var myLookupArray = new Array();
    myLookupArray[0] = new Object();
    myLookupArray[0].id = myLookupId;
    myLookupArray[0].name = myLookupName;
    myLookupArray[0].entityType = "account";
    Xrm.Page.getAttribute("customerid").setValue(myLookupArray);

    }

  • necsa Profile Picture
    3,455 on at

    Hi,

    Please can you try following code. (match your lookup field name)

    function GetParentAccount(){
        var account=Xrm.Page.getAttribute("customerid").getValue();
        if(account != null && account[0] != null){
            Xrm.Page.getAttribute("customerid").setValue(null);
            var accountId=account[0].id.slice(1,-1);
            var filter="?$select=_nec_shiptocustomer_value";
        
            Xrm.WebApi.retrieveRecord("account",accountId,filter).then(    
            function success(result){
                
                
                alert("Web API Called Succesfully");
                var myLookup=result._nec_shiptocustomer_value;
                if(myLookup !=null){
                    
                    alert("myLookup: " + myLookup);
                    var myLookupName=result['_nec_shiptocustomer_value@OData.Community.Display.V1.FormattedValue'];
                    
                    alert("myLookupName: " + myLookupName);
                    var myLookupId= result._nec_shiptocustomer_value;
                    
                    alert("myLookupId : " + myLookupId);
                    
                    alert("Setting to myLookup");
                    var myLookupArray=new Array();
                    myLookupArray[0]= new Object();
                    myLookupArray[0].id=myLookupId;
                    myLookupArray[0].name=myLookupName;
                    myLookupArray[0].entityType="account";
                    
                    Xrm.Page.getAttribute("customerid").setValue(myLookupArray);
                }
                  else{
                alert("Clearing customerid");
                Xrm.Page.getAttribute("customerid").setValue(null);
                alert("Please choose a valid account");
            }

        },
        function(error){
            
            alert(error.message);
        });
        }

    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans