• <Learning Apex
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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);
        }
     }
}    
 
iam getting an error :could not run tests on class because invalid type:baseConfigController
apex trigger:
===============================
trigger test on Case (before update )
{
  Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();

  case caseold=trigger.old[0];
  For (Case cs : Trigger.new)
  {
    for (string fieldName : caseFields.keySet())
     {
       
        if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
        {
           // cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to '  +string.valueOf(cs.get(fieldName));
             string oldvalue = string.valueOf(caseold.get(fieldName));
            
             string newvalue= string.valueOf(cs.get(fieldName));
           
             cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
         
             break;
          }   
       }
  }
}
================================test class============================================
@isTest
public class Test_casehistory
{

    public static testmethod void m1()
    {
     Case cs=new case();
     cs.Origin='Web';
     cs.Reason='other';
     cs.Priority='Medium';
     cs.Status='New';
     cs.Type='Other';

     test.startTest();
     list<case> lTest1=new list<case>();
     lTest1 = [SELECT Id,Origin,Reason,Priority,Status,Type  FROM Case WHERE Id=:cs.Id];
     update lTest1;
     test.stopTest();
    }
}