Skip to main content
6 answers
  1. Aug 5, 2019, 12:11 PM
    Hi Priti,

    You can use the below code:

    <------ Apex class--->>>>>

    public class UpdateFields {

        public static void updateCustom1fields(List<Custom2__c> listOfCustom2){

            try{

            if(listOfCustom2 != NULL){

                System.debug('subhasis');

                List<Custom1__c> existingList = new List<Custom1__c>();

                existingList = [SELECT Id, Id__c, CTC__c FROM Custom1__c LIMIT 50000];

                Map<Id, List<Custom1__c>> idVsCustom1 = new Map<Id, List<Custom1__c>>();

                if(existingList.size() > 0){

                    System.debug('subhasis');

                    for(Custom1__c object1 : existingList){

                        for(Custom2__c object2 : listOfCustom2){

                            if(object1.Id__c == object2.Id__c){

                                object1.CTC__c = object2.Bonous__c + object2.Expense__c;

                            }

                        }

                    }

                    Update existingList;

                }

                

            }

            }catch(Exception exp){

                System.debug('Exception Cause'+exp.getCause()+'Line Number'+exp.getLineNumber());

            }

        }

    }

    <<<---- Trigger------>>>>

     

    trigger UpdateFields on Custom2__c (after Update, after insert) {

        

        if(Trigger.isAfter )

            if(Trigger.isInsert || Trigger.isUpdate){

            UpdateFields.updateCustom1fields(Trigger.new);

        }

    }

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks,

    Ajay Dubedi
0/9000