0

I've implemented a call center in Salesforce with Twilio client JavaScript SDK. I'm trying to save the call record information in Salesforce and I'm using the connection.parameters.CallSid to identify the correct record when the recording call back fires. However my CallSid in client side is getting changed automatically sometimes to a different format and hence the recording call back function can't find the Salesforce end record to update it with the RecordingUrl. Have anyone experienced this before or appreciate any guidance.

Below is my JavaScript code. To be more specific, in the startCall function console.log print the CallSid correctly but when goes to saveLog function it's having a different value with a different format and hence the saved record having an incorrect value.

<script type="text/javascript">
    Twilio.Device.setup("{! token }");
    var callerSid;  //  hold the Twilio generated CallSid unique to this call
    var parentId;   //  hold the parent record being called in order to associate as the parent of task being logged for the call
    var newTaskId;  // hold the id of newly created task

    //  function to fire when click 2 dial executes
    var startCall = function (payload) {
        sforce.opencti.setSoftphonePanelVisibility({visible: true});  //pop up CTI softphone
        parentId = payload.recordId;    //  the record that the phone number being called belongs to
        var cleanednumber = cleanFormatting(payload.number);             
        params = {"PhoneNumber": cleanednumber};
        var connection = Twilio.Device.connect(params);
        callerSid = connection.parameters;  //  track the unique Twilio CallSid
        console.log('clk2dial : ', callerSid.CallSid); **// here it logs correcly as CAc57d05994cd69498e0353a5f4b07f2dc**
        setTimeout(function(){
            saveLog();  //  save the call information in a Task record
            }, 2000
        );
    };

    //OpenCTI!!
    sforce.opencti.enableClickToDial();
    sforce.opencti.onClickToDial({listener : startCall});   //  click 2 dial           

    function cleanFormatting(number) { 
        //changes a SFDC formatted US number, which would be 415-555-1212  into a twilio understanble number 4155551212      
        return number.replace(' ','').replace('-','').replace('(','').replace(')','').replace('+','');
    }

    //  save the call information in a Task record
    function saveLog() {
        var keyPrefix;
        var taskToSave;
        console.log('callerSid.CallSid : ', callerSid.CallSid); **// surprisingly here it logs as TJSce253eb4-c2a0-47f3-957f-8178e95162aa**
        if(parentId != null) {
            keyPrefix = parentId.slice(0,3);
        }
        if(keyPrefix != null && keyPrefix == '003') {
            taskToSave = {WhoId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};
        } else {
            taskToSave = {WhatId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};   
        }

        sforce.opencti.saveLog({value:taskToSave, callback: saveLogCallBack});        

    }

    //  call back function for saving the call information in a Task record
    var saveLogCallBack = function(response) {
        if(response.success) {
            newTaskId = response.returnValue.recordId;
            console.log('save success! : ', newTaskId);
        } else {
            console.error('Error saving : ', response.errors);   
        }
    }

    </script>
highfive
  • 628
  • 3
  • 12
  • 31

1 Answers1

0

Answering my own question as I got through this. I registered a function for Twilio.Device.connect and in the call back function retrieved the CallSid. Along with that I've updated my click 2 dial function as well accordigly as below. However I was unable to find this approach in Twilio documentation and any comments are welcome.

//  function to fire when click 2 dial executes
var startCall = function (payload) {
    sforce.opencti.setSoftphonePanelVisibility({visible: true});  //pop up CTI softphone
    parentId = payload.recordId;    //  the record that the phone number being called belongs to

    var cleanednumber = cleanFormatting(payload.number);             
    params = {"PhoneNumber": cleanednumber};
    Twilio.Device.connect(params);
};

//OpenCTI!!
sforce.opencti.enableClickToDial();
sforce.opencti.onClickToDial({listener : startCall});   //  click 2 dial        

// registered a function for Twilio Device connect
Twilio.Device.connect(function(response) {
    callSid = response.parameters;  //  track the unique Twilio CallSid
    // nothing change in save function so not posting again
    saveLog();  //  save the call information in a Task record
});
highfive
  • 628
  • 3
  • 12
  • 31