+ Start a Discussion
Nick KeehanNick Keehan 

Easy question - How to end Trigger by doing nothing

Hi Guys. 

How do i tell this trigger to do nothing if there is the "Sale__c" is blank on the Interaction. Sale is parent, interaction is child.

Works fine if there is a Sale__c attached to the interaction, doesnt work otherwise.


ERROR
caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
 
Trigger.Countchild: line 43, column 1

CODE
trigger Countchild on Interaction__c (after insert, after update) {

   List<Opportunity> OP = new List<Opportunity>();
   
   Set<Id> saleids = new Set<Id>();
   

   if(Trigger.isUpdate) {

     for(Interaction__c test:Trigger.New) {
      
        saleids.add(test.Sale__c);   
    
     }   
   
   }
   else
   {
     for(Interaction__c test:Trigger.New) {
      
        saleids.add(test.Sale__c);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Sale__c FROM Interaction__c where Sale__c IN :saleids GROUP BY Sale__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id saleid = (ID)ar.get('Sale__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Opportunity sale1 = new Opportunity(Id=saleid);
     
     sale1.Interaction_Count__c = count;
     
     OP.add(sale1);
      
   }
   
   
if(OP.size() > 0) {
update OP;
}


}


 
Best Answer chosen by Nick Keehan
Nick KeehanNick Keehan
How do i tell this trigger to do nothing if the "Sale__c"  field is blank on the Interaction. Sale is parent, interaction is child.