+ Start a Discussion
Ertyq MrskErtyq Mrsk 

Field Values Not Copied From One Object to Another Using Before Insert on Apex Trigger

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;
            }
        }
    }
}
Hope anyone could help me with this. Thanks in advance!

 
ShirishaShirisha (Salesforce Developers) 
Hi Ertyq,

Greetings!

How these two Objects related to each other.Also,have you tried to capture the debug logs to see,if it is meeting the criteria or not and updating the field based on the Rate_Item__c record.

I would suggest you to capture the debug logs by using finest level for Apex while running this or inserting the record on Custom Object to see the code execution.

https://help.salesforce.com/articleView?id=code_add_users_debug_log.htm&type=5

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri 
Deepak PalDeepak Pal
Hi Ertyq,

Please check the 'monthStartDate' value if it is calculated correctly. I would recommend creating a Date instance by using 'newInstance' method. It will remove unnecessary conversions made by your code.
Example: Date myDate = date.newinstance(1960, 2, 17);
Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm#apex_methods_system_date

Also, check the records in the list of records of Rate_Item__c. They might have value zero stored in them. 
I believe if both the mentioned check is validated your code should work.

You can debug logs for the same and if there is a large set of debug statements you can use log levels. Example: system.debug(Logginglevel.ERROR,'message no # 1');
Refer https://help.salesforce.com/articleView?id=code_setting_debug_log_levels.htm&type=5.
Apex code have very less error logs so use the ERROR logging level and check your results.

Regards,
Deepak