• Resolved josephmarkovich

    (@josephmarkovich)


    I am creating records in Dataverse from functions.php, but I am not understanding how to actually UPDATE a record that was just created.

    After WooCommerce completes an order, I create the account record in Dataverse:

    $order = wc_get_order($order_id);

    $client = ConnectionService::instance()->getClient();

    $account = new Entity( ‘account’ );
    $account[‘name’] = $order->get_billing_company();
    $account[‘address1_line1’] = $order->get_billing_address_1();
    $account[‘address1_line2’] = $order->get_billing_address_2();
    $account[‘address1_city’] = $order->get_billing_city();
    $account[‘address1_stateorprovince’] = $order->get_billing_state();
    $account[‘address1_postalcode’] = $order->get_billing_postcode();
    $account[‘address1_country’] = $order->get_billing_country();
    $account[‘telephone1’] = $order->get_billing_phone();

    $accountId = $client->Create( $account );

    Then I create a contact:

    $contact = new Entity( ‘contact’ );
    $contact[‘firstname’] = $order->get_billing_first_name();
    $contact[‘lastname’] = $order->get_billing_last_name();
    $contact[’emailaddress1′] = $order->get_billing_email();
    *** $contact[‘parentcustomerid’] = $accountId; ***

    $contactId = $client->Create( $contact );

    It all works great (thanks George and team!) — except for the starred line above. I want to link the contact to the account just created. Then I also want to update the Account to have the primary contact be the contact just created.

    I don’t understand how to do those two things.

    Any guidance would be really helpful.

    Thank you.
    Joe

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author alexacrm

    (@alexacrm)

    To set the value for a lookup field, you need to use EntityReference class:

    $contact['parentcustomerid'] = new EntityReference('account', $accountId);

    HTH

    Thread Starter josephmarkovich

    (@josephmarkovich)

    If I’ve created the Account and the Contact — and then I need to update the Account to set the Primary Contact of the contact just created, how do I do that?

    I tried this and not correct:

    $account['primarycontactid'] = new EntityReference('contact', $contactId);
    $accountId = $client->Update( $accountId );

    $accountId is the account created previously in the code.

    Thank you.
    Joe

    Plugin Author alexacrm

    (@alexacrm)

    Hi Joe,

    you do need to instantiate the record (very much similar to C# code):

    
    $accUpdate = new Entity( 'account', $accountId );
    $accUpdate['primarycontactid'] = new EntityReference('contact', $contactId);
    $client->update( $accUpdate ); /* or $accUpdate->update(); */

    HTH
    George

    Thread Starter josephmarkovich

    (@josephmarkovich)

    Thank you George!

    Joe

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Fill in lookup field once record created’ is closed to new replies.