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)

Using Dynamics CRM API to associate contact with account

(0) ShareShare
ReportReport
Posted on by

Hi everyone,

I'm new to the Dynamics API and just starting to get my head around how it all works. We have been using the REST API to create accounts and contacts, on creation of the account we are creating the primary contact, which seems to be working OK so far. The hurdle I'm facing is that I'm not sure how to assign the company to the contact.

This is how we are creating the primary contact for the account, however its not automatically linked as a contact for that account.

$response = $this->dn->accounts->insert(array('name'=> $company,'emailaddress1'=> $email,'telephone1'=>$phone,'address1_telephone1'=>$phone,'address1_line1'=>$address1_line1,'address1_line2'=>$address1_line2,'address1_stateorprovince'=>$address1_stateorprovince,'address1_city'=>$address1_city,'address1_postalcode'=>$address1_postalcode,'address1_country'=>$address1_country,'customertypecode'=>'3','primarycontactid'=>array('emailaddress1'=> $email,'mobilephone'=>$phone,'firstname'=>$fname,'lastname'=>$lname,'address1_line1'=>$address1_line1,'address1_line2'=>$address1_line2,'address1_country'=>$address1_country,'address1_city'=>$address1_city,'address1_stateorprovince'=>$address1_stateorprovince,'address1_postalcode'=>$address1_postalcode,'jobtitle'=>$jobtitle,'company'=>$phone)));

I thought nmaybe updating the contact after create parentcustomerid_account would work, but it seems to expect an array? Im lost any help would be greatly appreciated.

$cresponse = $this->dn->contacts->update($co['contactid'], array('emailaddress1'=> 'lmorrow@xxxx.com.au','parentcustomerid_account'=>$account_id));

7522.Screenshot_5F00_23.png

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Got it!!!!

    Sharing for anyone who might need this in the future. How to link a contact with an account in Dynamics CRM API

    $cresponse = $this->dn->contacts->update($c['contactid'], array('parentcustomerid_account@odata.bind'=>'/accounts('.$a['accountid'].')'));

  • Community Member Profile Picture
    on at

    Please, can you describe how you programatically derived the field 'parentcustomerid_account' to be used with the @odata.bind annotation?

    I need to figure out a way to dynamically discover what field to use with the @odata.bind annotation regardless of which two entities I am associating.  Also, if it helps...I will be performing these associations always during update.  never during create.

    Any help you can provide would be greatly appreciated.

    Thanks,

    -Charlie

  • Community Member Profile Picture
    on at

    I MAY have found something.  When I get the schema for 'contact' there exists an attribute where 'parentcustomerid' is the LogicalName (or AttributeOf) key.

    I come across two such attributes.  One of these attributes has a List called 'Target'.  Inside the list are 'account' and 'contact'  IF this is correct train of thought, and repeatable, I could take the parentcustomerid and concatenate the referencingEntity which is the 'account' after an underscore.  I need to see if this holds true for other cases.

  • Community Member Profile Picture
    on at

    Any navigation property must be updated using the @odata.bind annotation. I think generally it is the navigation property's name concatenated with an underscored followed by the current entity type.

    This is not always the case. For example on the Task Activity type when updating the regarding navigation property to an account it is regardingobjectid_account_task@odata.bind. When its a contact it changes to regardingobjectid_contact_task@odata.bind as you might expect. So when the navigation property can point to different types, it seems you must specify what type it is pointing to as well.

  • Community Member Profile Picture
    on at

    I believe that the solution is that when I get the attributes and match the ReferencedEntity to my EntityType, then i take the value under the key called

    ReferencedEntityNavigationPropertyName

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