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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

getting "Undefined" in oData service for custom entity. Can anyone please tell me what is wrong in my code?

(0) ShareShare
ReportReport
Posted on by 840

I put alert box alert(retrieveEntityReq.status) and it is returning value of 200, but whey I am getting "undefined" for an attribute. 

 

function populateAddress() {

    LookupFieldObject = Xrm.Page.data.entity.attributes.get('fund_organization');

    if (LookupFieldObject) {

        var EntityId = LookupFieldObject.getValue()[0].id;

        EntityId = EntityId.replace('{', '').replace('}', '');

        alert(EntityId);

        if (EntityId.trim() != null) {

            oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";

            RetrieveRecord(EntityId, oDataPath);

        }

    }

}

function RetrieveRecord(Id, ODataPath) {

    try {

        var retrieveReq = new XMLHttpRequest();

        retrieveReq.open("GET", ODataPath + "/fund_addressSet?$filter=fund_IsPrimary/Value eq 110000000 and fund_IndividualId/Id eq guid'" + Id + "'", true);

    

        retrieveReq.setRequestHeader("Accept", "application/json");

        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        retrieveReq.onreadystatechange = function () {

            retrieveReqCallBack(this);

        }

        retrieveReq.send();

    }

    catch (err) {

        var text = "There was an error on this page.\n\n";

        text += "Error description: " + err.message + "\n\n";

        text += "Click OK to continue.\n\n";

        alert(text);

    }

}

function retrieveReqCallBack(retrieveEntityReq) {

    if (retrieveEntityReq.readyState == 4) {

        alert(retrieveEntityReq.status);

        retrieveEntityReq.onreadystatechange = null;

        alert(retrieveEntityReq.status);

        if (retrieveEntityReq.status == 200) {

            var retrievedEntity = JSON.parse(retrieveEntityReq.responseText).d;  

 

            alert(retrievedEntity.fund_name);

            alert(retrievedEntity.fund_Address_Line2);

 

        }

    }

}

---

Thanks. 

*This post is locked for comments

I have the same question (0)
  • Aileen Gusni Profile Picture
    44,524 on at

    Hi,

    Which one you find undefined?

     alert(retrievedEntity.fund_name);

               alert(retrievedEntity.fund_Address_Line2);

    Make sure that you are using correct name (case sensitive)

    Try to alert(retrievedEntity)

    Do you get any returned object or not.

    Thank you.

  • crmUserMN Profile Picture
    840 on at

    Yes, I tried to put alert(retrievedEntity)

    and go t back an objact.

    Did you find anything wrong in the oData query string?

    I tried the query string with chrome browser and it returned data.

    Any clue?

    Thanks.

  • Suggested answer
    Hosk Profile Picture
    on at

    undefined usually means you are trying to retrieve a column which doesn't exist.

    When tricky part of using data is it's case sensitive.

    I would advise you to create the oData query designer (it works for CRM 2011 and CRM 2013) this will allow you to see what the fields are

    you can find it here

    blogs.msdn.com/.../dynamics-crm-2011-and-2013-useful-tools-and-features.aspx

  • crmUserMN Profile Picture
    840 on at

    Thanks Ben, I do have oData query designer installed. When I copy and paste my query string in query designer, I can see my data is returned and I can see data on browser as well. Thanks.

  • crmUserMN Profile Picture
    840 on at

    To troubleshoot the issue I added

    for (var key in retrievedEntity) {

                   if (retrievedEntity.hasOwnProperty(key)) {

                       alert(key + " -> " + retrievedEntity[key]);

                   }

               }

    at the end of my code to find properties

    But when I run it returned -

    result --> [object  object]

    Any suggestion?

    Thanks.

  • Royal King Profile Picture
    27,686 on at

    It looks like some of your attibutes are lookup so based on the attribute type , you need retrieve the value

    for Lookup field to get different value

    retrievedEntity[key].Id   //returns Guid

    retrievedEntity[key].Name  //returns Name

    retrievedEntity[key].LogicalName // Returns EntityName

    Try with below code if your field type is a lookup

    for (var key in retrievedEntity) {

                  if (retrievedEntity.hasOwnProperty(key)) {

                      alert(key + " -> " + retrievedEntity[key].Id);

                  }

              }

  • crmUserMN Profile Picture
    840 on at

    I think that somethnig is wrong with the query string. Can anyone help me?

    If I change my query string to

    retrieveReq.open("GET", ODataPath + "/fund_addressSet(guid'" + Id + "')", true);

    I can see both fund_name, and fund_Address_Line2 on alert boxes.

    But this is not my business requirement. So when I use -

    retrieveReq.open("GET", ODataPath + "/fund_addressSet?$filter=fund_IsPrimary/Value eq 110000000 and fund_IndividualId/Id eq guid'" + Id + "'", true);

    I get "Undefined" in the alert boxes.

    Thanks.  

  • Royal King Profile Picture
    27,686 on at

    It looks like you are not receiving any results back  when you add additional condition in the query. Check whether you are able to get any record returned in the advance find based on condition below

    fund_isprimary = 110000000  and fund_IndividualId = whatever record.

  • crmUserMN Profile Picture
    840 on at

    I copied and pasted query string from Query builder. Still I received "Undefined"

    Then I runt he query in query designer and copy the result from there and pasted to a JSON parser. Here is my received eval -

    {

    "d":{

    "results":[

    {

    "__metadata":{},

    "fund_Address_ProfessionalTitle":null,

    "fund_addressId":"f52e5134-421e-e411-9448-0050568966a2",

    "VersionNumber":null,

    "OwningUser":{},

    "fund_Email1IsPrimary":null,

    "fund_Phone2Type":{},

    "ModifiedOn":"/Date(1408734038000)/",

    "fund_Email2":null,

    "OwningBusinessUnit":{},

    "fund_IndividualId":{},

    "OwningTeam":{},

    "OwnerId":{},

    "fund_Address_Salutation":"fgnfh",

    "fund_Address_Zip":"55402",

    "fund_Phone3Type":{},

    "CreatedOn":"/Date(1407423043000)/",

    "fund_Address_Line2":null,

    "fund_Email1Type":{},

    "fund_Phone2":null,

    "fund_Organization":{},

    "fund_Type":{},

    "fund_Address_Line3":null,

    "CreatedBy":{},

    "fund_AutoSalulation":null,

    "fund_Address_Country":{},

    "fund_Organizationnotadonor":null,

    "fund_Email2IsPrimary":null,

    "OverriddenCreatedOn":null,

    "ImportSequenceNumber":null,

    "fund_Email1":null,

    "fund_Phone1":null,

    "fund_name":"150 S. Fifth St., #2600",

    "UTCConversionTimeZoneCode":null,

    "fund_Address_City":"Minneapolis",

    "TimeZoneRuleVersionNumber":null,

    "fund_Address_State":{},

    "ModifiedOnBehalfBy":{},

    "fund_Addresss_County":"Hennepin County",

    "fund_IsPrimary":{},

    "statuscode":{},

    "fund_Email2Type":{},

    "fund_Phone1IsPrimary":{},

    "statecode":{},

    "fund_Phone3":null,

    "fund_Phone3IsPrimary":{},

    "fund_Blank":null,

    "CreatedOnBehalfBy":{},

    "fund_Phone2IsPrimary":{},

    "fund_Address_Type":{},

    "ModifiedBy":{},

    "lk_fund_address_createdby":{},

    "lk_fund_address_createdonbehalfby":{},

    "lk_fund_address_modifiedby":{},

    "lk_fund_address_modifiedonbehalfby":{},

    "user_fund_address":{},

    "team_fund_address":{},

    "business_unit_fund_address":{},

    "fund_address_connections1":{},

    "fund_address_connections2":{},

    "fund_address_AsyncOperations":{},

    "fund_address_UserEntityInstanceDatas":{},

    "fund_address_ProcessSession":{},

    "fund_address_BulkDeleteFailures":{},

    "fund_address_PrincipalObjectAttributeAccesses":{},

    "fund_Individual_Address":{},

    "fund_address_Annotations":{},

    "fund_Address_Organization":{},

    "fund_address_ActivityPointers":{},

    "fund_address_Appointments":{},

    "fund_address_Emails":{},

    "fund_address_Faxes":{},

    "fund_address_Letters":{},

    "fund_address_PhoneCalls":{},

    "fund_address_ServiceAppointments":{},

    "fund_address_Tasks":{},

    "fund_address_RecurringAppointmentMasters":{},

    "fund_address_SocialActivities":{}

    }

    ]

    }

    }

    I can see fund_name, and fund_Address_City in the eval.. Then why is is throwing "Undefined"? Thanks.

  • crmUserMN Profile Picture
    840 on at

    Now I -

    1.  Change the query and select only "fund_name", and "fund_Address_City"

    2. stringify the received object to see the returning value

    Now I can see that it is returning the properties, but when I tried to access the properties on alert boxes, I received "undefined". Why? Any help?

    Thanks.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans