Question Status

Verified
Doug Coates asked a question on 24 Mar 2015 7:23 PM

Hi,

We're currently moving from CRM4 to CRM2011/13. When I run the following code...

    var queryOptions = {
        entityName: "new_employment",
        attributes: ["new_clientnameid"],
        values: oCGuid,
        columnSet: ["new_employerid", "new_startdate", "new_datefinished", "statuscode"],
        orderby: ["new_employerid"]
    };

    var fetchedJobs = XrmServiceToolkit.Soap.QueryByAttribute(queryOptions);

    if (fetchedJobs[0] != null) {
        for (var i = 0; i < fetchedJobs.length; i++) {
            
            // Get Employer GUID
            var oEGuid = fetchedJobs[i].attributes["new_employerid"].value;
        }
     }


 ...the line

     var oEGuid = fetchedJobs[i].attributes["new_employerid"].value

returns "undefined" and so results in an error later in the code.

The fetchedJobs.length returns 5 -this is correct (for the record I am accessing).

If I replace "new_employerid" with any other values from the columnSet then there is no problem at all, so the query appears to be working alright.

When the corresponding code is run in CRM4 on the same data, it works fine.

The field "new_employerid" is a lookup field on a form and stores the Employer GUID - though I understand that this is an array and wonder if that is why I am having this problem - though perhaps not.

Really am puzzled by this one and would appreciate it if anyone can shed any light on the issue.

Thanks in advance.

Doug.

Reply
Bruno Lucas responded on 24 Mar 2015 8:35 PM

is new_employerid compulsory?

do you have 1 out many "new_employment" that could have no value for "new_employerid"?

try to add this:

var employerid = retrievedContact.attributes['new_employerid'];

if(employerid != null) {

 oEGuid = fetchedJobs[i].attributes["new_employerid"].value;

}

 

Bruno Lucas, Senior CRM Consultant

DynamicDay CRM Blog 

Please mark my post as "Verified" If it helped to solve your problem 

Reply
Doug Coates responded on 24 Mar 2015 9:27 PM

Thanks for your reply, Bruno.

Yes, new_employerid is "business required" and it points back to the organisation that is the 'employer' for the job record that I am trying to access.

Tried the code modification that you suggested but it still returned 'undefined' and then when the code runs on to use the value that should have been returned to interrogate the organisation (accounts) entity - to get the name of the employer I get this error message...

Basically the links are:-  Casenote  -->  Client  -->  Job(s)  -->  Employer (organisation/account).

When doing a casenote for a client the user needs to identify who the employer of that client is so the code looks at the job record(s) of the client, finds the employerID and then uses that to retrieve the organisation name. It's the step of getting the employerID from the job record that is tripping up.

The logic worked fine in CRM4 (using the CrmServiceToolkit) but now using the XrmServiceToolkit it hits this brick wall each time.

Reply
Bruno Lucas responded on 24 Mar 2015 9:57 PM

I've used it recently for the same reason, upgrading from 4 to 11. it worked fine except for the SOAP call.

do you have all the dependencies added on the correct order?

"Dependency JSON2, jQuery 1.7.2 or above"

do you also have the latest version of "XrmServiceToolkit"?

it seems more like something like the JSON is not there because the error is not on the "var fetchedJobs = XrmServiceToolkit.Soap.QueryByAttribute(queryOptions);"

Bruno Lucas, Senior CRM Consultant

DynamicDay CRM Blog 

Please mark my post as "Verified" If it helped to solve your problem 

Reply
Verified Answer
Matthew Knight responded on 24 Mar 2015 11:06 PM

If "new_employerid" is a lookup, that means it stores an Entity Reference. Given that, "value" has no meaning in that context. Try replacing it with "Id". That member should reference the GUID of the EntityReference class.

msdn.microsoft.com/.../microsoft.xrm.sdk.entityreference_members.aspx

Reply
Verified Answer
Mithilesh Kumar responded on 24 Mar 2015 11:10 PM

Hi Doug,

Since "new_employerid" is a Lookup Field, it will store the Reference to the Record which will be a GUID.

To get the GUID Value from the Lookup Field, you will have to use

oEGuid = fetchedJobs[i].attributes["new_employerid"].id;

Hope that helps

Thanks

Reply
Verified Answer
Bruno Lucas responded on 24 Mar 2015 11:16 PM

Good point Matt

this is actually on the documentation but a bit hidden:

Bruno Lucas, Senior CRM Consultant

DynamicDay CRM Blog 

Please mark my post as "Verified" If it helped to solve your problem 

Reply
Doug Coates responded on 25 Mar 2015 12:12 AM

Thanks Matthew, Mithilesh & Bruno,
...that did the trick - all working now (until I hit the next hurdle).

Doug.

Reply
Verified Answer
Matthew Knight responded on 24 Mar 2015 11:06 PM

If "new_employerid" is a lookup, that means it stores an Entity Reference. Given that, "value" has no meaning in that context. Try replacing it with "Id". That member should reference the GUID of the EntityReference class.

msdn.microsoft.com/.../microsoft.xrm.sdk.entityreference_members.aspx

Reply
Verified Answer
Mithilesh Kumar responded on 24 Mar 2015 11:10 PM

Hi Doug,

Since "new_employerid" is a Lookup Field, it will store the Reference to the Record which will be a GUID.

To get the GUID Value from the Lookup Field, you will have to use

oEGuid = fetchedJobs[i].attributes["new_employerid"].id;

Hope that helps

Thanks

Reply
Verified Answer
Bruno Lucas responded on 24 Mar 2015 11:16 PM

Good point Matt

this is actually on the documentation but a bit hidden:

Bruno Lucas, Senior CRM Consultant

DynamicDay CRM Blog 

Please mark my post as "Verified" If it helped to solve your problem 

Reply