Case Escalation – Post in Chatter Private Group

I came across a scenario that when a Customer service agent change case status to ‘Escalated’ a group of users should be notified. Though this can be achieved in number of ways in Salesforce, but here I am going to give an example relating to Chatter feed. A  feedItem in Private Group should be posted when case status changes, this Private Group carries users that should be notified.

This works even if a customer service agent is not a member of Private Group, a trigger event is fired when a case status changes to ‘Escalated’, as soon as status changes our code creates a new feedItem with Case Number and URL of that Case,

Lets go through code,

trigger ChatterPostTrigger on Case (after insert, after update) {

//on insert and update only
if (Trigger.isInsert || Trigger.isUpdate){

ID groupId = [Select Id from CollaborationGroup where Name = ‘Demo Private Group’].Id;
List<feedItem> postFeed = new List<feedItem>();

for (Case c : Trigger.new){

if (c.status == ‘Escalated’){

//URL to post in Chatter.
String caseURL = URL.getSalesforceBaseUrl().toExternalForm() + ‘/’ + c.id;

//Create new chatter post
feedItem fi = new feedItem();
fi.Body = ‘Case Number ‘ + c.CaseNumber + ‘ is Escalated, please have a look ‘ + caseURL ;
fi.ParentId = groupId;

postFeed.add(fi);
}
}

//DML to create postFeed in Private Chatter Group
if (!postFeed.isEmpty())
insert postFeed;
}
}

Test Class

@isTest(seeAllData = true)
private class ChatterPostTest{

static testmethod void feedtestMethod(){

ID groupID = [Select Id from CollaborationGroup where Name = ‘Demo Private Group’].Id;

Case c = new Case();
c.origin = ‘Email’;
c.status = ‘New’;

insert c;

String caseURL = URL.getSalesforceBaseUrl().toExternalForm() + ‘/’ + c.id;

Case updateCase = [Select Id, CaseNumber, status from case where Id =: c.Id];
updateCase.status = ‘Escalated’;

test.StartTest();
update updateCase;
test.StopTest();

FeedItem f = [Select body from FeedItem where ParentId =: groupID order By createdDate DESC Limit 1];

System.assertEquals (f.body,’Case Number ‘ + updateCase.CaseNumber + ‘ is Escalated, please have a look ‘ + caseURL);

}

}

Running Apex in Execute Anonymous using SOAP UI

Use the below link to login to salesforce using command line to get session Id and instance URL

http://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_quickstart_login.htm

Now, passing sessionId and instance Url in Apex Request SOAP UI

Endpoint: https://instance URL

Request:

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:apex=”http://soap.sforce.com/2006/08/apex”&gt;
<soapenv:Header>
<apex:DebuggingHeader>
<apex:debugLevel>Detail</apex:debugLevel>
</apex:DebuggingHeader>
<apex:SessionHeader>
<apex:sessionId>00DJ00…DtMVE_udPqUPUN7x64wIftsb3TDMvydO.</apex:sessionId>
</apex:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<apex:executeAnonymous>
<apex:String>
Account a = new Account(Name=’Test Account’, phone=’111222111′);
insert a;
System.debug(‘New Account Name ‘ + a.Name);</apex:String>
</apex:executeAnonymous>
</soapenv:Body>
</soapenv:Envelope>

You write your apex code in between <apex:string></apex:string> tags, in this example I have created an account in my salesforce org using this request from SOAP UI.

How to throw an Exception in Apex

I was receiving an error “Type cannot be constructed: Exception” in my test class while throwing an exception. So below is my test class code with error.

 

@isTest(seeAllData = false) 
public with sharing class MyClass_Test {
       private static testmethod void loggerTest(){
                Test.startTest();
                try {
                         throw new Exception('testing');
                     }catch (Exception ex) {
                          MyClass.myMethod(ex);
                     }
                Test.stopTest();
       }
}

 

The solution was very simple, just created my custom Exception class that extends Exception class. Below is the error free code.

New Class

public class MyException extends Exception{}

 

Test Class updated

 

@isTest(seeAllData = false) 
public with sharing class MyClass_Test {
       private static testmethod void loggerTest(){
                Test.startTest();
                try {
                         throw new MyException('testing');
                     }catch (Exception ex) {
                          MyClass.myMethod(ex);
                     }
                Test.stopTest();
       }
}

 

Hope this helps someone.

 

Thanks