+ Start a Discussion
SkeeterSkeeter 

Trigger to not fire if lookup field is null

I need to update my trigger to not fire if the Staff__c lookup field on the escalated ticket is not null.  I wasn't sure where to include 
Staff__c != null

I tried at this line but it didn't work.
if(mapOfTicket.get(n.ParentId).client__c == n.OwnerId){

 
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        	incIds.add(n.ParentId);
        }
    }
    
    Map<Id,EscalatedTicket__c> mapOfTicket = new Map<Id,EscalatedTicket__c>([select Id,client__c , ClientUpdated__c from EscalatedTicket__c where id in:incIds]);
    Boolean updateValues = false;
    for(Note n : trigger.new){
    	if(mapOfTicket.ContainsKey(n.ParentId)){
    		if(mapOfTicket.get(n.ParentId).client__c == n.OwnerId){
    			mapOfTicket.get(n.ParentId).ClientUpdated__c = true;
    			updateValues = true;
    		}
    	}
    }
    
    if(updateValues){
    	update mapOfTicket.values();
    }
}


 
Best Answer chosen by Skeeter
Alexander TsitsuraAlexander Tsitsura
Hi lilranger

If i understand, you need add Staff__c field to your query, and add this condoition in loop

Please see code below
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        	incIds.add(n.ParentId);
        }
    }
    
    Map<Id,EscalatedTicket__c> mapOfTicket = new Map<Id,EscalatedTicket__c>([select Id,client__c , ClientUpdated__c, Staff__c from EscalatedTicket__c where id in:incIds]);
    Boolean updateValues = false;
    for(Note n : trigger.new){
    	if(mapOfTicket.ContainsKey(n.ParentId)){
			EscalatedTicket__c et = mapOfTicket.get(n.ParentId);
    		if(et.Staff__c == null) {
    			et.ClientUpdated__c = true;
    			updateValues = true;
    		}
    	}
    }
    
    if(updateValues){
		update mapOfTicket.values();
    }
}
and i recomended avoid updatating not needed records(line 25). You update map of all records. You can create list, that contains only records that need to changed.

see code below

 
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        	incIds.add(n.ParentId);
        }
    }
    
    Map<Id,EscalatedTicket__c> mapOfTicket = new Map<Id,EscalatedTicket__c>([select Id,client__c , ClientUpdated__c, Staff__c from EscalatedTicket__c where id in:incIds]);
    EscalatedTicket__c[] escTicketList = new EscalatedTicket__c[] {};
    for(Note n : trigger.new){
    	if(mapOfTicket.ContainsKey(n.ParentId)){
			EscalatedTicket__c et = mapOfTicket.get(n.ParentId);
    		if(et.Staff__c == null) {
    			et.ClientUpdated__c = true;
    			escTicketList.add(et);
    		}
    	}
    }
    
    if(!escTicketList.isEmpty()){
		update escTicketList;
    }
}




As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi lilranger

If i understand, you need add Staff__c field to your query, and add this condoition in loop

Please see code below
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        	incIds.add(n.ParentId);
        }
    }
    
    Map<Id,EscalatedTicket__c> mapOfTicket = new Map<Id,EscalatedTicket__c>([select Id,client__c , ClientUpdated__c, Staff__c from EscalatedTicket__c where id in:incIds]);
    Boolean updateValues = false;
    for(Note n : trigger.new){
    	if(mapOfTicket.ContainsKey(n.ParentId)){
			EscalatedTicket__c et = mapOfTicket.get(n.ParentId);
    		if(et.Staff__c == null) {
    			et.ClientUpdated__c = true;
    			updateValues = true;
    		}
    	}
    }
    
    if(updateValues){
		update mapOfTicket.values();
    }
}
and i recomended avoid updatating not needed records(line 25). You update map of all records. You can create list, that contains only records that need to changed.

see code below

 
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        	incIds.add(n.ParentId);
        }
    }
    
    Map<Id,EscalatedTicket__c> mapOfTicket = new Map<Id,EscalatedTicket__c>([select Id,client__c , ClientUpdated__c, Staff__c from EscalatedTicket__c where id in:incIds]);
    EscalatedTicket__c[] escTicketList = new EscalatedTicket__c[] {};
    for(Note n : trigger.new){
    	if(mapOfTicket.ContainsKey(n.ParentId)){
			EscalatedTicket__c et = mapOfTicket.get(n.ParentId);
    		if(et.Staff__c == null) {
    			et.ClientUpdated__c = true;
    			escTicketList.add(et);
    		}
    	}
    }
    
    if(!escTicketList.isEmpty()){
		update escTicketList;
    }
}




As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
This was selected as the best answer
SkeeterSkeeter
Thank you! That makes sense.