You need to sign in to do that
Don't have an account?
Trigger to update another object
Hi Everyone,
I have 2 objects 'Gift__c' and 'Field_Executive__c'. There is a lookup relation from the former to the latter. Whenever there is a name of a field executive in a newly created Gift record, an updation should occur in the record of that particular Field Executive.
There is a common decimal field called No_of_Gifts__c in both objects. This value should be extracted from the current Gift and it should be subtracted from the 'No_of_Gifts__c' field in the corresponding Field Executive. My code is not working. It is compiling but no updation occurs. Can someone point me the error ?
trigger Issue on Gift__c (after insert) { Decimal gifts=trigger.new[0].No_of_Gifts__c; string fieldexecutive_name=trigger.new[0].Fieldexecutive__c; List<Fieldexecutive__c> fieldexecutive=[Select Id from Fieldexecutive__c where Name=:fieldexecutive_name]; for(Fieldexecutive__c fe:fieldexecutive) { Decimal giftsfe=fe.Number_of_Gifts__c; if(giftsfe>=gifts) { fe.Number_of_Gifts__c=giftsfe-gifts; } } update fieldexecutive; }
try below
All Answers
try this
Hi Dapanchal,
It is still not working. Also do I need to use a list here if I only have to deal with just 1 record everytime?
In the Gifts__c standard page there is a LOOKUP field called Fieldexecutive which points to the Name field of a single Fieldexecutive.
try below
It gives the following error
Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Decimal>].add(Id, Decimal) at line 7 column 9
g.Field_Executive__c returns the Name field right? Can the Name field pass as Id?
mapGifts.put(g.Fieldexecutive__c, g.No_of_Gifts__c);
instead of
mapGifts.add(g.Fieldexecutive__c, g.No_of_Gifts__c);
Hi Daval,
It is working now. Thanks a lot for your help.