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
uma52551.3972270309784705E12uma52551.3972270309784705E12 

After Update Trigger

Hi All,
I am trying to write an agter update trigger to update date field in Bulk_Product__c object When date field in Bulk_Contract__c is Updated not on insert. Below my trigger is doing nothing please help me in finding my mistake. Thanks!

trigger UpdateExpirationDatesTrigger on Bulk_Contract__c (after update) {
 for (Bulk__Contract__c blkConUpd : trigger.new) {
        Bulk_Contract__c blkConOld = System.Trigger.oldMap.get(blkConUpd.Id);      
           List<Bulk_Product__c> blkProdList = [Select Id,New_Business_Expiration_Date__c FROM Bulk_Product__c WHERE Id = :blkConOld.Id];           
               for(Bulk_Product__c blk : blkProdList){
                 if(blk.Contract_Code__c == blkConUpd.Contract_Code__c&&blk.Company_Code__c ==blkConUpd.Company_Code__c){
                   blk.New_Business_Expiration_Date__c = blkConUpd.New_Business_Expiration_Date__c;
                   update ag;
                 
                   }
                     
               }
               
           }             
}
Bhanu MaheshBhanu Mahesh

Hi Uma,

List<Bulk_Product__c> blkProdList = [Select Id,New_Business_Expiration_Date__c FROM Bulk_Product__c WHERE Id = :blkConOld.Id];
In the above query you are querying the Bulk_Product__c records withb same Id as Bulk__Contract__c records which is not possible.
No two records will have the same Id in salesforce. So use the correct field in WHERE condition might be parent field or some thing which is Lookup or Master field based on your requirement.

You are using fields  from Bulk_Product__c which are not included in the query. Might be it is giving exception.
Try adding Contract_Code__c and Company_Code__c fields in the query.

Also as per best practices, avoid SOQL and DML operations inside For loops.
Make use of collections to avoid repeated queries.

Regards,
Bhanu Mahesh Gadi
Waqar Hussain SFWaqar Hussain SF
Hello Uma can you explain that what is the relation between Bulk__Contract__c and Bulk_Product__c objec?
Bhanu MaheshBhanu Mahesh
Hi Uma,

Assuming Account is the parent for both  Bulk_Contract__c and Bulk_Product__c objects through Producer__c field on respective objects.

Updating the New_Business_Expiration_Date__c value to the all the related Bulk_Product__c  from the first Bulk_Contract__c record which is related to the specific account and ignoring the remaining remaining records otherwise we can update withe Last record.

Check whether this is meeting your requirement or not.

Note: there might be compile errors as I have coded it on notepad

trigger UpdateExpirationDatesTrigger on Bulk_Contract__c (after update) {
    Set<Id> stBulkContrct = new Set<Id>();
    Map<Id,List<Bulk_Product__c>> mapcontractWithProdct = new Map<Id,List<Bulk_Product__c>>();
    List<Bulk_Product__c> lstblkProdctToUpdate = new List<Bulk_Product__c>();
    Set<Id> processed = new Set<Id>();
    
    for(Integer i = 0;i < trigger.new.size(); i++){
        if(trigger.New[i].New_Business_Expiration_Date__c != trigger.Old[i].New_Business_Expiration_Date__c){
            if(trigger.New[i].Producer__c != null){
                stBulkContrct.add(trigger.New[i].Producer__c);
            }
        }
    }
    if(!stBulkContrct.isEmpty()){
        for(Bulk_Product__c blk :Select Id,New_Business_Expiration_Date__c,Contract_Code__c,Company_Code__c,Producer__c FROM Bulk_Product__c WHERE Producer__c IN :stBulkContrct]){
                if(mapcontractWithProdct.get(blk.Producer__c) != null ){
                    mapcontractWithProdct.get(blk.Producer__c).add(blk);    
                }
                else{
                    List<Bulk_Product__c> lstBulkProd = new List<Bulk_Product__c>();
                    lstBulkProd.add(blk);
                    mapcontractWithProdct.put(blk.Producer__c,blk);
                }
        }
    }
    for (Bulk__Contract__c blkConUpd : trigger.new) {      
                     
        if(blkConUpd.Producer__c != null && mapcontractWithProdct != null && mapcontractWithProdct.get(blkConUpd.Producer__c) != null && !processed.contains(blkConUpd.Producer__c)){
            for(Bulk_Product__c blk : mapcontractWithProdct.get(blkConUpd.Producer__c)){
                 if(blk.Contract_Code__c == blkConUpd.Contract_Code__c && blk.Company_Code__c == blkConUpd.Company_Code__c){
                   blk.New_Business_Expiration_Date__c = blkConUpd.New_Business_Expiration_Date__c;
                   lstblkProdctToUpdate.add(blk);
                 }     
           }
           processed.add(blkConUpd.Producer__c);
        }   
    }
    if(!lstblkProdctToUpdate.isEmpty()){
        update lstblkProdctToUpdate;
    }
}


Regards,
Bhanu Mahesh