Implementation

To create a new Apex class, access Setup in SalesForce, and type in "Apex" in the quick search box. 

Select "Build ... Develop ... Apex Classes".

Create a new class. Add the following code:

Sample Apex writeCallStatistics
@RestResource(urlMapping='/jtelACD/writeCallStatistics')
global with sharing class JTELACD_WriteCallStatistics {
     
     
    public static DateTime convertISO8601( String iso8601_ts ) {
        DateTime dt = (DateTime) JSON.deserialize( '"' + iso8601_ts + '"', DateTime.class );
        return DateTime.newInstance( dt.getTime() );
        // NOTE: return dt does not work!
    }     
    
    public class JTELCallStatistics {
        public String SalesForceID;
        public String AgentUID;
        public Integer bOutbound;
        public String CallerID;
        public String ServiceNumber;
        public String ServiceName;
        public String AcdAgentGroupsName;
        public String AcdConfigurationGroupsName;
        public String dtCallStart;
        public String dtCallAlert;
        public String dtCallConnected;
        public String dtCallEnd;
        public Integer nDuration;
        public Integer CONNRES;
        public Integer Cause;
        public String TransactionCodeExportKey;
    }
     
    @HttpPost
    global static String doPost() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
 
        try {
            // Deserialize the JSON
            JTELCallStatistics callStatistics = (JTELCallStatistics) System.JSON.deserialize( req.requestBody.toString(), JTELCallStatistics.class );
             
            // Create a new task, and full some of the fields
            if( ( callStatistics.CONNRES == 1 ) ) {
                Datetime dtConnected          = convertISO8601( callStatistics.dtCallConnected );
                Task task                     = new Task();
                task.ActivityDate             = dtConnected.date();
                task.CallDisposition          = 'Completed';
                task.Subject                  = 'Call with Agent ' + callStatistics.AgentUID;
                task.Description              = 'Agent Group ' + callStatistics.AcdAgentGroupsName + '\r\nConfiguration ' + callStatistics.AcdConfigurationGroupsName;
                task.CallType                 = callStatistics.bOutbound <> 0 ? 'Outbound' : 'Inbound';
                task.CallDurationInSeconds    = callStatistics.nDuration;
                task.Status                   = 'Completed';
                task.Type                     = 'Call';
         
                // Link to sales force record
                task.WhoId                    = callStatistics.SalesForceID;
             
                // Insert
                insert task;
            }
        }
        catch( Exception e ) {
            res.statusCode = 500; // Internal server error
            return e.getMessage();
        }
 
        // All OK
        res.statusCode = 200;
        return null;
    }
}

CURL Test

First of all, obtain an OAUTH Token, see Testing with CURL.

The following CURL command can be used to test this API:

CURL - writeCallStatistics Test
curl --silent -i -X POST -d '{ "SalesForceID" : "<SalesForceRecordId>", "AgentUID" : "test.agent@example.com", "bOutbound" : 0, "CallerID" : "4989461495011", "ServiceNumber" : "49800123456", "ServiceName" : "Test Service", "AcdAgentGroupsName" : "Sales", "AcdConfigurationGroupsName" : "Sales", "dtCallStart" : "2018-03-16T16:45:00", "dtCallAlert" : "2018-03-16T16:45:02", "dtCallConnected" : "2018-03-16T16:45:10", "dtCallEnd" : "2018-03-16T16:50:10", "nDuration" : 300, "CONNRES" : 1, "Cause" : 16, "TransactionCodeExportKey" : "Offer Made" }' --header "Content-Type: application/json" --header "Authorization: Bearer <OAUTH_TOKEN>" --header "Connection: Close" "https://<SALES_FORCE_INSTANCE_URL>/services/apexrest/jtelACD/writeCallStatistics"
  • No labels