Skip Navigation
Expand
"ApplicationContext is not set for the request..." error with Connect PHP API v1.4
Answer ID 10299   |   Last Review Date 03/04/2019

Why is my code failing with "ApplicationContext is not set for the request..." when using version 1.4 of the Connect PHP API?

Environment

All Oracle B2C Service
Connect PHP API version 1.4

Resolution

The latest release for the Connect PHP API version 1.4 uses an ApplicationContext with its CurrentContext object. 

The ApplicationContext is used to annotate API requests and it is basically an arbitrary string that needs to be set to provide application context that can later on be retrieved in the code that is being developed. Its maximum length is 40.

It is mandatory to set this ApplicationContext in all custom code using the latest version of the Connect PHP API i.e. 1.4. Otherwise, the code will fail with the error below:

"ApplicationContext is not set for the request..."

The code below is a sample of how this can be achieved when using the Connect PHP API with the fetch() operation for contacts:
=================================================================
<?php
require_once(
get_cfg_var('doc_root'). '/include/ConnectPHP/Connect_init.phph' );
initConnectAPI("accountt","password");
use RightNow\Connect\v1_4 as RNCPHP;
try {
  
  $context = RNCPHP\ConnectAPI::getCurrentContext();
  $context->ApplicationContext = "Contact Name";
  $contact = RNCPHP\Contact::fetch(1);
  echo $contact->Name->First;
}
catch(ConnectAPIErrorFatal $e) {
   echo "**ConnectAPIErrorFatal: ".$e->getMessage()."\n";
} catch(ConnectAPIError $e) {
   echo "**ConnectAPIError: ".$e->getMessage()."\n";
} catch(Exception $e) {
   echo "**CPHP Error: ".$e->getMessage()."\n";
}

============================================================

Alternatively the catch statements may also incorporate logging to the Debug Logs section of the Customer Portal via code similar to below:
try
{
    //  some code
catch(\ConnectAPIErrorFatal $e) {
    logMessage("**ConnectAPIErrorFatal: ".$e->getMessage()."\n");
} catch(\ConnectAPIError $e) {
    logMessage("**ConnectAPIError: ".$e->getMessage()."\n");
} catch(\Exception $e) {
    logMessage("**CPHP Error: ".$e->getMessage()."\n");
}

Available Languages for this Answer:

Notify Me
The page will refresh upon submission. Any pending input will be lost.