function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

class showing 0% though it has all criteria

Hi, 

  Below is the trigger written 
trigger chatter_answers_question_escalation_to_case_trigger on Question (after update) {
    for (Question q: Trigger.new) {
        try {
            if (q.Priority == 'high' && (q.Cases == null || q.Cases.size() == 0) && Trigger.oldMap.get(q.id).Priority != 'high') {
                q = [select Id, Title, Body, CommunityId, createdById, createdBy.AccountId, createdBy.ContactId from Question where Id = :q.Id];
                Case newCase = new Case(Origin='Chatter Answers', OwnerId=q.CreatedById, QuestionId=q.Id, CommunityId=q.CommunityId, Subject=q.Title, Description = (q.Body == null? null: q.Body.stripHtmlTags()), AccountId=q.CreatedBy.AccountId, ContactId=q.CreatedBy.ContactId);
                insert newCase;
            }
        } catch (Exception e) {
            //In case you have issues with code coverage for this section, you can move this to a separate helper class method which can be tested separately
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setReplyTo('no-reply@salesforce.com');
            mail.setSenderDisplayName('Salesforce Chatter Answers User');

            // The default sender is the portal user causing this trigger to run, to change this, set an organization-wide address for
            // the portal user profile, and set the ID in the following line.
            // mail.setOrgWideEmailAddressId(orgWideEmailAddressId);
            mail.setToAddresses(new String[] { Site.getAdminEmail() });
            mail.setSubject('Case Escalation exception in site ' + Site.getName());
            mail.setPlainTextBody('Case Escalation on Question having ID: ' + q.Id + ' has failed with the following message: ' + e.getMessage() + '\n\nStacktrace: ' + e.getStacktraceString());
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}
Test class for trigger not sure what is missing it is still giving 0% code coverage. Please suggest me how to fix this issue. 
@isTest(seealldata=true)
public class  test_chatter_answers_question
{
 static testMethod void test_chatter_answers_question(){
 	
 	
 Question  q = new Question(Title='test', Body='test', CommunityId='09a800000000w1dAAA', createdById='00580000007lJUbAAM');
 
 insert q;
 
 Case newCase = new Case(Origin='Chatter Answers', OwnerId=q.CreatedById, QuestionId=q.Id, CommunityId=q.CommunityId, Subject=q.Title, Description = (q.Body == null? null: q.Body.stripHtmlTags()), AccountId=q.CreatedBy.AccountId, ContactId=q.CreatedBy.ContactId);
                
 insert newCase;
 }
}

Thanks
Sudhir
Amit Chaudhary 8Amit Chaudhary 8
Please below test class.
@isTest()
public class  test_chatter_answers_question
{
	 static testMethod void test_chatter_answers_question()
	 {

		Question  q = new Question();
	 	q.Title='test';
	  	q.Body='test';
		q.Priority ='low';
		//q.CommunityId = ''; please query and add
		insert q;
		 
		q.Priority ='high';
		update q;	
		
	 }
	 
}
Let us know if this will help you

Thanks
Amit Chaudhary
 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Amit for your reply how to handle the catch block in test class code coverage is 42% now
Amit Chaudhary 8Amit Chaudhary 8

As mention in your Trigger
//In case you have issues with code coverage for try catch, you can move this to a separate helper class method which can be tested separately

Please do need full and try above test class