Friday, September 7, 2018

WS > PHPToolkit > attach and detach code samples


Note: This article applies to the PHP Toolkit before version 2012.2.

User wishes to perform an attach or detach via WebServices operation using the PHPToolkit.  This article illustrates samplecode that makes this possible.

  • This article requires existing knowledge of Web Services and PHP.

Attach sample code:

...
    require_once '../PHPtoolkit.php';
    require_once 'login_info.php';

    global $myNSclient;


    # ATTACH A CONTACT TO A CUSTOMER
    # ===============


    // create array of fields
    $attachFields = array(
            'attachTo'=> newnsRecordRef(array('internalId' => '297', 'type' => 'customer')),
            'contact' => newnsRecordRef(array('internalId' => '298', 'type' => 'contact')),
        );

    // create record
    $my_attach = new nsComplexObject('AttachContactReference');

    // set fields
    $my_attach->setFields($attachFields);

    // perform attach operation
    $attachResponse = $myNSclient->attach($my_attach);

    // handle response
    if (!$attachResponse->isSuccess) {

        echo "<fontcolor='red'><b>" .$attachResponse->statusDetail[0]->message ."</b></font>";

    } else {

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

    }
 



The above code would generate the following SOAPrequest for the attach request:

<Body>
<attach>
<attachReference xsitype="DetachBasicReference">
<detachFrom internalId="297" type="customer"xsitype="RecordRef" />
<detachedRecord internalId="298" xsitype="RecordRef"/>
</attachReference>
</attach>
</Body>
 


Detach sample code:

...
    require_once '../PHPtoolkit.php';
    require_once 'login_info.php';

    global $myNSclient;


    # DETACH A CONTACT FROM A CUSTOMER
    # ===============


    // create array of fields
    $detachFields = array(
            'detachFrom'=> newnsRecordRef(array('internalId' => '297', type => 'customer')),
            'detachedRecord' =>new nsRecordRef(array('internalId' => '298', type => 'contact')),
        );

    // create record
    $my_detach = new nsComplexObject('DetachBasicReference');

    // set fields
    $my_detach->setFields($detachFields);

    // perform detach operation
    $detachResponse = $myNSclient->detach($my_detach);

    // handle response
    if (!$detachResponse->isSuccess) {

        echo "<fontcolor='red'><b>" .$detachResponse->statusDetail[0]->message ."</b></font>";

    } else {

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

    }
...

 


The above code would generate the following SOAPrequest for the detach request:

<Body>
<detach>
<detachReferencexsitype="DetachBasicReference">
<detachFrom internalId="297"type="customer" xsitype="RecordRef" />
<detachedRecord internalId="298"type="contact" xsitype="RecordRef" />
</detachReference>
</detach>
</Body>
 

 

No comments:

Post a Comment