+ Start a Discussion
SkeeterSkeeter 

Note Object Trigger

I created a trigger to populate a checkbox on a custom object when a note is added. Now I need to add the criteria that the creator of the note is the same as a user lookup on the custom object.  I'm not sure how to get the lookup and add to the criteria.

I need the trigger to fire only when the note creator = id from the client__c lookup field on the ticket__c object.

Any help is greatly appreciated.
 
trigger NoteAI on Note (after insert) {
    
    //Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    //List of all Incidents to be updated with the flag
    List<EscalatedTicket__c> toUpdate = new List<EscalatedTicket__c>();
    
    //Collect all ParentIds    
    for(Note n : trigger.New){
        if(n.ParentId.getSobjectType() == EscalatedTicket__c.SobjectType){
        incIds.add(n.ParentId);
        }
    }
    
    //Collect all records with the above list of parentIds and return only those records
    List<EscalatedTicket__c> TicList = [select Id, ClientUpdated__c from EscalatedTicket__c where id in:incIds];
    if(toUpdate !=null){
        for(EscalatedTicket__c inc:TicList){
            inc.ClientUpdated__c = true;
            toUpdate.add(inc);
        }
        if(toUpdate.size()>0){
        Database.update(toUpdate,false);    
        }
    
    }

}

 
Best Answer chosen by Skeeter
Abhishek BansalAbhishek Bansal
Hi,
I have modified your trigger code according to your requirement.
Please change your trigger code from below code:
 
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();
    }
}

Let me know if you have any issues in it or you need more help on this.

Thanks,
Abhishek

All Answers

Gupta.VishalGupta.Vishal
Hi ,

You can not prevent the trigger fire but you can put a check after getting the client__c from ticket__c object  and then comparing the ownerid and client__c field
 
List<EscalatedTicket__c> TicList = [select Id,client__c , ClientUpdated__c from EscalatedTicket__c where id in:incIds];
Map<Id,EscalatedTicket__c> ticketMap=new Map<Id,EscalatedTicket__c>(TicList );



for(Note n : trigger.New){
  if(ticketMap.containsKey(n.parentID))
{
if(n.ownerId==ticketMap.get(n.parentId).client__c)
{
toUpdate.add(ticketMap.get(n.parentId));
}
}
}

if(!toUpdate.isEmpty())
{
//Do rest of the Processing 
}


Hope this Helps ,

Thanks ,
Vishal
Abhishek BansalAbhishek Bansal
Hi,
I have modified your trigger code according to your requirement.
Please change your trigger code from below code:
 
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();
    }
}

Let me know if you have any issues in it or you need more help on this.

Thanks,
Abhishek
This was selected as the best answer
SkeeterSkeeter
Thank you Abhishek!  That did the trick.  I was thinking I needed a map,but I wasn't sure where to put it.