• Hormoz Hekmat 7
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I wrote a 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);
        }
 
    }
    
}
Test class 
@isTest
private class SlackOpportunityTest {
    

static testMethod void testpostToSlack() {
SlackOpportunityPush.Oppty opt = new SlackOpportunityPush.Oppty();
opt.opptyName = 'Unit Test Opt';
opt.Owner = 'Unit Test Owner';


List<SlackOpportunityPush.oppty> lis = new List<SlackOpportunityPush.oppty>();
lis.add(opt);
SlackOpportunityPush.postToSlack(lis);

System.assertEquals(lis, lis); 
    
}
     
}

Code coverage is now 100% but while deploying the classes I get error:

​Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null
I wrote a 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);
        }
 
    }
    
}
Test class 
@isTest
private class SlackOpportunityTest {
    

static testMethod void testpostToSlack() {
SlackOpportunityPush.Oppty opt = new SlackOpportunityPush.Oppty();
opt.opptyName = 'Unit Test Opt';
opt.Owner = 'Unit Test Owner';


List<SlackOpportunityPush.oppty> lis = new List<SlackOpportunityPush.oppty>();
lis.add(opt);
SlackOpportunityPush.postToSlack(lis);

System.assertEquals(lis, lis); 
    
}
     
}

Code coverage is now 100% but while deploying the classes I get error:

​Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null