+ Start a Discussion
KNKKNK 

Trigger to Update field

Hi Team,

 

 I am going update twi fields in Crop Record(Average CP and Average Seeds) which is from ABS (Average CP and Average Seeds).

 

In Crop Record ABS is Lookup field.

 

This is i have done so far but it is not working..

 

 

public with sharing class ABS_TriggerHandler {

    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
 
    public ABS_TriggerHandler(boolean isExecuting, integer size){
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
    
    public void OnBeforeInsert(list<ABS__c> newObjects)
      {
         Updatepotential (newObjects);
      }
    
    public void OnAfterInsert(list<ABS__c> newObjects)
      {
         Updatepotential (newObjects);
      }
private void Updatepotential (list <ABS__c> newCPs){
        Set<String> years = new Set<String>();
        set<Id> territories = new set<Id>();
        set<Id> crops = new set<Id>();
        map<String, set<Id>> absCropsToUpdateMap = new map<String, set<Id>>();


         for(ABS__c abs : newCPs)
            {
                        years.add(abs.Name);
                        territories.add(abs.Territory_Name__c);
                        crops.add(abs.Crop__c);
                        String key = abs.Name + abs.Territory_Name__c;
                        
                        if(!absCropsToUpdateMap .containsKey(key))
                        absCropsToUpdateMap .put(key, new set<Id>{abs.Crop__c});
                        else
                        absCropsToUpdateMap .get(key).add(abs.Crop__c);                        
            }

         if(absCropsToUpdateMap.size() > 0)
          {
            list<ABS__c> CPCropsToBeInserted = new list<ABS__c>();
            List<Crop_Record__c> cropRecords = new List<Crop_Record__c>();
             for (ABS__c cpp : [ Select Id, Name, Average_CP__c, Average_Seeds__c,
                                 Crop__r.Name, Crop__c, (SELECT Average_CP__c,Average_Seeds__c,Crop__c FROM Crop_Records__r) From ABS__c Where  Name = :years
                                 and Crop__r.Name = :crops])
               
                             
                                 for (Crop_Record__c cr : cpp.Crop_Records__r) {
                                     if (cpp.Crop__c == cr.Crop__c) {
                                         cr.Average_CP__c = cpp.Average_CP__c;
                                         cr.Average_Seeds__c = cpp.Average_Seeds__c;
                                         cropRecords.add(cr);
                                     }
                                 }
                                
                             
               if (!cropRecords.isEmpty()){
                   update cropRecords;
               }
           }  
       }
 
    
}

clouddev@surashriclouddev@surashri

Hi,

 

You are mentioning Trigger in your subject line but written a class. If you want to update fields depeding on another fields value change, you need to write a trigger or workflow rule (preferred).

 

thanks

 

KNKKNK

forgot to mention trigger, this is the trigger..

 

trigger ABS on ABS__c (before insert, after insert, after update, before delete) {
    ABS_TriggerHandler handler = new ABS_TriggerHandler(Trigger.isExecuting, Trigger.size);
   
    if( Trigger.isBefore && Trigger.isInsert )   
         handler.OnBeforeInsert(Trigger.new);
    else if(Trigger.isInsert && Trigger.isAfter)
        handler.OnAfterInsert(Trigger.new);
    else if(Trigger.isUpdate && Trigger.isAfter)
        handler.OnAfterUpdate(Trigger.new, Trigger.oldMap);
    }