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
Jill HedbergJill Hedberg 

Trigger - how make it NOT fire for one profile ID

Hi,

 

I have a trigger that ensures a delivery contact is added to the opportunity. I now want to make the trigger NOT fire when a Finance user edits the opportunity. So somehow I want to add NOT profileId = '00e20000000me3Q'. So that Finance users can go in and change stuff without having to add a delivery contact, but everyone else have to.

 

Where would I add this? Can you use NOT in apex code?

 

 

Here is my trigger:

trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();

    //Trigger.new is an array of opportunities 
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
        if  (Trigger.new[i].contact_required__c == 1) {
            oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
        }
    }

    //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required 

    List<OpportunityContactRole> roles = [select OpportunityId, Role from OpportunityContactRole
        where (OpportunityContactRole.Role ='Delivery Contact') and OpportunityContactRole.OpportunityId
        in :oppy_contact.keySet()];


    for (OpportunityContactRole ocr : roles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required    
String OppRectypeId = [SELECT Id, Name FROM RecordType WHERE Sobjecttype = 'Opportunity' AND Name = 'License Opportunity'].Id;

for (Opportunity oppy : system.trigger.new) 
{
    if(Oppy.RecordTypeId == OppRectypeId)
    {
        if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
        {
            oppy.addError('No Delivery Contact exists. Please go to the Contact Roles area and select a delivery contact before you can move on to stage "A6 - Closed Won".');
        }
    }
}   
 }

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

David,I got your point as what you are trying to say here.

 

Jill,

 

Try this one,

 

trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();
	
	Profile pr = [select id from Profile where name='Finance'];
	
	if(UserInfo.getProfileId()!=pr.id){

    //Trigger.new is an array of opportunities 
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
        if  (Trigger.new[i].contact_required__c == 1) {
            oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
        }
    }

    //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required 

    List<OpportunityContactRole> roles = [select OpportunityId, Role from OpportunityContactRole
        where (OpportunityContactRole.Role ='Delivery Contact') and OpportunityContactRole.OpportunityId
        in :oppy_contact.keySet()];


    for (OpportunityContactRole ocr : roles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required    
String OppRectypeId = [SELECT Id, Name FROM RecordType WHERE Sobjecttype = 'Opportunity' AND Name = 'License Opportunity'].Id;

for (Opportunity oppy : system.trigger.new) 
{
    if(Oppy.RecordTypeId == OppRectypeId )
    {
        if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
        {
            oppy.addError('No Delivery Contact exists. Please go to the Contact Roles area and select a delivery contact before you can move on to stage "A6 - Closed Won".');
        }
    }
}  
} 
 }

 

All Answers

Sean TanSean Tan

Couple things, I would not hard code the Id of the profile as this would be coupled to the org you're working in and won't work for future deployments.

 

That being said you can handle it by aggregating the list of "LastModifiedBy" id's into a set and querying the user table to get their profile Id. You would then exclude your opportunities if the user matches the criteria.

 

Something like this:

trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();
    Set<Id> userIds = new Set<Id>{};
    
    for (Opportunity item : Trigger.new)
    {
        userIds.add(item.LastModifiedBy);
    }
    
    Set<Id> excludedUserIds = new Set<Id>{};
    
    for (User u : [ select Id from User where ProfileId = '<value>' AND Id IN :userIds ])
    {
        excludedUserIds.add(u.Id);
    }
    
    List<Opportunity> opportunitiesToProcess = new List<Opportunity>{};
    
    //Trigger.new is an array of opportunities
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Opportunity item : Trigger.new)
    {
        System.debug('*****Required? ' + item.contact_required__c);
        if (item.contact_required__c == 1 && !excludedUserIds.contains(item.LastModifiedBy))
        {
            opporunitiesToProcess.add(item);
            oppy_contact.put(item.id,item);
        }
    }
    
    if (!opportunitiesToProcess.isEmpty())
    {    
        //Rest of the logic you have goes here
        
        //...
        
        //Note how this loops over the list built before rather then all the items in the trigger again
        for (Opportunity oppy : opportunitiesToProcess)
        {
            //Logic goes here            
        }
    }
 }

 

 

Vinit_KumarVinit_Kumar

Jill,

 

Try below code :-

 

trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();

    //Trigger.new is an array of opportunities 
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
        if  (Trigger.new[i].contact_required__c == 1) {
            oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
        }
    }

    //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required 

    List<OpportunityContactRole> roles = [select OpportunityId, Role from OpportunityContactRole
        where (OpportunityContactRole.Role ='Delivery Contact') and OpportunityContactRole.OpportunityId
        in :oppy_contact.keySet()];


    for (OpportunityContactRole ocr : roles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required    
String OppRectypeId = [SELECT Id, Name FROM RecordType WHERE Sobjecttype = 'Opportunity' AND Name = 'License Opportunity'].Id;

Profile pr = [select id from Profile where name='Finance'];

for (Opportunity oppy : system.trigger.new) 
{
    if(Oppy.RecordTypeId == OppRectypeId && UserInfo.getProfileId()!=pr.id)
    {
        if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
        {
            oppy.addError('No Delivery Contact exists. Please go to the Contact Roles area and select a delivery contact before you can move on to stage "A6 - Closed Won".');
        }
    }
}   
 }

 

davidjgriffdavidjgriff
I would do a similar approach to VInit, but put the User Profile check much sooner. You might as well wrap the whole trigger in an IF statement to check the current User Profile and not execute any code if they are Finance. It doesn't make much sense to do all that prep work if you are going to ignore it in the end :)
Jill HedbergJill Hedberg
How do I wrap the whole trigger in an IF statement?
Vinit_KumarVinit_Kumar

David,I got your point as what you are trying to say here.

 

Jill,

 

Try this one,

 

trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();
	
	Profile pr = [select id from Profile where name='Finance'];
	
	if(UserInfo.getProfileId()!=pr.id){

    //Trigger.new is an array of opportunities 
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
        if  (Trigger.new[i].contact_required__c == 1) {
            oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
        }
    }

    //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required 

    List<OpportunityContactRole> roles = [select OpportunityId, Role from OpportunityContactRole
        where (OpportunityContactRole.Role ='Delivery Contact') and OpportunityContactRole.OpportunityId
        in :oppy_contact.keySet()];


    for (OpportunityContactRole ocr : roles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required    
String OppRectypeId = [SELECT Id, Name FROM RecordType WHERE Sobjecttype = 'Opportunity' AND Name = 'License Opportunity'].Id;

for (Opportunity oppy : system.trigger.new) 
{
    if(Oppy.RecordTypeId == OppRectypeId )
    {
        if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
        {
            oppy.addError('No Delivery Contact exists. Please go to the Contact Roles area and select a delivery contact before you can move on to stage "A6 - Closed Won".');
        }
    }
}  
} 
 }

 

This was selected as the best answer
Jill HedbergJill Hedberg
Thank you guys!