PeeKay7
PeeKay7

Reputation: 11

Dynamics CRM 2013: How to check whether an contact already got a connection to a record?

we've got multiple companys (entity "Account") in our Dynamics CRM 2013, which can have multiple employees (entity "Contact"). We connect them (entity: "Connection") together.

One employee can work for multiple companys at the same time.

It should be possible to connect an employee to the same company again, if the field "EffectiveEnd" (entity "Connection") is not empty.

I wanted to use the SDK this way:

If field "Record1" contains data, catch all records in the entity "connection", where the Name is identical. This way, I could check in the next step, whether the field "EffectiveEnd" contains data and if not, break the function. I used the following code:

function validateContact() {
if (Xrm.Page.data.entity.attributes.get("record2id").getValue() != null) {
    var primaryContactName = Xrm.Page.data.entity.attributes.get("record2id").getValue()[0].name;

    var paramType = "Connection";

    var paramOptions = "$select=EffectiveStart&$filter=Record2RoleId/Name eq " + primaryContactName + "";

    var strQueryResult = SDK.REST.retrieveMultipleRecords(paramType, paramOptions, retrieveSuccessCallback, errorHandler, retrieveComplete);
} 
else {
//     alert("empty");
    return;
  }
}

but I'm getting error 400 (own translation at the error messages):

var paramOptions = "$select=EffectiveStart&$filter=Record2RoleId/Name eq " + primaryContactName + "";

results in

Error: 400: Bad Request: No Property 'Schulz' in type 'Microsoft.Xrm.Sdk.Entity' at position 22.

and

var paramOptions = "$select=EffectiveStart&$filter=Record2RoleId/FullName eq " + primaryContactName + "";

results in

Error: 400: Bad Request: No property 'FullName' in type 'Microsoft.Crm.Metadata.ComplexTypeinstance'1[[Microsoft.Xrm.Sdk.EntityReference, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=XXXXX]]' at position 14.

Thanks in advance!

:e Typos

Upvotes: 1

Views: 502

Answers (1)

James Wood
James Wood

Reputation: 17562

I'm guessing your SDK.REST.retrieveMultipleRecords is the same function demonstrated here.

In which case you are effectively doing this:

SDK.REST.retrieveMultipleRecords("Connection", "$select=EffectiveStart&$filter=Record2RoleId/FullName eq " + primaryContactName + ", ...);

In which case you would seem to be trying to query the connection entity using fields that doesnt exist on the connection entity. Fullname is a contact field.

You will need to join from the connection entity to the contact entity via the lookup (EntityReference) fields.

Retrieve related entities by expanding navigation properties

Use the $expand system query option in the navigation properties to control what data from related entities is returned.

[Organization URI]/api/data/v8.2/accounts?$select=name&$expand=primarycontactid($select=contactid,fullname)

Or, you could change to query directly onto the contact entity. var paramType = "contact";

Upvotes: 0

Related Questions