3

Hello I am developing an iOS app allowing to chat with a service agent using live agent. The app is using Live Agent REST API.

Everything was working fine till I added prechatDetails to the ChasitorInit. When I populate prechatDetails property in the request, I get a response with a 500 error:

{ URL: https://d.la1-c1-lon.salesforceliveagent.com/chat/rest/Chasitor/ChasitorInit } { status code: 500, headers {
    "Access-Control-Allow-Credentials" = true;
    "Access-Control-Allow-Origin" = "*";
    "Cache-Control" = "no-cache";
    "Content-Encoding" = gzip;
    "Content-Length" = 319;
    "Content-Type" = "application/json";
    Expires = "-1";
    Pragma = "no-cache";
    "X-Cnection" = close;
} }

My code is:

let prechatDetails: [[String : AnyObject]] = [
                ["label": "First Name", "value": userData[RegistrationViewController.Keys.FirstName]!, "transcriptFields": ["FirstName__c"], "displayToAgent": true],
                ["label": "Last Name", "value": userData[RegistrationViewController.Keys.LastName]!, "transcriptFields": ["LastName__c"], "displayToAgent": true]
            ]

            let jsonBody : [String : AnyObject] = [
                JSONRequestBodyKeys.OrganizationId: AppDelegate.OrganizationId,
                JSONRequestBodyKeys.DeploymentId: AppDelegate.DeploymentId,
                JSONRequestBodyKeys.ButtonId: discussion.chatButton.buttonId,
                JSONRequestBodyKeys.SessionId: discussion.sessionId,
                JSONRequestBodyKeys.VisitorName: userData[RegistrationViewController.Keys.FirstName]!,
                JSONRequestBodyKeys.ReceiveQueueUpdates: true as Bool,
                JSONRequestBodyKeys.IsPost: true as Bool,

                "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36",
                "language": "en-US",
                "screenResolution": "2560x1440",
                "prechatDetails": prechatDetails,
                "prechatEntities": []

            ]
            do {
                request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(jsonBody, options: .PrettyPrinted)
            }

If I replace the first line by prechatDetails: [[String : AnyObject]] = [], everything works fine. It does not seems related to the JSON as a wrong JSON returns a 400 error.

Is anybody able to help? I am out of idea to solve this.

Thanks in advance,

Alex

0

NSJSONSerialization is an Objective-C API that you are calling into from Swift. That's okay, but you should not expect it to properly serialize Swift objects. You should replace Swift's Bool type with NSNumber(bool:), double-check that your other values have Obj-C bridged equivalents (is userData[RegistrationViewController.Keys.LastName]! always a String? Are you sure?), and be aware that you're dancing madly on the lip of a volcano as long as you're force-unwrapping optionals all over the place in your serialization code.

Edit: Does this sound like an iOS user agent to you? 🙃

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"

Edit 2: I would also like an iOS device with screen resolution 2560x1440, while you're at it!

2
  • Hi Jonathan, thank you for your answer. I am sorry I know my code is bad, I am still learning swift and was trying to make the connection work before cleaning up the code. – Alex May 13 '16 at 18:09
  • The JSON is correct as salesforce sends a 400 error if the JSON is not. But I found here that a component called "entityMaps" was missing. It is not in the documentation. The prechatDetails should look like this: ["label": "First Name", "value": "alex", "entityMaps": [["entityName": "Contact", "fieldName": "FirstName"]], "transcriptFields": ["FirstName__c"], "displayToAgent": true] – Alex May 13 '16 at 18:15

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.