0

We have a telephone system integration with salesforce using opencti, and we're seeing an intermittent error when executing a webservice method createNewLead.

Softphone logs:

js:1160 calls Array[1]
js:1160 calls Array[1]
(unknown) 2016-12-12 14:04:31.50 +0800 | segue.sforce | onInboundCall | Call Object
(unknown) 2016-12-12 14:04:31.50 +0800 | segue.sforce | runApex | CreateNewLead Args: Object 
ArgString: callType=[omitted]&numberDialed=[omitted]&callerNumber=[omitted]&username=[omitted]
phone?isdtp=vw&sfdcIFrameOrigin=https%3A%2F%2Fap1.salesforce.com&nonce=60b214a……:1 Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().
https://ap1.salesforce.com/_ui/cti/interaction/RunApexServlet Failed to load resource: the server responded with a status of 500 (Server Error)
(unknown) 2016-12-12 14:04:31.65 +0800 | segue.sforce | runApex | CreateNewLead Response: failed An error occurred while calling the API method.(anonymous function)
(unknown) 2016-12-12 14:04:31.65 +0800 | segue.sforce | runApex | CreateNewLead Response: false
js:36 Mon Dec 12 2016 14:04:41 GMT+0800 (Malay Peninsula Standard Time) | sip.transport | received WebSocket text message:

Here's the web service method in question, which pretty much just creates a new lead on an inbound call:

public class CreateNewLeadResponse
    {
        public boolean success;
        public Error    error;
        public string   leadId;
        public string   screenPopURL;
    }

    webservice static string CreateNewLead( string callType,
                                            string  numberDialed, 
                                            string  callerNumber,
                                            string  username)
    {
        CreateNewLeadResponse response = new CreateNewLeadResponse();

        try
        {
            Lead newLead = new Lead();
            newLead.FirstName           = DEFAULT_CALLER_NAME;
            newLead.LastName            = DEFAULT_CALLER_NAME;
            newLead.Phone               = callerNumber;
            newLead.Origin__c           = 'IVR Created';
            newLead.Call_Type__c        = callType;
            newLead.Bypass_De_duplication_Lead__c   = true;                 // (PC 2016-12-01) Set the Bypass Deduplication Rules checkbox to TRUE

            // first we need to see if the number dialed matches to a mapping that is within the system already
            string landingNumberLookup = numberDialed.replace('[ ()+a-zA-Z]', '');

            for(Landing_Number_Mapping__c s : [
                                                select  id, Offer__c, Affiliate__c 
                                                from    Landing_Number_Mapping__c 
                                                where   Landing_Number_For_Query__c like :'%' + landingNumberLookup + '%'
                                                and     Offer__c != null
                                                and     Affiliate__c != null
                                                order by CreatedDate desc])
            {
                // bingo!
                newLead.Offer__c        = s.Offer__c;
                newLead.Affiliate__c    = s.Affiliate__c;
                newLead.Landing_Number__c   = landingNumberLookup;
                continue;
            }

            if(newLead.Affiliate__c == null)
            {
                newLead.Affiliate__c    = [ select  id 
                                            from    Account 
                                            where   Name = :DEFAULT_AFFILIATE 
                                            limit 1].Id;
            }

            if(newLead.Offer__c == null)
            {
                Landing_Number_Mapping__c s =  [
                                                select  Offer__c 
                                                from    Landing_Number_Mapping__c 
                                                where   Affiliate__c = :newLead.Affiliate__c
                                                and     Offer__c != null
                                                limit 1];

                newLead.Offer__c = s.Offer__c;
            }

            try
            {   
                //username += '%';
                System.debug('username ' + username);
                User owner = [select id from User where IsActive = true AND Segue_username__c =:username];
                newLead.OwnerId = owner.Id;
            }
            catch(Exception e)
            {
                System.Debug('Unable to load CTI User ' + username);
                // could not find the user
            }

            insert newLead;
            System.debug('Lead has been created successfully: ' + newLead);     

            response.leadId         = newLead.id;
            //response.screenPopURL = '/apex/ActivityInboundScreenPop?id='+newLead.id;// Requirements state that there is no different VFPage required. So forwarding to standard lead edit page
            response.screenPopURL   = '/'+newLead.id+'/e';
            response.success        = true;
            response.error          = null;
        }
        catch(exception e)
        {
            response.success    = false;
            response.error      = new Error(e); // add unique id for exception
        }

        return JSON.serialize(response);
    }

The phone system runs the CreateNewLead method fine the vast majority of the time, but certain times of day we're seeing the lead not get created, and if we get to the agent's machine in time, Chrome dev tools console will show the above log. The time of day is making me suspect a bulk issue.

Is there any error handling anyone can recommend to expose more about the cause of this failure, and also to generate an Apex exception email when this happens so we don't have to rely on phone logs through a user console?

  • Are you able to get the full network response when the error happens? Because you're doing runApex followed by a 500 exception it makes me suspicious it may be this known issue success.salesforce.com/issues_view?id=a1p3A000000emsrQAA and the full response might indicate if it is indeed a session issue. – Paul H. Jan 3 '17 at 19:06
1

As an alternative, you could look to use the Open CTI method saveLog() which despite it's name can create any entity.

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.