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
Anoop RbAnoop Rb 

any body help me how to cover catch() block in this code..?

any body help me how to cover catch() block in this code..? i am just covering try block only. How to cover catch block please help me.
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) {
            String subjectText = 'Case Escalation exception in site ' + Site.getName();
            String bodyText = 'Case Escalation on Question having ID: ' + q.Id + ' has failed with the following message: ' + e.getMessage() +
                '\n\nStacktrace: ' + e.getStacktraceString();

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] { Site.getAdminEmail() };

            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(toAddresses);
            mail.setSubject(subjectText);
            mail.setPlainTextBody(bodyText);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Just set an Id to your sObject newCase. It will probably throw an String or DML exception:
Case newCase = new Case(Id = 'test' , 
						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
					);
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
Anoop RbAnoop Rb
No use boss still we are unable to cover catch block. Please Give me another idea.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Oh, my bad! It was a silly suggestion to make. I can't figure out right now a way to throw that exception, perhaps, that exception may never happen. Moreover, I've just noticed now that you are querying inside a FOR loop which is highly dangerous as you can easily hit Salesforce's limits what will gives you a splitting headache. I highly recommend you to take out of you loop that statement. Find out more about in: 

https://developer.salesforce.com/page/Apex_Code_Best_Practices (https://developer.salesforce.com/page/Apex_Code_Best_Practices" target="_blank)

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Anoop RbAnoop Rb
that is the only reason ..?
Anoop RbAnoop Rb
Hello this is my test class please do the modifications if needed.

@isTest(SeeAllData=true)
private class ChatterAnswersEscalationTriggerTest {
// Method -1
    static testMethod void validateQuestionEscalation() {
        String questionTitle = 'questionTitle';
        String questionBody = 'questionBody';
        Community[] c = [SELECT Id from Community WHERE isActive = true AND NetworkId != NULL];
        // We cannot create a question without a community
        if (c.size() == 0) { return; }
        String communityId = c[0].Id;
        Question q = new Question();
        q.Title = questionTitle;
        q.Body = questionBody;
        q.CommunityId = communityId;
        insert(q);
        q.Priority = 'high';
        update(q);
        Case ca = [SELECT Origin, CommunityId, Subject, Description from Case where QuestionId =: q.Id];
        // Test that escaltion trigger correctly escalate the question to a case
        System.assertEquals(questionTitle, ca.Subject);
        System.assertEquals(questionBody, ca.Description);
        System.assertEquals('Chatter Answers', ca.Origin);
        System.assertEquals(communityId, ca.CommunityId);
    }
    // Method -2
    static testMethod void validateQuestionEscalation1() {
   
        try{
            Community[] c = [SELECT Id from Community WHERE isActive != false AND NetworkId = NULL];    
            Question q = new Question();
        q.Title ='Test' ;
        q.Body = 'test';
        q.CommunityId = c[0].Id;
        insert q;
             q.Priority='High';
            update q;  
                        /*Case newCase = new Case( 
                        Origin='Chatter Answers',
                        Id='Test',
                        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
                    );*/
            
            Case newCase = new Case(Id = 'test' , 
                        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){
            
        }

           
                
        
    }
}