• Deepak Pal
  • NEWBIE
  • 5 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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!

 
  Hi, Iam stuck in trailhead challenge in "Connect components with Events". I am getting above error(The campingList component isn't handing the added item correctly.), though my functionality is perfecly working fine. Please help me in this...I have tried solutions for the same error in this forum before, but they are not working as well.

CampingList.cmp:
 
<aura:component controller="CampingListController">
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <aura:handler name="addItem" action="{!c.handleAddItem}" event="c:addItemEvent"/>
    
    <aura:attribute name="items" type="Camping_Item__c[]" />
    
    <c:campingHeader />
    
    <c:campingListForm />
    
    <aura:iteration items="{!v.items}" var="item">
        <c:campingListItem item="{!item}" />
    </aura:iteration>
    
</aura:component>

CampingListController.js:
 
({

    doInit: function(component, event, helper){
        
        var action=component.get("c.getItems");
        action.setCallback(this, function(response)
                           {
                               var state = response.getState();
                               if(component.isValid() && state=='SUCCESS')
                               {
                                   component.set("v.items", response.getReturnValue());
                               }
                               else
                               {
                                   console.log("Error in state: "+ state);
                               }
                           });

        $A.enqueueAction(action);
        
    },
    
    handleAddItem : function(component, event, helper)
    {
        var item=event.getParam("item");
        var action=component.get("c.saveItem");
        
        action.setParams
        ({
                "item" : item
        });
        
        action.setCallback(this, function(response)
                           {
                               var state=response.getState();
                               if(component.isValid() && state == 'SUCCESS')
                               {
                                   var itemArray=component.get("v.items");
                                   itemArray.push(response.getReturnValue());
                                   component.set("v.items", itemArray);
                               }
                               else
                               {
                                   console.log('Error in state: '+ state);
                               }
                           });
        
        $A.enqueueAction(action);
    },
})