You need to sign in to do that
Don't have an account?
<Learning Apex
Api call class code coverage increase
I am making an API call out to Slack.com and have written a class that send's message' to slack users when an Opportunity is closed-won.
Run all tests and the code coverage is 60%, 11/18 lines covered.
What best can be done here to improve the coverage ?
I am new to Apex, any help will be highly appreciated.
Below is the class:
public with sharing class SlackOpportunityPush {
// To publish Opportunity values in form of a String//
public static final String slackURL = 'https://hooks.slack.com/services/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';
public class Oppty {
@InvocableVariable(label='Opportunity Name')
public String opptyName;
@InvocableVariable(label='Owner')
public String Owner;
@InvocableVariable(label='Opportunity Amount')
public String Amount;
@InvocableVariable(label='Account Development Owner')
public String AccountDE;
@InvocableVariable(label='Account')
public String Account;
}
@InvocableMethod(label='Push to Slack')
public static void postToSlack(List<Oppty> oppties) {
Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', '\n Opportunity Won ! Congrats to Opportunity Owner! :- ' +o.Owner + '\n and Account Development Owner:- '+ o.AccountDE + '\n who just closed Opportunity:- ' +o.opptyName + '\n associated to Account:- ' + o.Account +'\n for Opportunity Amount:- $ ' + o.Amount);
System.debug('Called msg.put');
System.debug('OpportunityOwner '+o.Owner);
System.debug('OpportunityAccount '+o.Account);
System.debug('OpportunityAmount '+o.Amount);
msg.put('mrkdwn',true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(slackURL,'POST',body));
}
public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
public final String url;
public final String method;
public final String body;
public QueueableSlackCall(String url,String method,String body) {
this.url = url;
this.method = method;
this.body = body;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
HttpResponse res = http.send(req);
}
}
}
Run all tests and the code coverage is 60%, 11/18 lines covered.
What best can be done here to improve the coverage ?
I am new to Apex, any help will be highly appreciated.
Below is the class:
public with sharing class SlackOpportunityPush {
// To publish Opportunity values in form of a String//
public static final String slackURL = 'https://hooks.slack.com/services/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';
public class Oppty {
@InvocableVariable(label='Opportunity Name')
public String opptyName;
@InvocableVariable(label='Owner')
public String Owner;
@InvocableVariable(label='Opportunity Amount')
public String Amount;
@InvocableVariable(label='Account Development Owner')
public String AccountDE;
@InvocableVariable(label='Account')
public String Account;
}
@InvocableMethod(label='Push to Slack')
public static void postToSlack(List<Oppty> oppties) {
Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', '\n Opportunity Won ! Congrats to Opportunity Owner! :- ' +o.Owner + '\n and Account Development Owner:- '+ o.AccountDE + '\n who just closed Opportunity:- ' +o.opptyName + '\n associated to Account:- ' + o.Account +'\n for Opportunity Amount:- $ ' + o.Amount);
System.debug('Called msg.put');
System.debug('OpportunityOwner '+o.Owner);
System.debug('OpportunityAccount '+o.Account);
System.debug('OpportunityAmount '+o.Amount);
msg.put('mrkdwn',true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(slackURL,'POST',body));
}
public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
public final String url;
public final String method;
public final String body;
public QueueableSlackCall(String url,String method,String body) {
this.url = url;
this.method = method;
this.body = body;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
HttpResponse res = http.send(req);
}
}
}
Hari_Apex
Hari_Apex
This test class will cover your code 100%.