Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Lookup Fields - Guidance needed #25

Closed
adrenaline15 opened this issue Aug 21, 2017 · 24 comments
Closed

Setting Lookup Fields - Guidance needed #25

adrenaline15 opened this issue Aug 21, 2017 · 24 comments

Comments

@adrenaline15
Copy link

First of all I wanted to thank you for this very helpful project, it makes the whole Dynamics CRM communication a lot easier!

I successfully tested and implemented the code to create and update a new contact (lead in my case).

The only thing I'm missing now is 'to set lookup values', which should be retrieved as GUID from the org.

I did not find an example howto achieve this task, so I was hoping that someone would be so nice to show me / point me to right implementation.

Thanks in advance!

@wizardist
Copy link
Collaborator

Hi @adrenaline15

We will surely add an example to the codebase. If you want a quick one, here it is:

$leadContact = $client->entity( 'contact' );
$leadContact->ID = 'GUID HERE';
$lead->contactid = $leadContact;

$lead->update();

Thanks.

@wizardist
Copy link
Collaborator

wizardist commented Aug 22, 2017

It would be more semantically correct (and in the spirit of .NET implementation) to use an EntityReference:

$lead->contactid = new EntityReference( 'contact', 'GUID HERE' );
$lead->update();

@adrenaline15
Copy link
Author

Hi @wizardist,

thank you very much for your response.

I was able to set the required Lookup-Fields, but I needed to change an if-check in your code which obviously shouldn't be necessary.

if ( !$value instanceOf self ) {

The line above checks if my EntityReference-Object is an instance of self (=Entity-Object).
Since Entity extends EntityReference (and not the other way round) it doesn't match and so I needed to change this line to be able to set the Lookup-values

<?php

class A {}
class B extends A {}

$a = new A();
$b = new B();

echo sprintf('$a is %san instance of A' . PHP_EOL, $a instanceOf A ? '' : 'not ');
// $a is an instance of A
echo sprintf('$a is %san instance of B' . PHP_EOL, $a instanceOf B ? '' : 'not ');
// $a is not an instance of B
echo sprintf('$b is %san instance of A' . PHP_EOL, $b instanceOf A ? '' : 'not ');
// $b is an instance of A
echo sprintf('$b is %san instance of B' . PHP_EOL, $b instanceOf B ? '' : 'not ');
// $b is an instance of B

Thanks

@wizardist
Copy link
Collaborator

Yeah, this should be fixed.

@sfenne
Copy link

sfenne commented Oct 17, 2017

When calling the entity method while passing a KeyAttribute, the script will fail in Entity:1297

Catchable Fatal Error: Object of class AlexaCRM\CRMToolkit\KeyAttributes could not be converted to string

@georged
Copy link
Contributor

georged commented Oct 17, 2017

@sfenne can you provide a snippet of code that causes the error?

@sfenne
Copy link

sfenne commented Oct 18, 2017

@georged sure. I'm trying to lookup an account by its accountnumber.

    $contactKey = new \AlexaCRM\CRMToolkit\KeyAttributes();
    $contactKey->add( 'accountnumber', $accountNumber );

    $contact = $this->client->entity( 'account', $contactKey );`

@georged
Copy link
Contributor

georged commented Oct 18, 2017

@sfenne
This construct is designed to handle alternate keys - did you create alternate key over the accountnumber in your CRM instance?

@sfenne
Copy link

sfenne commented Oct 18, 2017

@georged the accountnumber exists, but I'm not too familiar with CRM. If I replace the KeyAttributes $contactKey with the GUID of an account, I get full account result. An array with all the values, which includes the accountnumber key and value. Is it possible I need to do further actions in CRM to "enable" accountnumber as a lookupable field via API?

Thanks

@georged
Copy link
Contributor

georged commented Oct 18, 2017

@sfenne

you need to create alternate key in CRM. Note that it's not going to create a lookup which is CRM construct for a foreign key, it will simply allow you to retrieve the record by account number.

If you have any further questions, I recommend creating a separate thread as your question has little to do with lookup fields and is about alternate keys.

Thanks

@wizardist
Copy link
Collaborator

077ae7e now allows setting EntityReference as a lookup value. Closing this one.

@ziyaindia
Copy link

ziyaindia commented Feb 13, 2019

I am trying to create a Case by using https://github.com/AlexaCRM/php-crm-toolkit.

When I am using following code getting following error:

Fatal error: Uncaught AlexaCRM\CRMToolkit\SoapFault: Contact With Id = 9f3065e3-64bc-e811-a94c-000d3a4e761d Does Not Exist in \vendor\alexacrm\php-crm-toolkit\src\Client.php on line 1159

<?php
/**
 * Use init.php if you didn't install the package via Composer
 */
require_once 'vendor/autoload.php';

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

$options = [
    'serverUrl' => '*********',
    'username' => '**************',
    'password' => '*******************',
    'authMode' => 'OnlineFederation',
];
$guid = '9F3065E3-64BC-E811-A94C-000D3A4E761D';


$serviceSettings = new Settings( $options );
//echo '<pre>';print_r($serviceSettings);echo '</pre>';
$service = new OrganizationService( $serviceSettings );
//echo '<pre>';print_r($service);echo '</pre>';

$incident = $service->entity('incident');
//echo '<pre>';print_r($incident);echo '</pre>';
$incident->title = 'Test Created With Proxy';
$incident->description = 'This is a test incident';
$incident->customerid = new EntityReference( 'contact', $guid );
//$incident->ID = $guid;//contactid responsiblecontactid primarycontactid
$incidentId = $incident->create();

@wizardist
Copy link
Collaborator

@ziyaindia are you sure such contact really exists? The fault is coming from CRM, and that's CRM telling you that it doesn't exist.

@ziyaindia
Copy link

How can i come to know which contact id i can use for this?

@georged
Copy link
Contributor

georged commented Feb 13, 2019

@ziyaindia you're trying to create a case for a contact. Surely, you need to know who the contact is. How would you like to identify the contact you want to create a case for? It can be hard-coded (you need to find out guid of that contact from CRM), or you could run fetchxml with some conditions to find a contact, or you could use alternate keys. Finally, you can just create a contact before creating a case. Whatever you want. But you need to have a valid contact id, this is something that we can't help you with.

@ziyaindia
Copy link

I used Email link and get following link in my compose email window:

Eric Williams
https://localposhinc.crm.dynamics.com/main.aspx?appid=bf2a44ac-d5b8-e811-a999-000d3a1ab48b&etc=8&extraqs=formid%3dc2cd9e55-d4b4-4b55-9951-16ead79643e5&id=%7b9F3065E3-64BC-E811-A94C-000D3A4E761D%7d&pagetype=entityrecord
Sent from Mail for Windows 10

I took - 9F3065E3-64BC-E811-A94C-000D3A4E761D this id for contact

@georged
Copy link
Contributor

georged commented Feb 13, 2019

That's id of a systemuser (etc=8), not of a contact.

@ziyaindia
Copy link

How i can find the contact id in my php script?Please help me I stuck too much.

@georged
Copy link
Contributor

georged commented Feb 13, 2019

What contact do you want the case created for?!

@ziyaindia
Copy link

When someone submit contact us query from my website then I want to create a case in CRM

@georged
Copy link
Contributor

georged commented Feb 13, 2019

So who would be a contact in this scenario? Are you going to prompt them for first/last name and create contact at the same time? Then instead of

$guid = '9F3065E3-64BC-E811-A94C-000D3A4E761D';

you'd want something like

$contact = $service->entity( 'contact' );
$contact->firstname = 'John';
$contact->lastname = 'Doe';
$contact->emailaddress1 = 'john.doe@example.com';
$guid = $contact->create();

Thanks
George

@ziyaindia
Copy link

ziyaindia commented Feb 13, 2019

Thanks for replying

If I already having a contact in my CRM with email id 'john.doe@example.com', How can i get contact id for this?
Can I use this contact Id to create a case?

Regards,
Ziya

@georged
Copy link
Contributor

georged commented Feb 13, 2019

Ziya,

you would have to use fetchxml to search and locate the contact.

Thanks

@ziyaindia
Copy link

Thanks for all your help

Now I am creating note for a lead.

Note is not creating under leads

Using following code foe this

$leadId = $lead->create();

$note = $service->entity('annotation');
//echo '

';print_r($incident);echo '
';
$note->objectid = new EntityReference( 'lead', $leadId );
$note->objecttypecode = 'lead';
$note->subject = 'Note is Created From Lead';
$note->documentbody = $insuranceFile;
$note->mimetype = "text/plain";
$note->notetext = $note;
$note->filename = $insuranceFileName;
//$incident->ID = $guid;//contactid responsiblecontactid primarycontactid
$noteId = $note->create();

Thanks,
Ziya

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants