• anil Anil5
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
How to write Genaric Trigger on Parent Object to show error if child Objects fields having null.
Parent Object is Opp
Child Objects are opp.lineitems [Field1__c,Field__2] and GD_Val__c[FieldJ__c,FieldZ__c]
If child Objects field are blank and child objects having attachments then have to show error msg[Update Fields & Remove Attmt] while updating User__c Field on Parent[Opp]
trigger act as Rollupsumary functionality(sum,count,min,max) for lookup Relationship between two object with bestpractices
 



trigger RollUpSummaryUpdate on Company__c(after insert,after update){
           
               double totalSum = 0;

    Set<Id> Parentids = New Set<Id>();
    
    for(integer i=0;i<Trigger.new.Size();i++){
       Parentids.add(Trigger.new[i].Clients__c);
    }
    
    List<Client__c> AP = [SELECT ID,(SELECT ID FROM Company__r) FROM Client__c WHERE ID=:Parentids];
    
    for(Client__c apUpdates:AP){
    
            totalSum += Company__c.Amount__c; 
        apUpdates.Sum__c =totalSum;
    }
    Update AP;
}
I am perform trigger operation  on Company obj, trigger acts like rollup summary functionallity--->sum
Company is chlid
client is parent    R.S:Lookup
How to slove this error and any forward link for trigger rollup summary functionallity

trigger rollupTrigger on Company__c (after insert, after update, after delete) {
    
    double totalSum = 0;
  
    Company__c myObjects = trigger.new;
    
    if(Trigger.isUpdate || Trigger.isInsert)
    {
            for(Company__c object : myObjects) //assume more than 1 record in trigger.new
            {
              for(Client__c parent : [select Id, Name from Client__c where Id = :object.Clients__c]) //get the parent object
              {
                for(Company__c childObject : [select Id, Name, Amount__c from Company__c where Clients__c = :parent.Id]) //get all the objects that belong to the parent object
                {
                  totalSum += childObject.Amount__c;   //sum the field of choice
                }
                parent.Sum__c = totalSum;   //set the total in the parent
                update parent                       // update the parent
              }
            }
    }
    else if(Trigger.isDelete)
    {
        Company__c [] oldObjects = Trigger.old;
        
      for(Company__c oldObject :oldObjects)    //assume deletion of more than 1 record
      {
        for (Client__c parentObject : [select id, Name, Sum__c where Id = :oldObject.Clients__c]) //get the parent object(s)
        {
          for (Company__c childObject : [select id, Name, Amount__c from Company__c where Clients__c = :parentObject.id])   //get the records that belong to the parent
          {
             totalSum += childObject.Amount__c;  //sum the fields after a record has been deleted
          }
            parentObject.Sum__c = totalSum;   //set new total in parent
            update parentObject;                      //update parent
        }   
      } //end oldObject for loop
    } //end else if
    
} //end trigger
How to write Genaric Trigger on Parent Object to show error if child Objects fields having null.
Parent Object is Opp
Child Objects are opp.lineitems [Field1__c,Field__2] and GD_Val__c[FieldJ__c,FieldZ__c]
If child Objects field are blank and child objects having attachments then have to show error msg[Update Fields & Remove Attmt] while updating User__c Field on Parent[Opp]