Thursday, September 20, 2018

Web Service > PHP Toolkit > Add a Custom Record

A sample code to Add a record of a Custom record type via WS using PHP

Note: This article requires existing knowledge of Web Services and PHP.

Sample code snippet in PHP:

require_once '../PHPtoolkit.php';
require_once 'login_info.php';
 
 global $myNSclient;
  
 # ADD A Record of a Custom Record type
 # ===============
 //create an array for the custom record type which will set the custom record ID, etc.
    $recordTypeRef = array(
 "internalId" => "6" // Record type's internal ID
 );
 
 $otherName = " Test - Name"; // Free-Form text  
 $location = "71"; // List/Record
 
 // StringCustomFieldRef : Free-Form text field type
 $customStringField = new nsComplexObject('StringCustomFieldRef');
 $customStringField->setFields(array('internalId' => 'custrecord_other_name',
                        'value' => $otherName));
 
 // SelectCustomFieldRef : List/Record field type
    $customSelectField = new nsComplexObject('SelectCustomFieldRef');
    $customSelectField->setFields(array('value' => new nsListOrRecordRef(array('internalId' => $location)), 'internalId' => 'custrecord_wlf_prod_pref_item'));
        
 $customRecordFields = array(
  "recType" => $recordTypeRef,  
  'customFieldList'  => array($customStringField, $customSelectField)
 ); 
 
 // create Custom Record
 $customRecord = new nsComplexObject('CustomRecord');
 // set fields
 $customRecord->setFields($customRecordFields);
 
 // perform add operation and store nsWriteResponse object in $addResponse variable
 $addResponse = $myNSclient->add($customRecord);
 
 // handle add response
 if ($addResponse->isSuccess) {
  echo "<font color='green'><b>SUCCESSFULLY Added Custom Record!</b></font>";
 } else {
  echo "<font color='red'>" . $addResponse->statusDetail[0]->message . "</font>";
  echo "</b></font>";
 }
 

  
Equivalent SOAP request:

<SOAP-ENV:Body> <ns3:add> <ns3:record xsi:type="ns1:CustomRecord"> <ns1:recType internalId="6" /> <ns1:customFieldList> <ns2:customField xsi:type="ns2:StringCustomFieldRef" internalId="custrecord_other_name"> <ns2:value>Test - Namens2:value> ns2:customField> <ns2:customField xsi:type="ns2:SelectCustomFieldRef" internalId="custrecord_wlf_prod_pref_item"> <ns2:value internalId="71" /> ns2:customField> ns1:customFieldList> ns3:record> ns3:add></SOAP-ENV:Body>

No comments:

Post a Comment