Oracle Service Cloud RightNowCX External Events / Custom Process: Curl a service on Save or Update

/*
* CPMObjectEventHandler: updateContact
* Package: OracleServiceCloud
* Objects: Contact
* Actions: Create, Update
* Version: 1.3
*/

use \RightNow\CPM\v1 as RNCPM;
use \RightNow\Connect\v1_3 as RNCPHP;

load_curl();

class updateContact implements RNCPM\ObjectEventHandler {

/**
* Apply CPM logic to object.
* @param int $runMode
* @param int $action
* @param object $contact
* @param int $cycle
*/
public static function apply($run_mode, $action, $contact, $n_cycles) {

if ($n_cycles != 0) return;

try {

$contact->CustomFields->c->contact_field_being_checked = new RNCPHP\NamedIDLabel();
$contact->CustomFields->c->contact_field_being_checked->ID = 2192; // Unregistered CPM
$header[] = "Content-Type: application/x-www-form-urlencoded";
$service_endpoint = 'http://INTERFACE-URL/cgi-bin/nikeagent.cfg/php/custom/scriptName.php?email=';

$noEmail = true;

foreach ($contact->Emails as $email) {
if ($email) {
$noEmail = false; // found an email

$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $service_endpoint . $email->Address,
CURLOPT_HEADER => 0,
CURLOPT_HTTPHEADER => $header,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
));
$resp = curl_exec($ch);

if ($resp !== false) {
$respObj = json_decode($resp, true);
$regUserString = $respObj['contact_field_being_checked']; // 1 or empty, is boolean
if ($regUserString == 1) {
$contact->CustomFields->c->contact_field_being_checked->ID = 2189; // Registered CPM
}else{
}
}
curl_close($ch);
}
}

$contact->save(RNCPHP\RNObject::SuppressAll);
} catch (\Exception $e) {
throw $e;
}

return;
}
}

class updateContact_TestHarness implements RNCPM\ObjectEventHandler_TestHarness {
static $test_contact = null;

public static function setup() {
// TODO: Pass array of contacts
// Should pass an array of contacts with different
// emails and different orders of registered emails

// We want to create contacts on the fly, but
// you can't duplicate emails, so we should probably check
// if the email exists, and delete it - perhaps have a set test
// contact
try {
$contact = new RNCPHP\Contact;
$contact->Emails = new RNCPHP\EmailArray();
$contact->Emails[0] = new RNCPHP\Email();
$contact->Emails[0]->AddressType = new RNCPHP\NamedIDOptList();
$contact->Emails[0]->AddressType->LookupName = "Email - Primary";
$contact->Emails[0]->Address = "somebody@fastmail.jp";
$contact->Emails[0]->Invalid = false;
$contact->CRMModules = new RNCPHP\CRMModules();
$contact->CRMModules->Service = 1;
$contact->save(RNCPHP\RNObject::SuppressAll);

static::$test_contact = $contact;
} catch (\Exception $e) {
echo $e->getMessage();
}

return;
}

public static function fetchObject($action, $object_type) {
// Returned object is passed to apply()
return (static::$test_contact);
}

public static function validate($action, $object) {
if (RNCPM\ActionCreate == $action) {
if (assert($object->CustomFields->c->contact_field_being_checked->ID == 2189)) {
echo "Create test passed - " . $object->Emails[0]->Address . "\n";
return true;
} else {
echo "Create test FAILED - " . $object->Emails[0]->Address . "\n";
}
} elseif (RNCPM\ActionUpdate == $action) {
if (assert($object->CustomFields->c->contact_field_being_checked->ID == 2189)) {
echo "Update test passed - " . $object->Emails[0]->Address . "\n";
return true;
} else {
echo "Update test FAILED - " . $object->Emails[0]->Address . "\n";
}
} else {
echo "Invalid action";
}
}

public static function cleanup() {
// Delete temporary test contact
$contact = static::$test_contact;
$contact->destroy();

static::$test_contact = null;
}
}

Leave a Reply

You must be logged in to post a comment.