You need to sign in to do that
Don't have an account?
shrey.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;
}
}
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;
}
}
Try this,
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
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
Try this,
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