function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nmnbawanmnbawa 

Help with trigger code

I have two objects Distributions and distributions used. there is a lookup from distribution to the distribution used object.

 

I have a field named Distribution amount left on the Distribution object which I want to update everytime distribution used object's field amount Reinvested is populated

 

So the formula would be 

 

Distribution amount left (distribution field)= Distrbution Amount(Distribution field) -  Amount Reinvested(Distribution used field)

 

Can someone please help with the below trgger. Also I want the error message if the amount left is negative. I know my trigger is totally screwed up.

 

Many thanks

 

trigger dda on Distribution__c  (before insert, before update)  {
 Map<Id,Id> userMap = new Map<Id,Id>();
    for (Distribution__c item : Trigger.new)
    {
        
        userMap.put(item.Distribution_Amount_Left__c, null);
        userMap.put(item.Distribution_Amount__c, null);
}

for (distribution_used__c item : [SELECT Id, Amount_Re_Invested__c FROM distribution_used__c WHERE Id IN :userMap.keySet()])
    {
        userMap.put(item.Id, item.Amount_Re_Invested__c);
    }
    
    for (Distribution__c item : Trigger.new)
    {
        item.Distribution_Amount_Left__c = userMap.get(item.Distribution_Amount__c)- (userMap.get(item.Amount_Re_Invested__c));
                              
    }

 

JayNicJayNic
I just answered a very similar question. this one also does undeletes, and deletes... Have a look:

http://boards.developerforce.com/t5/Apex-Code-Development/Calculate-summary-field-on-Look-up-Master-Record-using-trigger/td-p/627049
nmnbawanmnbawa

Hi

 

Thanks for your reply. 

 

I am very new to apex. Can you please help me with the trigger more in detail.

 

I am sure with such an awesome community somebody will defintely help.