+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Need help with Map syntax in apex trigger - Please help!!!

Hi Everyone,
           Please find my use case given below:

I am dealing with  3 objects : Opportunity,CBS,CAN.

Now both CBS and CAN objects are detail of Opportunity. I want that when CAN record gets created , it takes some of its values from an existing CBS record (which is unique). I have written an apex trigger but have issue with syntax. Please review and let me know what exactly I am doing wrong. The highlighted line is throwing syntax error. 




trigger AutopopulateCanAmount on CAN__c (before insert,before update) {

   List<CAN__c> CanRecords = Trigger.New;
   Set<Id> CanRecordIds = (new Map<Id,SObject>(CanRecords)).keySet();
    
    List<CAN__c> ParentOpportunityRecords = [Select Opportunity__c from CAN__c where Id In:CanRecordIds];   
    Set<Id> ParentOpportunityIds= (new Map<Id,SObject>(ParentOpportunityRecords)).keySet(); 

    
 
       Map<Id,Cost_Breakdown__c> OpportunityCostBreakDownMap = new map<id, Cost_Breakdown__c>();
      OpportunityCostBreakDownMap.putall([Select Id,(Select Base_Award_Fee__c from Cost_Breakdown__r where     Cost_Breakdown_Status__c='Awarded') From Opportunity where Id In:ParentOpportunityIds]);

for (Can__c can :Trigger.new){
c.Base_Award__c=OpportunityCostBreakDownMap.get(c.Opportunity__c).Base_Award_Fee__c;
}
}
Best Answer chosen by shrey.tyagi88@tcs.com
SarfarajSarfaraj
Hi Shrey,

Try this,
 
trigger AutopopulateCanAmount on CAN__c (before insert,before update) {

   Set<Id> CanRecordIds = trigger.newMap.keySet();
    
    List<CAN__c> ParentOpportunityRecords = [Select Opportunity__c from CAN__c where Id In:CanRecordIds];   
    Set<Id> ParentOpportunityIds= (new Map<Id,SObject>(ParentOpportunityRecords)).keySet(); 

    
 
       Map<Id,Opportunity> OpportunityCostBreakDownMap = new map<id, Opportunity>();
      OpportunityCostBreakDownMap.putall([Select Id,(Select Base_Award_Fee__c from Cost_Breakdown__r where     Cost_Breakdown_Status__c='Awarded') From Opportunity where Id In:ParentOpportunityIds]);

for (Can__c can :Trigger.new){
c.Base_Award__c=OpportunityCostBreakDownMap.get(c.Opportunity__c).Cost_Breakdown__r[0].Base_Award_Fee__c;
}
}

I am assuming that there will always be one and only one Cost_Breakdown__c record with status 'Awarded' against each opportunity. If not please modify accordingly.

--Akram

All Answers

Temoc MunozTemoc Munoz
Hi Shrey.

putAll() is exepcting a Cost_BreakDown__ object but you're querying an Opportunity. Is this what you're expecting? If not, change your Map to 
 
Map<Id,Opportunity> OpportunityCostBreakDownMap = new map<id, Opportunity>();


 
SarfarajSarfaraj
Hi Shrey,

Try this,
 
trigger AutopopulateCanAmount on CAN__c (before insert,before update) {

   Set<Id> CanRecordIds = trigger.newMap.keySet();
    
    List<CAN__c> ParentOpportunityRecords = [Select Opportunity__c from CAN__c where Id In:CanRecordIds];   
    Set<Id> ParentOpportunityIds= (new Map<Id,SObject>(ParentOpportunityRecords)).keySet(); 

    
 
       Map<Id,Opportunity> OpportunityCostBreakDownMap = new map<id, Opportunity>();
      OpportunityCostBreakDownMap.putall([Select Id,(Select Base_Award_Fee__c from Cost_Breakdown__r where     Cost_Breakdown_Status__c='Awarded') From Opportunity where Id In:ParentOpportunityIds]);

for (Can__c can :Trigger.new){
c.Base_Award__c=OpportunityCostBreakDownMap.get(c.Opportunity__c).Cost_Breakdown__r[0].Base_Award_Fee__c;
}
}

I am assuming that there will always be one and only one Cost_Breakdown__c record with status 'Awarded' against each opportunity. If not please modify accordingly.

--Akram
This was selected as the best answer