I am trying integrate Slack using Apex Trigger and not by process builder. I figured out a way to get it executed using Trigger and works perfectly for my requirements. However, I am unable to get a proper test coverage and the method is passed as well with no errors.
I am fairly new with this type of integrations. I am sure I am missing something which I am not able to figure out.
I have other methods too which I am calling from Apex Class, but figured if I can cover one method, I will be able to work with others. See below:
Apex Trigger:(50% - Test coverage):
trigger SalesforceToSlackOppTrigger on Opportunity (after insert, after update) {
Id NewRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New').getRecordTypeId();
List<Id> opptyIds = new List<Id>();
if(SalesforceToSlack.isFirstTime) {
for(Opportunity opp:Trigger.new){
SalesforceToSlack.isFirstTime = false;
Account acct = [SELECT Id, CRM_Owner__c FROM Account WHERE Id =: Opp.AccountId];
if((opp.RecordTypeId == NewRecordTypeId && opp.isClosed == true && opp.isWon == True && acct.CRM_Owner__c == null)
&& (opp.OwnerId != '00536000003AUNV' && opp.OwnerId != '00536000001i9UG')){
opptyIds.add(opp.Id);
if(opptyIds.size() > 0){
SalesforceToSlack.OppWonPushToSlack(opptyIds);
}
}
}
}
}
Apex Class (1% - Test coverage):
public class SalesforceToSlack { private static final String OppWonSlackURL = 'WEBHOOK_URL' //Edited out for privacy
public static void OppWonPushToSlack(List opportunityId) {
Id oppId = opportunityId[0]; // If bulk, only post first to avoid overloading Slack channel
Opportunity o = [SELECT Id, Name, CloseDate, Account.Name, Owner.Name
FROM Opportunity
WHERE Id=:oppId];
Map msg = new Map();
msg.put('text', 'Please assign CRM owner to our following new opportunity in salesforce:' +
'\nOpportunity Name: ' + o.Name + '' +
'\nCompany Name: ' + o.Account.Name + '' +
'\nClose Date: ' + o.CloseDate +'' +
'\nOpportunity Owner: ' + o.Owner.Name +'' +
'\nLink: ' + 'https://cs14.salesforce.com/' + o.id);
msg.put('mrkdwn', true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(OppWonSlackURL, 'POST', body));
}
public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
private final String url;
private final String method;
private 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();
if (!Test.isRunningTest()) {
HttpResponse res = http.send(req);
}
}
}
}
Apex Test Class:
@isTest public class SalesforceToSlackTest {
static testMethod void testOppWonPushToSlack() {
Id NewRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New').getRecordTypeId();
Id pricebookId = Test.getStandardPricebookId();
Account acct1 = new Account(Name = 'Test Account 1',
Company_Type__c = 'Trading',
Trading__c = 'Hedge Fund',
CRM_Owner__c = null);
Insert acct1;
Product2 prod = new Product2(Name = 'US',
isActive = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 10000,
IsActive = true
);
insert pbEntry;
Opportunity newOpp = new Opportunity(Name = 'New Opportunity',
CloseDate = System.today(),
StageName = '1. Initiated Evaluation',
Type ='New',
RecordTypeId = NewRecordTypeId,
AccountId = acct1.Id,
Payment_Terms__c = 'Annual',
Contract_Start_Date__c = System.today(),
Contract_End_Date__c = System.today() + 364,
Amount = 10000,
OwnerId = userinfo.getuserid());
Insert newOpp;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = newOpp.Id,
PricebookEntryId = pbEntry.Id,
Quantity = 1,
UnitPrice = pbEntry.UnitPrice
);
Insert oli;
Test.startTest();
newOpp.StageName = 'Contract Signed';
Update newOpp;
Test.stopTest();
Opportunity OppRecord = [select StageName from Opportunity where Id =: newOpp.Id];
System.assertEquals('Contract Signed',OppRecord.StageName);
}
}