Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Writing REST API

Posted on by Microsoft Employee

I'm trying to write two sets of code:

1. Will pull and concatenate the "name" part of the array for two lookup fields

2. The second will pull and concatenate the salutation, first name, last name information from contacts in those lookup fields to build salutation

Below is the code I have so far for part one. When I run it as an OnChange event the function comes back undefined. I've never worked with a REST API.

//---AUTOPOPULATE HOUSEHOLD NAME
function = retrieveHeadofHousehold(){
SDK.REST.retrieveRecord("", "tprnew_household", "tprnew_HeadofHousehold", null, function(result) {
var tprnew_HeadofHousehold = result.tprnew_HeadofHousehold;
}, function(error) {
writeMessage("Head of Household is empty");
}
);
}
function = autopopulate_HouseholdName(){
XRM.Page.getAttribute(tprnew_HeadofHousehold).set.Value(household_name) + " and ";
}

*This post is locked for comments

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Writing REST API

    var contactId = getLookupId("tprnew_contact");

    if (contactId != '')

    {

     // perform your logic here

    }

    // getLookupId function

    function getLookupId(fieldName) {

       var field = Xrm.Page.getAttribute(fieldName);

       if (field != null) {

           var fieldId = field.getValue();

           if (fieldId != null)

               return fieldId[0].id.toString();

           else

               return '';

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing REST API

    That is helpful, how can I tell what the guid is for the contact in the lookup? Do I also need to add the value to the function parentheses like in the example with accountid?

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Writing REST API

    The following line:

    SDK.REST.retrieveRecord("", "tprnew_household", "tprnew_HeadofHousehold", null, function(result)

    is missing the Guid of the record. That is supposed to be the first parameter where you have ""

    See example of usage of SDK.REST.retrieveRecord:

    function retrieveAccount(accountId)

    {

       SDK.REST.retrieveRecord(accountId, "Account", null, null, retrieveAccountCallback, retrieveErrorCallback);

    }

    function retrieveAccountCallback(address)

    {

       var name = address1.Address_Name;

       var line1 = address1.Address_Line1;

       var line2 = address1.Address_Line2;

       var city = address1.Address_City;

       var stateprovince = address1.Address_StateOrProvince;

       var postalcode = address1.Address_PostalCode;

    }

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing REST API

    I don't have a namespace applied in this library since this is the only function of it's kind and I've verified that "retrieveHeadofHousehold" is properly entered into the event handler.

    Other than the .setValue issue Aric pointed out I haven't been able to identify any other syntax errors. All of my brackets and parentheses are accounted for.

    //---AUTOPOPULATE HOUSEHOLD NAME

    function = retrieveHeadofHousehold(){

    SDK.REST.retrieveRecord("", "tprnew_household", "tprnew_HeadofHousehold", null, function(result) {

       var tprnew_HeadofHousehold = result.tprnew_HeadofHousehold;

    }, function(error) {

       writeMessage("Head of Household is empty");

    }

    );

    }

    function = autopopulate_HouseholdName(){

    XRM.Page.getAttribute(tprnew_HeadofHousehold).setValue(household_name) + " and ";

    }

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Writing REST API

    The following line has an error:

    XRM.Page.getAttribute(tprnew_HeadofHousehold).set.Value(household_name) + " and ";

    setValue is one word. You have it as set.Value

    Hope this is the issue.

    Let us know.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Writing REST API

    The undefined error comes when browser is unable to find tge object/ funtion in the loded script.

    And this generaly happens if you have used the function name incorrectly or you have a syntax error in the script.

    Can you check if the name of your funtion and the one coming in the error message is same?

    Do you have a namespace in your javascript liabrary? If yes then you need to include that as well e.g. Abc.retrieveHeadofHousehold

    Can you verody your script for any syntax error? Have seen cases where braces, comma etc were missing causing the old version of file being loaded in the browser and thus throwing undefined error.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing REST API

    I've added the SDK.REST.js to the Form Library for Households. Comes back as undefined when I set the OnChange event handler for the "retrieveHeadofHousehold" function.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Writing REST API

    Hi,

    The error ubdefined is occuring for SDK? if yes then check if you have added rhe SDK.REST liabrary on form

    Hope it 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

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