0

I need to throw an error to the user when he tries to post/comment on case whose status is Closed - Resolved. I've tried to implement that and when I try to comment on a case the trigger is failing with the below error -

FeedCommentTriggerHandler: execution of AfterInsert caused by: System.FinalException: SObject row does not allow errors Trigger.FeedCommentTriggerHandler: line 30, column 1

Here is my trigger-

trigger FeedCommentTriggerHandler on FeedComment (after insert) { // GET LIST OF PARENT IDS
List<Id> ParentIds = new List<Id>();
for(FeedComment f:Trigger.new)ParentIds.add(f.ParentId);
// CREATE LIST OF FEEDITEM FOR CREATEDBY TO BE POPULATED
List<FeedComment> feedItems = new List<FeedComment>([SELECT Id,FeedItemId,CreatedById,CreatedDate,CreatedBy.Profile.UserType,CreatedBy.Profile.Name,ParentId,CommentType FROM FeedComment WHERE Id in :Trigger.newMap.keyset()]);

// INIT A MAP OF RELATED CASES WITH PARENT IDS
Map<Id,Case> relatedCases = new Map<Id,Case>([SELECT id,Status,isClosed, OwnerId FROM Case WHERE Id in :ParentIds ]);

// UPDATE CASE STATUS OF EACH CUSTOMER FEED RELATED CASE TO OPEN
for(FeedComment feed:feedItems)
{
    // IF FEED IS  CREATED BY customers
    System.debug('FeedCommentTriggerHandler: '+feed.CreatedBy.Profile.Name+' '+feed.ParentId.getSObjectType().getDescribe().getName()+' '+feed.CommentType);
    if( (feed.CreatedBy.Profile.Name =='B2B Customer Community Login' || feed.CreatedBy.Profile.Name == 'B2B Customer Community User' || feed.CreatedBy.Profile.Name == 'B2B Account Manager User' || feed.CreatedBy.Profile.Name == 'B2B Account Manager Login') && (relatedCases.get(feed.ParentId).status == 'Pending' || relatedCases.get(feed.ParentId).status == 'Waiting Internally' ) && feed.ParentId.getSObjectType().getDescribe().getName() =='Case')
    {            
        // SET CASE TO in progess 
        
        relatedCases.get(feed.ParentId).Pending_Date_Time__c = null;
        relatedCases.get(feed.ParentId).Status = 'In Progress';
        relatedCases.get(feed.ParentId).Sub_Status__c = '';
        relatedCases.get(feed.ParentId).Feed_Last_Modified_Date__c=feed.CreatedDate;  
        
            }
    else if( (feed.CreatedBy.Profile.Name =='B2B Customer Community Login' || feed.CreatedBy.Profile.Name =='System Administrator' ||  feed.CreatedBy.Profile.Name == 'B2B Customer Community User' || feed.CreatedBy.Profile.Name == 'B2B Account Manager User' || feed.CreatedBy.Profile.Name == 'B2B Account Manager Login') && (relatedCases.get(feed.ParentId).status == 'Closed - Resolved') && feed.ParentId.getSObjectType().getDescribe().getName() =='Case')

    {
        Feed.addError('Comments/Post not allowed on Resolved/Closed Cases');
    }
    else if(feed.ParentId.getSObjectType().getDescribe().getName() =='Case')
    {
        relatedCases.get(feed.ParentId).Feed_Last_Modified_Date__c=feed.CreatedDate;
        
    } 
    
}

if(relatedCases.values().size()>0)update relatedCases.values();}

1 Answer 1

1

The addError() documentation states (emphasis mine):

Marks a trigger record with a custom error message and prevents any DML operation from occurring.

The Trigger Exceptions one states (emphasis mine):

Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged.

The issue is that you're calling addError() on an instance of SObject that is not a trigger record instance.
Indeed you retrieved those FeedComment via SOQL and even if they are related to the same db records, they are not the same instances.
You should add the error on Trigger.new or Trigger.newMap records.

Change this line

Feed.addError('Comments/Post not allowed on Resolved/Closed Cases');

to

FeedComment triggerFeed = Trigger.newMap.get(feed.Id);
triggerFeed.addError('Comments/Post not allowed on Resolved/Closed Cases');

By the way, you can also change

List<FeedComment> feedItems = new List<FeedComment>([SELECT Id,FeedItemId,CreatedById,CreatedDate,CreatedBy.Profile.UserType,CreatedBy.Profile.Name,ParentId,CommentType FROM FeedComment WHERE Id in :Trigger.newMap.keyset()]);

to

List<FeedComment> feedItems = [SELECT Id,FeedItemId,CreatedById,CreatedDate,CreatedBy.Profile.UserType,CreatedBy.Profile.Name,ParentId,CommentType FROM FeedComment WHERE Id IN :Trigger.new];

That SOQL already returns a list.

Please also consider moving the logic in an handler class.

You must log in to answer this question.

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