Friday, February 15, 2019

Ability to mimic the upsert or upsertList function using PHP Toolkit


This article applies to the PHPToolkit prior to 2012.2

To make the code below compliant to PHP Toolkit 2012.2 and above versions, please refer to the solution Coding Comparison between PHP Toolkit 2012.2 and old versions - Answer ID 24910. 

Upsert / UpsertList function is not available on the latest version of PHP Toolkit 2011.2, when accessing the PHPtoolkit.php file on the PHP_Toolkit folder these two functions are not available.

As a workaround ,customer could use the following construct with the previous and current(2011.2)  PHP Toolkit versions: update() first and if fails, add() the record

// create array of customer fields
    $customerFields = array (
        'isPerson'          => true,
        'firstName'         => "myFirstName3",
        'lastName'          => "myLastName3",
        'companyName'       => "myCompanyName3",
        'phone'             => "555-555-1233",
        'email'             => "",
        'externalId'        => 678

    // create Customer record
    $customer = new nsComplexObject('Customer');
    // set fields
    // perform add operation and store nsWriteResponse object in $updateResponse variable
    $updateResponse = $myNSclient->update($customer);

    // handle add response
    if (!$updateResponse->isSuccess)

          echo "<font color='red'><b>" . $updateResponse->statusDetail[0]->message . "</b></font>";
           if ($updateResponse->statusDetail[0]->type === 'ERROR' && $updateResponse->statusDetail[0]->code === 'INVALID_KEY_OR_REF') {
               $addResponse = $myNSclient->add($customer);
    } else {

     echo "<font color='green'><b>SUCCESS!!!</b></font>";


No comments:

Post a Comment