You need to sign in to do that
Don't have an account?
Daniel Wretman
Call out from triggers not supported
Hi
I am setting up a trigger and triggerHandler to send a post callout request, but I have issues with the code coverage. I do have the mocked http and do have a calloutClass and with it a calloutClassTest. this is working and give me the code coverage I need. My problem is with the handler class, it is initiated by a trigger so I can't call the true callout (named OPSImSendRequest) class. I have instead put a if statement on the callout to check if it is a test, if so I skip that part but doing that messes up my code coverage.
I am a bit lost on this, my code below:
public class OPSImSendRequest {
public static HttpRequest sendRequest(map<string,string> data){ //was public static void,HttpRequest
HttpRequest req = new HttpRequest();
req.setEndpoint('https://alert.xxx.com/integrations/generic/20131114/alert/$YOUR_API_KEY_HERE/$ROUTING_KEY_HERE'); //not set to proper values yet.
req.setMethod('POST');
String body = JSON.serialize(data);
req.setBody(body);
req.setHeader('Content-Type','application/json;charset=UTF-8');
HttpResponse res = new Http().send(req);
return req;
}
}
@isTest
public class CalloutClassTest {
@isTest
public static void testPostCallout() {
Test.setMock(HttpCalloutMock.class, new MockOPSImSendRequest());
Map<String, String> data = new Map<String, String>();
data.put('id','Id');
data.put('salesforce_id','salesforce_id');
data.put('event_type','event_type');
data.put('acknowledge_by','acknowledge_by');
data.put('timestamp','timeStamp');
test.startTest();
HttpRequest response = OPSImSendRequest.sendRequest(data);
test.stopTest();
}
}
@isTest
global class MockOPSImSendRequest implements HttpCalloutMock{
global HTTPResponse respond (HTTPRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setStatus('OK');
response.setStatusCode(200);
return response;
}
}
trigger opsIMEvents on Case (after insert, after update) {
if(trigger.isUpdate && triggerIterationController.isFirstTime){
for(case c : trigger.new){
if(c.Status == 'CLOSED' && c.Record_type__c =='Incident_Cases' && c.Origin_Event_case__c != null){
opsIMEventsTriggerHandler.caseClosedIncident(c.id, c.CaseNumber, c.LastModifiedById, c.Last_Status_Change__c, c.Origin_Event_case__c);
triggerIterationController.isFirstTime = false;
}
public class opsIMEventsTriggerHandler {
@future(callout=true)
public static void caseRoutedIncident(Id incidentId, String incidentCaseNumber, String newCaseQueue, DateTime lastStatusChange, Id originEventId){
String Id = originEventId;
String salesforce_id=(String)incidentId;
String event_type='case_routed';
String sf_case_no=incidentCaseNumber;
String routed_to=newCaseQueue;
String timeStamp=string.valueOfGMT(lastStatusChange);
Map<String, String> data = new Map<String, String>();
data.put('id',id);
data.put('salesforce_id',salesforce_id);
data.put('event_type',event_type);
data.put('sf_case_no',sf_case_no);
data.put('routed_to',routed_to);
data.put('timestamp',timeStamp);
if (!Test.isRunningTest()){
System.debug('is not a test: ');
OPSImSendRequest.sendRequest(data);
}else{
System.debug('this seem to be a test on RoutedIncident');
System.debug('routed incident, data map to send to victorops: '+data);
}
}
}
I am setting up a trigger and triggerHandler to send a post callout request, but I have issues with the code coverage. I do have the mocked http and do have a calloutClass and with it a calloutClassTest. this is working and give me the code coverage I need. My problem is with the handler class, it is initiated by a trigger so I can't call the true callout (named OPSImSendRequest) class. I have instead put a if statement on the callout to check if it is a test, if so I skip that part but doing that messes up my code coverage.
I am a bit lost on this, my code below:
public class OPSImSendRequest {
public static HttpRequest sendRequest(map<string,string> data){ //was public static void,HttpRequest
HttpRequest req = new HttpRequest();
req.setEndpoint('https://alert.xxx.com/integrations/generic/20131114/alert/$YOUR_API_KEY_HERE/$ROUTING_KEY_HERE'); //not set to proper values yet.
req.setMethod('POST');
String body = JSON.serialize(data);
req.setBody(body);
req.setHeader('Content-Type','application/json;charset=UTF-8');
HttpResponse res = new Http().send(req);
return req;
}
}
@isTest
public class CalloutClassTest {
@isTest
public static void testPostCallout() {
Test.setMock(HttpCalloutMock.class, new MockOPSImSendRequest());
Map<String, String> data = new Map<String, String>();
data.put('id','Id');
data.put('salesforce_id','salesforce_id');
data.put('event_type','event_type');
data.put('acknowledge_by','acknowledge_by');
data.put('timestamp','timeStamp');
test.startTest();
HttpRequest response = OPSImSendRequest.sendRequest(data);
test.stopTest();
}
}
@isTest
global class MockOPSImSendRequest implements HttpCalloutMock{
global HTTPResponse respond (HTTPRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setStatus('OK');
response.setStatusCode(200);
return response;
}
}
trigger opsIMEvents on Case (after insert, after update) {
if(trigger.isUpdate && triggerIterationController.isFirstTime){
for(case c : trigger.new){
if(c.Status == 'CLOSED' && c.Record_type__c =='Incident_Cases' && c.Origin_Event_case__c != null){
opsIMEventsTriggerHandler.caseClosedIncident(c.id, c.CaseNumber, c.LastModifiedById, c.Last_Status_Change__c, c.Origin_Event_case__c);
triggerIterationController.isFirstTime = false;
}
public class opsIMEventsTriggerHandler {
@future(callout=true)
public static void caseRoutedIncident(Id incidentId, String incidentCaseNumber, String newCaseQueue, DateTime lastStatusChange, Id originEventId){
String Id = originEventId;
String salesforce_id=(String)incidentId;
String event_type='case_routed';
String sf_case_no=incidentCaseNumber;
String routed_to=newCaseQueue;
String timeStamp=string.valueOfGMT(lastStatusChange);
Map<String, String> data = new Map<String, String>();
data.put('id',id);
data.put('salesforce_id',salesforce_id);
data.put('event_type',event_type);
data.put('sf_case_no',sf_case_no);
data.put('routed_to',routed_to);
data.put('timestamp',timeStamp);
if (!Test.isRunningTest()){
System.debug('is not a test: ');
OPSImSendRequest.sendRequest(data);
}else{
System.debug('this seem to be a test on RoutedIncident');
System.debug('routed incident, data map to send to victorops: '+data);
}
}
}
Your query is answered in the below official article,
https://help.salesforce.com/articleView?id=000330691&type=1&mode=1
If this helps mark it as the best answer.
Thank you for your answer, but it does not anwer my question. I already have encapsulated the callout in the trigger with the @future method, it is in the opsIMEventsTriggerHandler class.
The issue I have is that i have one test class for the callout class and another for the trigger. I seem not able to incorporate the callout tests in the trigger test class so my code coverage never reaches higher than 60%.
Best regards
Daniel