+ Start a Discussion
Dee Dee AaronDee Dee Aaron 

Set criteria for trigger to fire.

I have a trigger that works, but I want to add criteria.
If the Sales Engineering Request “Status” does not equal “Approved” or “Unable to Meet Request”, then trigger this.
How do I modify this? Thank you for your help.

My Trigger:
trigger FeedCommentTest on FeedComment (after insert) 
{
    for(FeedComment f : Trigger.New)
    {
        if(UserInfo.getProfileId()  == '00e6w000000FjiUAAS')
        {
            Sales_Engineering_Request__c SalesEngineeringRequestToUpdate = [SELECT ID FROM SALES_ENGINEERING_REQUEST__c WHERE ID =: f.ParentId];
            SalesEngineeringRequestToUpdate.Status__c = 'Approved';
            Update SalesEngineeringRequestToUpdate;
        }          
    }
}
Best Answer chosen by Dee Dee Aaron
Maharajan CMaharajan C
Hi Aaron,

Don't use the harcodeed profile Id use profile name.
 
trigger FeedCommentTest on FeedComment (after insert) 
{
	Id profileId = UserInfo.getProfileId();
	String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
	Set<id> SalesEngineeringSet = new Set<Id>();
	List<SALES_ENGINEERING_REQUEST__c> serList = new List<SALES_ENGINEERING_REQUEST__c>();
    for(FeedComment f : Trigger.New)
    {
        if(profileName  == 'Net Planning Department')
        {
			SalesEngineeringSet.add(f.ParentId);
        }          
    }
	
	if(!SalesEngineeringSet.IsEmpty()){
		for(SALES_ENGINEERING_REQUEST__c ser : [SELECT ID,Status__c FROM SALES_ENGINEERING_REQUEST__c WHERE ID In: SalesEngineeringSet]){
			if(ser.Status__c != 'Approved' && ser.Status__c != 'Unable to Meet Request'){
				ser.Status__c = 'Approved';
				serList.add(ser);
			}
		}
	}
	
	if(!serList.IsEmpty())
		update serList;
}

Thanks,
Maharajan.C​​​​​​​

All Answers

Prachi Goel 17Prachi Goel 17
Hi,
you can go like this:

trigger FeedCommentTest on FeedComment (after insert) 
{
    for(FeedComment f : Trigger.New)
    {
   if(f.status!='Approved' || f.status!='Unable to Meet Request')
}
}
Maharajan CMaharajan C
Hi Aaron,

Don't use the harcodeed profile Id use profile name.
 
trigger FeedCommentTest on FeedComment (after insert) 
{
	Id profileId = UserInfo.getProfileId();
	String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
	Set<id> SalesEngineeringSet = new Set<Id>();
	List<SALES_ENGINEERING_REQUEST__c> serList = new List<SALES_ENGINEERING_REQUEST__c>();
    for(FeedComment f : Trigger.New)
    {
        if(profileName  == 'Net Planning Department')
        {
			SalesEngineeringSet.add(f.ParentId);
        }          
    }
	
	if(!SalesEngineeringSet.IsEmpty()){
		for(SALES_ENGINEERING_REQUEST__c ser : [SELECT ID,Status__c FROM SALES_ENGINEERING_REQUEST__c WHERE ID In: SalesEngineeringSet]){
			if(ser.Status__c != 'Approved' && ser.Status__c != 'Unable to Meet Request'){
				ser.Status__c = 'Approved';
				serList.add(ser);
			}
		}
	}
	
	if(!serList.IsEmpty())
		update serList;
}

Thanks,
Maharajan.C​​​​​​​
This was selected as the best answer
Dee Dee AaronDee Dee Aaron
Thank you, Maharajan. This worked! Can you help provide a test class for this? Thank you very much.
Dee Dee AaronDee Dee Aaron
Hi Maharajan. Can you please help to provide a test class for this? Thank you.