AlexaCRM / dynamics-webapi-toolkit

Dynamics 365 Web API Toolkit for PHP
MIT License
75 stars 58 forks source link

Creating 'connections' between Account and Contact and Roles #92

Closed juliandavis71 closed 1 year ago

juliandavis71 commented 1 year ago

How do you create a Connection ie ‘connecting’ a contact to a Role?. I have tried the parentcustomerid and $client->Associate but can’t seem to get the connection working and whils the data is saving, it is not creating the connection.

Below is an extract of the function I have:

$contact = new \AlexaCRM\Xrm\Entity( 'contact' );

            $contact['firstname'] = $firstname;
            $contact['lastname'] =  $lastname;
            $contact['emailaddress1'] = $emailaddress;
            $contact['mobilephone'] =$phoneNumber;
            $contact['jobtitle'] =$jobtitle;
            $contact['moq_preferredname'] =$preferredname;

            //Subscriptions
            foreach ($subs as $sub){ 
                $contact[$sub]=true;
            }

            $contact['parentcustomerid'] = new \AlexaCRM\Xrm\EntityReference( 'account', $accountID );
            $custNumber =  $this->_client->Create( $contact );

            if($custNumber){
            $this->_client->Associate(
                'contact',
                $accountID,
                new \AlexaCRM\Xrm\Relationship( 'contact_customer_accounts' ),
                [
                    new \AlexaCRM\Xrm\EntityReference( 'contact', $custNumber ),
                ]
            );
            }
georged commented 1 year ago

@juliandavis71

when you say "Role", what table are you talking about? Connection?

When trying to associate with an account, first parameter should be 'account':

$this->_client->Associate( 'account', $accountID, ...

but it shouldn't be required as you're already setting up association when setting $contact['parentcustomerid'] during create.

juliandavis71 commented 1 year ago

@georged

Might be easier to explain in screen shots:

When we create a contact we need to link them via a New Connection to an organisation and to the role. In this case, the job role is a Teacher, Non-Teacher etc. so not 'Role' on thr true sense of the word

image

georged commented 1 year ago

@juliandavis71

Connection is a standard Dataverse table. It's a bit tricky to deal with as there are connection roles involved and there are two records that need to be created but there is nothing in there that can't be done with the toolkit. https://learn.microsoft.com/power-apps/developer/data-platform/reference/entities/connection.

There are C# samples available: https://learn.microsoft.com/dynamics365/customerengagement/on-premises/developer/sample-code-connection-entities that should give you some ideas.

juliandavis71 commented 1 year ago

@georged

To clarify, a 'Connection' is different to a 'Relationship'?

Thanks

georged commented 1 year ago

@juliandavis71 I don't quite understand the question.

Relationship is a term describing ... ehmmm... a relationship between two tables, can be 1:N implemented as a lookup field, can be N:N which in Dataverse can be implemented as either 'native' or 'manual'. https://learn.microsoft.com/power-apps/maker/data-platform/relationships-overview

Connection is a physical table that allows you to create a relationship between any two records from almost any two tables in Dataverse.

juliandavis71 commented 1 year ago

@georged Just trying to understand how I can create the connection record using the wrapper. I see the Xrm\Relationship and was looking to see if this links to Connection, if at all.

georged commented 1 year ago

@juliandavis71 Xrm\Relationship is a class describing a 1:N relationship, it corresponds to the Relationship .NET class https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.relationship. It is used in Associate request when you need to create a link between two records.

Connection is a table in Dataverse and you will need to create a record in this table using a standard Create request. As I mentioned it can get a bit tricky because you need to know both records and both roles (to and from), all of which are lookups, and create a reverse connection record as well (don't think it's mandatory but expected). You don't need Associate request in this scenario, simply set attribute values and toolkit will take care of the rest.

I suggest you look at C# .NET samples I mentioned earlier to learn what it's all about.

juliandavis71 commented 1 year ago

For anyone else that may need assitance, the following is what I used to create connections between accounts and contacts:

                   $_connectionSchool = new \AlexaCRM\Xrm\Entity('connection');

                     $_connectionSchool["record2id"] = new \AlexaCRM\Xrm\EntityReference( 'contact', $contactID );  //Target
                     $_connectionSchool["record1id"] = new \AlexaCRM\Xrm\EntityReference( 'account', $accountID );  //Source
                     $_connectionSchool["record2roleid"] = new \AlexaCRM\Xrm\EntityReference( 'connectionrole', $roleID ); //ConnectionRole

                     $this->_client->Create( $_connectionSchool );