简体   繁体   中英

Create an email in Dynamics 365 with php using AlexaCRM php-crm-toolkit

I'm trying to create an email with php in Dynamics 365 by using the AlexaCRM php-crm toolkit after someone fills in the form on our website. The email also appears in Dynamics, but the from and to fields are empty. The email itself and the subject are stored in Dynamics.

Does anyone had the same problem or does anyone knows what I'm doing wrong?

This is the code I'm using.

<?php
require_once '../vendor/autoload.php';

use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;
use AlexaCRM\CRMToolkit\Client;
use AlexaCRM\CRMToolkit\Entity\MetadataCollection;
use AlexaCRM\CRMToolkit\Entity\EntityReference;

$options = [
    'serverUrl' => 'xxx.dynamics.com',
    'username' => 'xxx@xxx.com',
    'password' => 'xxxxxx',
    'authMode' => 'OnlineFederation',
];

$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );

$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->sender = 'Sender Name';
$email->from = 'test@gmail.com';
$email->to = 'Our Company';
$email->torecipients = 'test@ourcompany.com';
$emailId = $email->create();

?>

Thanks for your help. I created activityparties, but it still doesn"t write the email correctly to Dynamics.

I don't know how to combine the two parties.

This is my code:

$to = $service->entity( 'activityparty' );
$to->partyid = new EntityReference( 'systemuser', ''.$guid_stassen.'');

$from = $service->entity( 'activityparty' );
$from->partyid = new EntityReference( 'contact', ''.$guid.'');

$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->from = ''.$from.'';
$email->to = ''.$to.'';
$emailId = $email->create();

rather than

$email->from = ''.$from.'';
$email->to = ''.$to.''; 

you will have to use email_activity_parties as an array. I am not familair with PHP but something like below

$email->email_activity_parties=[
        {
            "partyid_systemuser@odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
            "participationtypemask" : 1
        },
         {
           "addressused":"vvyas@cloudfronts.com",
            "participationtypemask" : 2
        }
        ];

This happens because To and From field is not text field rather it is Party list field .

you cannot directly add/put email address, you will need to create object with it's type and email address.

Take a look at this blog, it has all the information you need.

Below sample for To and from, but here these are users in system.

"email_activity_parties" : [
        {
            "partyid_systemuser@odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
            "participationtypemask" : 1  ///From Email
        },
        {
            "partyid_account@odata.bind" : "/accounts(69C38067-EDB7-E811-A961-000D3A363C81)",
            "participationtypemask" : 2  ///To Email
        }]

Creating Email with unresolved emails (To field of email is not record in MS CRM).

"email_activity_parties" : [
        {
            "partyid_systemuser@odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
            "participationtypemask" : 1 ///From Email
        },
         {
           "addressused":"vvyas@cloudfronts.com",
            "participationtypemask" : 2 ///To Email
        }
        ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM