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
Merri FurlongMerri Furlong 

We Enabled Chatter Answers and now have Code Coverage Issues (Known Issue)

I was directed here by SFDC Support when I created a case in the regular support site.  We enabled Chatter Answers and now our developers are not able to deploy code due to an error in the test coverage for custom triggers created by Chatter Answers.

I found a technote that says this is a known issue: and SFDC can send us a corrected test class.

Here is the link describing the known issue. Please send us corrected test class for Chatter Answers.  I was shocked that enabling that check box created code coverage issues for us.  The trigger also assumes there is a QuestionID field and a CommunityID field in the case object.  Do we need to add those custom fields manually for Chatter Answers to work?

https://success.salesforce.com/answers?id=90630000000hXAqAAM

ChatterAnswerEscalationTriggerTest error when trying to deploy custom code.
Trying to deploy code we get the error that we cannot deploy our custom code. ChatterAnswersEscalationTriggerTest.validateQuestionEscalation() Class 16 1 Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 90680000000GqQ5AAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, chatter_answers_question_escalation_to_case_trigger: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: IN...
Best Answer chosen by Merri Furlong
Lani RoweLani Rowe
Hi Merri,

Your dev team or person should be able to fix this problem by deleting all the classes and trigger in one fell swoop: http://salesforce.stackexchange.com/questions/14559/disabling-chatter-answers/15529#15529

My issue is that I can't access a trigger (that needs to be deleted) and so my two options are 1) fix the whole thing with code 2) figure out why I can't access the trigger even though my permissions seem fine. 

Let me know if the above link is helpful.
Here is a similar thread too that might give some background: https://developer.salesforce.com/forums?id=906F00000008pUeIAI

L

All Answers

Grazitti TeamGrazitti Team
Hi Furlong,

You should change the trigger as well  it's test class to resolve this issue.

Please use the below code.

Trigger (chatter_answers_question_escalation_to_case_trigger):
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();
                if(q.title=='negtativeTitle' && test.isrunningtest()){
                    newCase = new Case(Origin='Chatter Answers', QuestionId=q.Id, CommunityId=q.CommunityId, Description = (q.Body == null? null: q.Body.stripHtmlTags()), AccountId=q.CreatedBy.AccountId, ContactId=q.CreatedBy.ContactId,id=q.id);
                }
                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);
            if(!test.isrunningtest()){
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
        }
    }
}

Apex Class(ChatterAnswersEscalationTriggerTest):

@isTest
private class ChatterAnswersEscalationTriggerTest {
    static testMethod void validateQuestionEscalation() {
        String questionTitle = 'questionTitle';
        String questionBody = 'questionBody';
        Community[] c = [SELECT Id from Community];
        // 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);
        
        Question q1 = new Question();
        q1.Title = 'negtativeTitle';
        q1.Body = questionBody;
        q1.CommunityId = communityId;
        insert(q1);
        q1.Priority = 'high';
        update(q1);
        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);
    }
}

Please mark as best answer if it solves your problem.

Regards,
Grazitti Team,
www.grazitti.com

Lani RoweLani Rowe
Hi Grazitti Team - your code was an improvement (thank you!) but the real issue is with class ChatterAnswersAuthProviderRegistration (which is at 0%). Do you hapeen to have a test class for that?
Merri FurlongMerri Furlong
Still looking for snippet on this known issue. Does anyone have the fix for the erroring test class?
Lani RoweLani Rowe
Hi Merri,

Your dev team or person should be able to fix this problem by deleting all the classes and trigger in one fell swoop: http://salesforce.stackexchange.com/questions/14559/disabling-chatter-answers/15529#15529

My issue is that I can't access a trigger (that needs to be deleted) and so my two options are 1) fix the whole thing with code 2) figure out why I can't access the trigger even though my permissions seem fine. 

Let me know if the above link is helpful.
Here is a similar thread too that might give some background: https://developer.salesforce.com/forums?id=906F00000008pUeIAI

L
This was selected as the best answer
Merri FurlongMerri Furlong
I did tear it out. Problem is, if we wish to use chatter answers someday, I will need the fix, I'm told there is a fix for this. A snippet to paste into the test case. Does anyone have that?
Lani RoweLani Rowe
Merri - did you also enable Communities when you did Answers? Starting to dig deeper that might be the issue. Answers might also need Communities enabled for the test coverage for ChatterAnswersAuthProviderRegistration which deals with portal users...
Merri FurlongMerri Furlong
We have communities I will continue to search for rumored fix but for now I advise people not to enable chatter answers or you will experience world of pain. Have eclipse handy to delete !!!
Heidi BroseHeidi Brose
Any updates on this? 
Lisa Cook 5Lisa Cook 5
Grazzitti team, I used your code above for the trigger and test class but am receiving this error: 
System.QueryException: List has no rows for assignment to SObject 
Stack Trace: Class.ChatterAnswersEscalationTriggerTest.validateQuestionEscalation: line 25, column 1
Any ideas on what I am doing wrong?
y py p
Can We Enable chatter settings using apex code and metadata api? Can anyone help me?