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
Justin LonisJustin Lonis 

One line in this trigger is causing a problem for us

Hi thanks to SF Issue-Fixer, we got this awesome trigger but for some reason we can't get it to completely fire correctly. 

The trigger was to have a status change on a feed comment so here is the first one: 

This trigger allows anyone's feed comment to change the status

01trigger CommentStatusUpd on FeedComment (after insert) {
02     
03    for (FeedComment feedcs : trigger.new){
04         
05        List<CaseFeed> chkCsFeed = [ SELECT Id FROM CaseFeed WHERE ParentId=: feedcs.ParentId ];
06        if( chkCsFeed.size() > 0){
07            system.debug('***DEBUG***'+feedcs.ParentId);           
08            List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status FROM Case WHERE Id =: feedcs.ParentId ]);
09            for (integer i = 0; i < Pros.size(); i++){
10                system.debug('***DEBUG-INFO: CaseId***'+Pros[i].Id);
11                system.debug('***DEBUG-INFO: CaseNumber***'+Pros[i].CaseNumber);
12                system.debug('***DEBUG-INFO: CaseStatus***'+Pros[i].Status);
13                Pros[i].Status = 'Working';
14            }
15            update Pros;
16             
17        }
18         
19    }
20     
21}

However, we only wanted the status to change if a specific profile (00e800000018sx8) so only their feed comment on a case would change the status from "Awaiting Customer Feedback" to "Working": 

This trigger is supposed to only have the status change when 00e800000018sx8​ profile makes a feed comment. 

  trigger UpdateCommStatus on FeedComment (before insert) {
    for (FeedComment feedcs : trigger.new){
        List<CaseFeed> chkCsFeed = [ SELECT Id FROM CaseFeed WHERE ParentId=: feedcs.ParentId ];
        if( chkCsFeed.size() > 0){
            system.debug('***DEBUG***'+feedcs.ParentId);
            List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedcs.ParentId ]);
            for (integer i = 0; i < Pros.size(); i++){
                List<User> Usrs = new List<User>([ SELECT ProfileId FROM User WHERE Id=: Pros[i].OwnerId ]);
                for (User usrprofile : Usrs) {
                    if( usrprofile.ProfileId == '00e800000018sx8' ) {
                        Pros[i].Status = 'Working';       
                    }
                }
            }
            update Pros;
        }
    }
}  

There are some differents between the two triggers which both are supposed to change the status. Can anyone help with figuring out why it is not firing for the profile? 

Thanks, 
 
RainnyCloudyRainnyCloudy
One possible issue, might be the profile Id. If you migrated from a sandbox to production, the Id could be different. But anyways, to make your code a little better. I suggest you query for the id for the profile you need, instead of just hard coding the id. Something like this :
 
Profile myProfile = [SELECT Id FROM Profile WHERE Name: = 'Some Profile'];

if ( user.profileId == myProfile.Id ) {

   // DO SOMETHING

}

 
Justin LonisJustin Lonis
Any thoughts? I tried to take your recommendation and implant it in the code. Did I do it correctly? Also notice the differences between the : = and = : do they need to be consistent? 


 trigger UpdateCommStatus on FeedComment (before insert) {
    for (FeedComment feedcs : trigger.new){
        List<CaseFeed> chkCsFeed = [ SELECT Id FROM CaseFeed WHERE ParentId=: feedcs.ParentId ];
        if( chkCsFeed.size() > 0){
            system.debug('***DEBUG***'+feedcs.ParentId);
            List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedcs.ParentId ]);
            for (integer i = 0; i < Pros.size(); i++){
                List<User> Usrs = new List<User>([ SELECT ProfileId FROM User WHERE Id=: Pros[i].OwnerId ]);
                for (User usrprofile : Usrs) {
                        Profile myProfile = [SELECT Id FROM Profile WHERE Name: = '00e800000018sx8'];
                        if ( user.profileId == myProfile.Id ) {
                        Pros[i].Status = 'Working';       
                    }
                }
            }
            update Pros;
        }
    }
}  

 Error : unexpected token: ':' line 10 

 
venkat-Dvenkat-D
If you are querying from Profile use below. replace Profile name with your actual profile name
Profile myProfile = [SELECT Id FROM Profile WHERE Name = 'Profile Name'];
venkat-Dvenkat-D
Also you should avoid soql queries inside for loops refer to below best practice
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
RainnyCloudyRainnyCloudy
Whoops, sorry wrote my example wrong. It should be:
Profile myProfile = [SELECT Id FROM Profile WHERE Name =: 'Some Profile'];

if ( user.profileId == myProfile.Id ) {

   // DO SOMETHING

}

You'll want to stick the query outside the for loop. Usually all trigger logic would actually be in a seperate apex class, and not within the trigger itself. But you can check that out yourself. 

So your code should look like this:
 
trigger UpdateCommStatus on FeedComment (before insert) {

// use the name of the profile not the Id. Don't hardcode Ids :D
 Profile myProfile = [SELECT Id FROM Profile WHERE Name =: 'Profile Name']; 

    for (FeedComment feedcs : trigger.new){
        List<CaseFeed> chkCsFeed = [ SELECT Id FROM CaseFeed WHERE ParentId=: feedcs.ParentId ];
        if( chkCsFeed.size() > 0){
            system.debug('***DEBUG***'+feedcs.ParentId);
            List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedcs.ParentId ]);
            for (integer i = 0; i < Pros.size(); i++){
                List<User> Usrs = new List<User>([ SELECT ProfileId FROM User WHERE Id=: Pros[i].OwnerId ]);
                for (User usrprofile : Usrs) {
                        if ( user.profileId == myProfile.Id ) {
                        Pros[i].Status = 'Working';       
                    }
                }
            }
            update Pros;
        }
    }
}

 
Justin LonisJustin Lonis
Hi Luke, 

got an error on line 14: Comparison arguments must be compatible types: Schema.SObjectField, Id

My apex knowledge is minimal at best so I appreciate all the help from everyone thus far. I feel we are close!

Thanks, 
 
venkat-Dvenkat-D
My mistake..try usrprofile.Id ==myProfile.Id 
venkat-Dvenkat-D
I couldnt resist fixing the whole issue.
 
trigger UpdateCommStatus on FeedComment (before insert) {
    Id myProfileId = [SELECT Id FROM Profile WHERE Name: = 'Some Profile' limit 1]>id;

    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
    	feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId FROM Case WHERE Id =: feedparentIds ];
    
    if(pros.size()>0){
        for(Case cs : Pros){
            if(cs.owner.type == 'User'){
                if(cs.owner.ProfileId == myProfileId){
                 cs.Status = 'Working';        
                }
            }
        
        } 
        update pros;
    }
}

Try above code.
venkat-Dvenkat-D
trigger UpdateCommStatus on FeedComment (before insert) {
    Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'Some Profile' limit 1].id;

    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
    	feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId FROM Case WHERE Id =: feedparentIds ];
    
    if(pros.size()>0){
        for(Case cs : Pros){
            if(cs.owner.type == 'User'){
                if(cs.owner.ProfileId == myProfileId){
                 cs.Status = 'Working';        
                }
            }
        
        } 
        update pros;
    }
}

Fixed typos.

 
Justin LonisJustin Lonis

Hi Venky409, 

Got this error when as a Customer under profile "00e800000018sx8." Also, in the trigger you provided where can I put the profiles that I want to be able to change the status so "00e800000018sx8." Like discussed above we don't want internal case owners to have comments that change this status. 

User-added image
venkat-Dvenkat-D
Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'Some Profile' limit 1].id 

in above Replace some profile with profile name you want to match like Marketing User or System Administrator or whatever name you want that exists in system
venkat-Dvenkat-D
If you have more than one profile you can do something like this. 
 
trigger UpdateCommStatus on FeedComment (before insert) {
    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
    	feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId,owner.Profile.Name FROM Case WHERE Id =: feedparentIds ];
    
    if(pros.size()>0){
        for(Case cs : Pros){
            if(cs.owner.type == 'User'){
                if(Label.ProfileNames.contains(cs.owner.Profile.Name)){
                 cs.Status = 'Working';        
                }
            }
        
        } 
        update pros;
    }
}

and Create a new customlabel called ProfileNames with value as Profile names separated by comma
Justin LonisJustin Lonis
Venky, 

How does this look? I was going to do the multiple profile trigger but wanted to keep it simple for now so I'll do the one profile one: 

trigger UpdateCommStatus on FeedComment (before insert) {
    Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id;

    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
        feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId FROM Case WHERE Id =: feedparentIds ];
    
    if(pros.size()>0){
        for(Case cs : Pros){
            if(cs.owner.type == 'User'){
                if(cs.owner.ProfileId == myProfileId){
                 cs.Status = 'Working';        
                }
            }
        
        } 
        update pros;
    }
}

Do I need anything else? I'm doing this in sandbox, saved the trigger and went to a ticket and it still didn't change the status. Just wanted to confirm that I didn't miss anything like the last time. Also just to confirm, I don't need anywhere in this code that it was in awaiting customer feedback first, right? 

User-added image


Also, I saw your note 
and Create a new customlabel called ProfileNames with value as Profile names separated by comma. I need to learn how to create a customlabel as I am just getting started with Apex. So that's why I stuck with the first one-profile trigger. 

Thanks, 


 
venkat-Dvenkat-D
trigger UpdateCommStatus on FeedComment (before insert) {
    Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id;

    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
        feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId FROM Case WHERE Id =: feedparentIds ];

    if(pros.size()>0){
        
        for(Case cs : Pros){
            if(cs.owner.type == 'User'){
                if(cs.owner.ProfileId == myProfileId && cs.Status == 'Awaiting Customer Feedback'){
                 cs.Status = 'Working';        
                }
            }
        
        } 
        update pros;
    }
}

Added awaiting customer check as well. Check whether profile name is correct or not? Code looks good.
venkat-Dvenkat-D
Try this if the above doesnt work
 
trigger UpdateCommStatus on FeedComment (before insert) {
    Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id;

    set<Id> feedparentIds = new set<Id>();
    for (FeedComment feedcs : trigger.new){
        feedparentIds.add(feedcs.ParentId);    
    }
    List<Case> Pros = new List<Case>();
    Pros = [SELECT CaseNumber, Id, Status, owner.type,OwnerId,owner.ProfileId FROM Case WHERE Id =: feedparentIds ];
    
    if(pros.size()>0){
        for(Case cs : Pros){
            //if(cs.owner.type == 'User'){
                if(cs.owner.ProfileId == myProfileId && cs.Status == 'Awaiting Customer Feedback'){
                 cs.Status = 'Working';        
                }
            //}
        
        } 
        update pros;
    }
}

 
Justin LonisJustin Lonis
Tried both triggers and got the same error. Just to confirm here is the profile that I want to be able to switch the status from awaiting customer feedback to working on a feed comment....screenshot below with ID as well. 

User-added image

User-added image
venkat-Dvenkat-D
It seems like some permission issue related to Customer portal users. can you check that? I do not have working knowledge of portals
Justin LonisJustin Lonis
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm

I'm not sure if this helps but the user's are CSPLitePortal. But we don't have any special permissions. The status field is also required on all page layouts for field accessibility. 

Just for theory sake, with the very first trigger I posted to where it has evolved to...why did we get rid of the <case feed> since that is what the customers are commenting on? A feed comment on the case feed. 
venkat-Dvenkat-D
I have removed that because you are using same set of Ids in the SOQL . So I didnt get why we had to check first one. 

  List<CaseFeed> chkCsFeed = [ SELECT Id FROM CaseFeed WHERE ParentId=: feedcs.ParentId ];
 List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedcs.ParentId 
Justin LonisJustin Lonis
I just included the first one because that once was the only one that has been able to get the status to change on the feed comment. Figured it couldn't hurt to include. 
Justin LonisJustin Lonis
Hi Everyone, 

I put this towards the beginning: system.debug('***debugtest***');

How can we tell if it is firing? 

*This is all I have to share from a log perspective

LOGS: 10:19:47:016 SOQL_EXECUTE_BEGIN [46]|Aggregations:0|select id, LastModifiedDate, lead_grade__c, first_conversion_date__c, first_conversion_event__c, recent_conversion_date__c, recent_conversion_event__c, guid__c, found_site_via__c, hubspot_detail__c, ip_address__c, conversion_events__c, website_visits__c, total_page_views__c, first_visit__c, recent_visit__c, lead__c, contact__c, ip_region__c, ip_city__c, ip_country__c, portal_id__c from HubSpot_Intelligence__c where Contact__c ='003540000026FEO'

10:19:47:042 SOQL_EXECUTE_END [51]|Rows:0

10:19:47:053 CUMULATIVE_LIMIT_USAGE 

10:19:47:036 SOQL_EXECUTE_END [46]|Rows:0
venkat-Dvenkat-D
Search for this text ***debugtest*** in the debug logs.
Justin LonisJustin Lonis
Venky, 

I added the last line of code from yesterday that you gave (the last comment from 3/16) and got this problem(s): Variable does not exist: feedcs.ParentId line 17 

    trigger UpdateCommStatus on FeedComment (before insert) {   
        Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id; 
        set<Id> feedparentIds = new set<Id>(); 
        for (FeedComment feedcs : trigger.new){ 
            feedparentIds.add(feedcs.ParentId); 
        }
        List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedcs.ParentId]); 
        if(pros.size()>0){ 
            for(Case cs : Pros){ 
                    if(cs.owner.ProfileId == myProfileId && cs.Status == 'Awaiting Customer Feedback'){ 
                    cs.Status = 'Working'; 
                    }
                }
            }
    update pros; 
    }

What I'm thinking is it's a scope error, right? 
 
venkat-Dvenkat-D
Yes use this query  
List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedparentIds]); 
Justin LonisJustin Lonis
Hi Venky, 

Thanks again for you help. Here is the new code with a different error (argggg...lol). I bolded the error in the code and provided a screenshot. Can we take one more stab at this? If we need to get another set of eyes on this I can repost to another thread in the developer community and mark you as best answer for your hard work. I appreciate everyone taking the time to help out with this, it means the world to me!!!

trigger UpdateCommStatus on FeedComment (before insert) {   
        Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id; 
        set<Id> feedparentIds = new set<Id>(); 
        for (FeedComment feedcs : trigger.new){ 
            feedparentIds.add(feedcs.ParentId); 
        }
        List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, Status, OwnerId FROM Case WHERE Id =: feedparentIds]); 
        if(pros.size()>0){ 
            for(Case cs : Pros){ 
                    if(cs.owner.ProfileId == myProfileId && cs.Status == 'Awaiting Customer Feedback'){ 
                    cs.Status = 'Working'; 
                    }
                }
            }
    update Pros; 
    }

User-added image


 
venkat-Dvenkat-D
trigger UpdateCommStatus on FeedComment (before insert) {   
        Id myProfileId = [SELECT Id FROM Profile WHERE Name = 'CardinalCommerce Customer' limit 1].id; 
        set<Id> feedparentIds = new set<Id>(); 
        for (FeedComment feedcs : trigger.new){ 
            feedparentIds.add(feedcs.ParentId); 
        }
        List<Case> Pros = new List<Case>([ SELECT CaseNumber, Id, owner.ProfileId, Status, OwnerId FROM Case WHERE Id =: feedparentIds]); 
        if(pros.size()>0){ 
            for(Case cs : Pros){ 
                    if(cs.owner.ProfileId == myProfileId && cs.Status == 'Awaiting Customer Feedback'){ 
                    cs.Status = 'Working'; 
                    }
                }
            }
    update Pros; 
    }

Try this one. we have to query that field in soql . i changed that
Justin LonisJustin Lonis
So no more errors but when I tested a customer ticket the status didn't change. 

Just want to confirm I'm doing everything correct on my end....adding new changes to trigger in developer console...saving....refreshing customer ticket...submitting new feed comment (while ticket "status" is in "Awaiting Customer Feedback") and then evaluating if it works? I don't have a test class prepared (although in sandbox you don't need it, right?) and I have 82% coverage. 

I might have some error messages....I'll put some screenshots below:

User-added image

User-added image

User-added image

Hopefully we are getting close! Thanks!!
 
venkat-Dvenkat-D
Yes...You dont need test class coverage in sandbox to test.
1. Check whether you are testing with right profile or not
2. Check the data.
Justin LonisJustin Lonis
User-added image

Checked the profile.