0

I created an apex trigger which calls an apex class method before insert. This trigger is created for the Custom_Object__c and needs to copy Rate_Amount__c field value from another custom object which is Rate_Item__c. Rate__c field from Custom_Object__c must be populated with Rate_Amount__c given the condition that monthStartDate falls within between the range of Start_Date__c and End_Date__c fields from the Rate_Item__c.

Please take note that both custom objects do not have direct relationship with each other.

There are no errors when inserting the Custom_Object__c record but I noticed that Rate__c field is still zero.

How can I copy Rate_Amount__c value to Rate__c before insert?

Meanwhile, below are the current codes I have:

CustomObjectTrigger

trigger CustomObjectTrigger on Custom_Object__c (before insert)
{
    if(Trigger.isBefore && Trigger.isInsert) {
        CustomObjectController.copyRateItemValues(Trigger.new);
    }
}

CustomObjectController

public with sharing class CustomObjectController {
    @AuraEnabled
    public static void copyRateItemValues(List<Custom_Object__c> cRecords) {
        List<Custom_Object__c> cList = new List<Custom_Object__c>();
        Map<Id, Rate_Item__c> rateItemMap = new Map<Id, Rate_Item__c> ([SELECT Id, Name, Rate__c, 
                                                Rate_Amount__c, Start_Date__c, End_Date__c
                                                FROM Rate_Item__c WHERE Rate__r.Name = 'Rate A']);
                                                                                 
        if(cRecords.size() > 0) {
            for(Custom_Object__c cObj: cRecords){ 
                Date monthStartDate;
                Integer firstMonth = 1;
                String yearStart;
                String yearEnd;
                Integer intYearStart = 0;
                Integer intYearEnd = 0;
                String firstMonthDate;
                
                yearStart = proj.Fiscal_Year__c.right(4);   
                yearEnd = proj.Fiscal_Year__c.right(4);
                
                intYearStart = Integer.ValueOf(yearStart); 
                intYearEnd = Integer.ValueOf(yearEnd) + 1;
                
                firstMonthDate = '1' + '/' + String.ValueOf(firstMonth) + '/' + String.ValueOf(intYearStart);
                
                if(cObj.Picklist__c == 'value 1') {
                    monthStartDate = Date.parse(firstMonthDate);
                }

                for(Rate_Item__c rItemMap : rateItemMap.values()) {   
                    if(monthStartDate >= rItemMap.Start_Date__c && monthStartDate <= rItemMap.End_Date__c) {
                        Custom_Object__c co = new Custom_Object__c();
                        co.Rate__c = rItemMap.Rate_Amount__c;
                        cList.add(co);
                    }
                }
            }
        
            if(cList.size() > 0) {
                insert cList;
            }
        }
    }
}
1
  • Have you debugged your queries? Are they returning expected values? – David Reed Jul 6 '20 at 15:31
1

Your method is inserting brand-new Custom_Object__c records instead of setting the values of the actual records being processed.

When working with records in before insert / update, the basic method should look like this:

public static void handler(List<Custom_Object__c> newList) {
  // get your rate values here
  for (Custom_Object__c co : newList) {
    co.Rate__c = the rate amount value;
  }
}

The Trigger.new records are passed into the method by reference, so when you set the field values and complete the method, the trigger will continue and insert the records with the values you have set.

1
  • Looks like I just complicated things, everything works on my end now. Thanks! – Ertyq Jul 8 '20 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.