0

Can someone please take a look at the below trigger and tell me what I can do to make it work!

It's triggering an APEX Class to run, however I only want it to run on a specific Record Type.

Essentially I only want it to run the APEX Class when a FeedComment is made on the Case Record type 'UK_Pricing_Query'. Any help would be greatly appreciated!

trigger CaseCommentSyncFeedCommentBeforeTrigger on FeedComment (before insert) {
    List<FeedComment> cmtList = new List<FeedComment>();
    for(FeedComment cmt: Trigger.new) {
        Case c;
        recordTypeName = Schema.SObjectType.Case.getRecordTypeInfosById().get(c.RecordTypeId).getDeveloperName();
        if('UK_Pricing_Query'.equalsIgnoreCase(recordTypeName)) {
            cmtList.add(cmt);
        }
    }
    CaseFeedCommentSyncUtils.caseFeedCommentToCaseComment(cmtList);
 }

Currently this trigger is failing with the error -

Error: Compile Error: Variable does not exist: recordTypeName at line 5 column 5.

5
  • You missed declaring the datatype of the complaining variable, should be 'String recordTypeName'
    – Raul
    Aug 12, 2019 at 10:11
  • 1
    You will have to declare recordTypeName. You can add this declaration before the for loop. i.e. add line string recordTypeName = '' Aug 12, 2019 at 10:12
  • @VijayGanji Gotcha! Added and now the Trigger saves. However, I get hit with the following error on the record page 'Attempt to de-reference null object' when it runs the 'recordTypeName' Aug 12, 2019 at 10:22
  • Yes you will get that error because you are trying to access RecordTypeId of Case i.e. cwhich is null. So you will need to get the Case instance with RecordType information related to FeedCommentand then use RecordTypeId Aug 12, 2019 at 10:29
  • @VijayGanji I see, what would this look like in code? Thank you in advance and sorry for the basic questions - you can probably tell I am new to code! :) Aug 12, 2019 at 10:44

1 Answer 1

2

You need to use the "aggregate-query-update" (my blog post) pattern to get parent record information. Basically, you aggregate the values to query from the database, query the database, then perform any updates. We can also optimize this by using a simple Set to determine if the case matches; we'll filter by a query filter to reduce the number of records we return. In your case, that logic looks like the following.

trigger CaseCommentSyncFeedCommentBeforeTrigger on FeedComment (before insert) {
    // Aggregate
    Set<Id> caseIds = new Set<Id>();
    for(FeedComment record: Trigger.new) {
      caseIds.add(record.ParentId);
    }
    // Query
    caseIds.retainAll(
      new Map<Id, Case>(
        [SELECT Id FROM Case WHERE Id = :caseIds AND RecordType.DeveloperName = 'UK_Pricing_Query']
      ).keySet()
    );
    // Update: Get the CMT values then pass to method
    FeedComment[] cmtList = new FeedComment[0];
    for(FeedComment record: Trigger.new) {
      if(caseIds.contains(record.ParentId)) {
        cmtList.add(record);
      }
    }
    CaseFeedCommentSyncUtils.caseFeedCommentToCaseComment(cmtList);
}
5
  • This looks like it will do the trick - However the trigger is failing with the Error: Compile Error: Missing '{' at 'Set' at line 4 column 1. Aug 12, 2019 at 12:30
  • @LiamHeppenstall Did you include the "trigger" line at top? This looks like it should compile. I've edited in what the entire solution should look like.
    – sfdcfox
    Aug 12, 2019 at 12:47
  • Yurp, I popped that in the top. I have copied yours across to validate and now its failing with Error: Compile Error: Unexpected token '('. at line 8 column 22. Thanks again for your help with this! :) Aug 12, 2019 at 12:52
  • @LiamHeppenstall There was a missing ] and a misplaced ;. I just copied this to my org and fixed it. Try again.
    – sfdcfox
    Aug 12, 2019 at 13:01
  • You are god! Works like a charm, You can probably tell I am really new to code! I am learning though, but thanks to the help of people like you I am learning a lot faster! Aug 12, 2019 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .