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
Tzemach Aronow 1Tzemach Aronow 1 

How to transfer a partial $ value to a different record based on a rule.

I have a Member Object, donation fund object, and donation line object.

Member name: Sam
Donation Fund name: Office computers
Donation Fund name: Surplus
Donation line item Amount: $100

If Member "Sam" Donates $100 to donation fund "Office Computers", and the remaining needed to cover the goal of that fund is only $50, how Do I transfer the remainig $50 to donation fund Surplus? 

Once the transfer is made I want a record of where the surplus got its donation from.
Vinit_KumarVinit_Kumar
Where are you storing the value which is remaining needed to cover the Goal,I mean which field ??
Tzemach Aronow 1Tzemach Aronow 1
Donation fund has a formula field which is calculated from goal - recieved = remaining.
that filed in this case is on the "Office computer" record.

so if the goal is 100, and they recieved a donation of fifty, the remaining would go from 100 to 50. and if they get an addtional donation of 100 the remaining should go to 0, and the surplus 50 should transfer to the surplus record.
Vinit_KumarVinit_Kumar
Ok the remaining amount should go to which field on surplus record ??
Vinit_KumarVinit_Kumar
The way I would approach is to create an apex Trigger and calculate the remaining balance in that if it is more update the Surplus record.

So your logic should be something like below :-

// I am taking Opportunity as an example
// check if amount is greater than remaining amount,update amount Left field on Opp
if(opp.Amount >= opp.Received_Amount__c)
        {
            opp.Amount_Left__c = opp.Amount - opp.Received_Amount__c;
        }
// If remaining amount is greater than amount,update Amount_Left__c to 0.00 and update the Surplus record
        else if (opp.Amount < opp.Received_Amount__c)
        {
            opp.Amount_Left__c=0.00;
            surOpp.Amount_Left__c = opp.Received_Amount__c-opp.Amount;
            update surOpp;
        }

Hope this helps !!

If this helps,please mark it as best answer to help others.
Tzemach Aronow 1Tzemach Aronow 1
Thanks for your response, but since I'm new to apex I still need some more help and clarification.

I'll repeat again what I need and if you can try using my api names so its easier for me to follow.


If member X Makes a donation into Donation_Fund__c Fund_Name__c (Office Computers) using the Donation_Line_Item__c.
The Donation_Line_Item__c Amount__c is $200 but the Donation_Fund__c Goal__c is only $100, so the Remaining_to_collect__c is only $100.

What I want to happen is,
Donation_Fund__c Remaining_to_collect__c should go to $0, Amount_Collected__c should go to $100 and the remaining $100 should go to Donation_Fund__c Fund_Name__c (Surplus)

(If however the Donation_Fund__c Goal__c is (empty (i think the corrct term is NULL)) then do not apply this rule.
(because Surplus is in the same object as Office computers. so if it always applys the rule, it would never work, because the surplus does not have a goal ammount.)


If you look at the screen shots you might see how i set it up.

Goal is set when creating the record.
Amount collected is callculating donation amount.
I have another line item to transfer from the fund to an expense. so amount used is expense amount.
remaining available is amount collected - expense.
remaining to collect is goal - amount collected.

Thank you for your help and again, please be as detailed as possible as im a beginner here.User-added image
Vinit_KumarVinit_Kumar
This is more of Consulting help !!

Still like I told you create an Apex Trigger on  Donation which would verify the Conditions and update the Surplus record Amount field.So,you should be looking at something like below :-

// Queryin the Surplus record

Opportunity surOpp =[select id,Amount_Left__c from Opportunity where name='Surplus'];

// I am taking Opportunity as an example
// check if amount is greater than remaining amount,update amount Left field on Opp
if(opp.Amount >= opp.Received_Amount__c)
        {
            opp.Amount_Left__c = opp.Amount - opp.Received_Amount__c;
        }
// If remaining amount is greater than amount,update Amount_Left__c to 0.00 and update the Surplus record
        else if (opp.Amount < opp.Received_Amount__c)
        {
            opp.Amount_Left__c=0.00;
            surOpp.Amount_Left__c = opp.Received_Amount__c-opp.Amount;
            update surOpp;
        }

I am not sure about your salesforce coding experience as this would require some custom coding.I gave you an idea as how you can achieve the same.

Hope this helps !!

If this helps,please mark it as best answer to help others.