2

I'm trying to develop an apex function to connect my Org with live agent based on Salesforce official guide. Apex function connect with live agent got 2 main step as following:

  1. Create a Live Agent Session:

refer to https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_SessionId.htm

  1. Chat visitor init:

refer to https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_create_visitor_session.htm

  1. connect successful

Develop process

a. my apex function already successful achieve the step 1: I get the session ID and in my debug log show "CALLOUT_RESPONSE [218]|System.HttpResponse[Status=OK, StatusCode=200]"

b. but I fail to Chat visitor init error message was "Session required but was invalid or not found" debug log show "CALLOUT_RESPONSE [268]|System.HttpResponse[Status=Forbidden, StatusCode=403]"

I feel confuse because session ID what I put into the apex code to request Chat visitor init was obtain from the step a. Session ID I sure is correct and no problem becasue I already test and debug many time to comfirm the result.

can anyone tell me the reason and help me solve the problem. this is urgent thing. My apex code show as following;

Part 1: Create a Live Agent Session

HttpRequest req = new HttpRequest();   
req = new HttpRequest();
Http h = new Http();  
h = new Http(); 
String bodyRes = '';     
req.SetEndPoint('https://d.la10.salesforceliveagent.com/chat/rest/System/SessionId'); 
req.setMethod('GET');
req.setHeader('X-LIVEAGENT-API-VERSION','36');
req.setHeader('X-LIVEAGENT-AFFINITY', '');

try {
    HttpResponse res = h.send(req);
    bodyRes = res.getBody();
    System.debug('[Client] Get Session id=============' + bodyRes);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
}

String id = '';
String key = '';
JSONParser parser = JSON.createParser(bodyRes);
System.debug('parser=============' + parser);

while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'id')) {
        parser.nextToken();
        id = parser.getText();System.debug('id=============' + id);
    }
    else if((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'key')){
        parser.nextToken();
        key = parser.getText();System.debug('key=============' + key);
    }
}

Part 2: Chat visitor init

req.SetEndPoint('https://d.la10.salesforceliveagent.com/chat/rest/Chasitor/ChasitorInit'); 
req.setMethod('POST');
req.setHeader('X-LIVEAGENT-API-VERSION','36');
req.setHeader('X-LIVEAGENT-SESSION-KEY', key);System.debug('req=============' + req);

JSONGenerator gen = JSON.createGenerator(true);

gen.writeStartObject();
gen.writeStringField('organizationId', 'xxxxxxxxxx');
gen.writeStringField('deploymentId', 'xxxxxxxxxxxxxxx');
gen.writeStringField('buttonId', 'xxxxxxxxxxxxxxx');
gen.writeStringField('sessionId', id);
gen.writeStringField('userAgent', '');
gen.writeStringField('language', 'en-US');
gen.writeStringField('screenResolution', '1366x768');
gen.writeStringField('visitorName', 'Jen');
//gen.writeFieldName('prechatDetails');gen.writeStartObject();gen.writeEndObject();
//gen.writeFieldName('prechatEntities');gen.writeStartObject();gen.writeEndObject();
gen.writeBooleanField('receiveQueueUpdates', true);
gen.writeBooleanField('isPost', true);
gen.writeEndObject();

String sendMsg = gen.getAsString();
System.debug('sendMsg in json==============' + sendMsg);
req.setBody(sendMsg);
try {
    HttpResponse res = h.send(req);
    bodyRes = res.getBody();System.debug('bodyRes==============' + bodyRes);            
} catch (System.CalloutException e) {
    System.debug('Callout error: '+ e);
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
}

I already test on my postman based on your guide, and finally my request can pop up my live agent. But when I try to implement in my apex code I got error message "Session required but was invalid or not found" again even I already hard code "X-LIVEAGENT-SESSION-KEY" and "sessionId" value.

enter image description here

Can you help me verify my code as following? Thank you very very much.

New updated at 30-4-2016

    HttpRequest req = new HttpRequest();   
    Http h = new Http();  
    String bodyRes = '';

    req.SetEndPoint('https://d.la10.salesforceliveagent.com/chat/rest/Chasitor/ChasitorInit'); 
    req.setMethod('POST');

    req.setHeader('X-LIVEAGENT-API-VERSION','36');
    req.setHeader('X-LIVEAGENT-SESSION-KEY', '18c52adc-0bfc-4843-bc14-6b3ff98cebc5!c9ce3e32-684a-4359-b6f1-edb43141d2a7');
    req.setHeader('X-LIVEAGENT-AFFINITY', 'null');        
    req.setHeader('X-LIVEAGENT-SEQUENCE', '1');

    JSONGenerator gen = JSON.createGenerator(true);

    gen.writeStartObject();
    gen.writeStringField('organizationId', '00D2xxxxxxxxxxx');
    gen.writeStringField('deploymentId', '572xxxxxxxxxxxx');
    gen.writeStringField('buttonId', '573xxxxxxxxxxxx');
    gen.writeStringField('sessionId', '18c52adc-0bfc-4843-bc14-6b3ff98cebc5');
    gen.writeStringField('userAgent', '');
    gen.writeStringField('language', 'en-US');
    gen.writeStringField('screenResolution', '1366x768');
    gen.writeStringField('visitorName', 'Jen');
    gen.writeFieldName('prechatDetails');
        gen.writeStartArray();
        gen.writeEndArray();
    gen.writeFieldName('prechatEntities');
        gen.writeStartArray();
        gen.writeEndArray();
    gen.writeBooleanField('receiveQueueUpdates', true);
    gen.writeBooleanField('isPost', true);
    gen.writeEndObject();

    String sendMsg = gen.getAsString();
    req.setBody(sendMsg);
    try {
        HttpResponse res = h.send(req);
        bodyRes = res.getBody();    
    } catch (System.CalloutException e) {
        System.debug('Callout error: '+ e);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
    }
0
2

You haven't include header for ChasitorInit request:

X-LIVEAGENT-AFFINITY

Where X-LIVEAGENT-AFFINITY is the system-generated ID used to identify the Live Agent session on the Live Agent servers. This affinity token is included in the response body of the SessionId request. That's why you have http 403

When you are using Rest tools e.g. PostMan, token could be omitted, but when you are trying to use SF, ChasitorInit denies access.

Modify Get:

req.setHeader('X-LIVEAGENT-AFFINITY', 'null');
//..
else if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
    (parser.getText() == 'affinityToken')){
    parser.nextToken();
    affinityToken = parser.getText();
}

Post:

req.setHeader('X-LIVEAGENT-AFFINITY', affinityToken);

Minimum request message looks like:

{
"organizationId": "00Dxxxxxxxxxxxx",
"deploymentId": "572xxxxxxxxxxxx",
"buttonId": "573xxxxxxxxxxxx",
"sessionId": "0b333f76-90c7-4cc0-8653-de2885857236",
"userAgent": "",
"language": "en-US",
"screenResolution": "1900x1080",
"visitorName": "Ilya",
"prechatDetails": [],
"prechatEntities": [],
"receiveQueueUpdates": true,
"isPost": true
}

It's very close to your original message, but prechatDetails,prechatEntities are required.

13
  • May I ask, where I can get X-LIVEAGENT-SEQUENCE value? – Jen Apr 29 '16 at 5:41
  • salesforce.stackexchange.com/questions/24966/… define X-Liveagent-Affinity as null when send get to sessionid – Ilya Lepesh Apr 29 '16 at 5:58
  • I already test but I got the same error message "Session required but was invalid or not found" in the force.com developer console – Jen Apr 29 '16 at 6:48
  • but I try to use postman (google chrome app) to simulate the same URL message and send it to /chat/rest/Chasitor/ChasitorInit. Then I got new message as following: – Jen Apr 29 '16 at 6:51
  • "An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. Error ID: 670331594-10275 (445913155)" – Jen Apr 29 '16 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.