Twilio Client CallSid Getting Changed in JavaScript





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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>









share|improve this question





























    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>









    share|improve this question

























      0












      0








      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>









      share|improve this question














      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>






      javascript twilio twilio-click-to-call






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 19:27









      highfivehighfive

      42821030




      42821030
























          1 Answer
          1






          active

          oldest

          votes


















          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
          });





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452043%2ftwilio-client-callsid-getting-changed-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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
            });





            share|improve this answer






























              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
              });





              share|improve this answer




























                0












                0








                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
                });





                share|improve this answer















                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
                });






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 25 '18 at 15:39

























                answered Nov 25 '18 at 15:32









                highfivehighfive

                42821030




                42821030
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452043%2ftwilio-client-callsid-getting-changed-in-javascript%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()