0

My question is , I need to divide the amount in such a way that the addition of all should be equal to total amount.

With the below trigger my answer is coming as :- If the amount on contact is 50 & three related contacts are there , then its divided as 50/3 = 16.67 each but total is 50.01 which is incorrect.

I need it as 50 , It can be divided in anyway like two contacts same and another contact different

trigger AccountTrigger on Account (after update) {

List<Contact> conListToUpdate = new List<Contact>();
for(Account acc : [SELECT Id,Total__c, (SELECT Id, Share__c FROM Contacts) FROM Account WHERE Id IN : Trigger.new]){
    
    if(acc.Total__c != null){
        Integer contactNumber = acc.Contacts.size();
        Decimal shareEachContact = 0;
        if(contactNumber  > 0){
             shareEachContact = acc.Total__c / contactNumber;
        }
       
       for(Contact con : acc.Contacts){
           con.Share__c = shareEachContact;    
                     
           conListToUpdate.add(con);
       }
    }
}

update conListToUpdate;

}

2 Answers 2

0

The discrepancy that you see is likely due to rounding by Salesforce. The best way to achieve this would be to put sane share to all contacts (n) except the last one (nth). There you can subtract the total of (n-1) contact from account total.

trigger AccountTrigger on Account (after update) {

    List<Contact> conListToUpdate = new List<Contact>();
    for (Account acc : [SELECT Id,Total__c, (SELECT Id, Share__c FROM Contacts) FROM Account WHERE Id IN :Trigger.new]) {

        if (acc.Total__c != null) {
            Integer contactNumber = acc.Contacts.size();
            Decimal shareEachContact = 0;
            if (contactNumber > 0) {
                shareEachContact = Math.floor(acc.Total__c / contactNumber);
            }

            for (Integer i = 0; i < acc.Contacts.size(); i++) {
                if (i == (contactNumber - 1)) {
                    acc.Contacts[i].Share__c = acc.Total__c - (shareEachContact * (contactNumber - 1));
                } else {
                    acc.Contacts[i].Share__c = shareEachContact;
                }
                conListToUpdate.add(acc.Contacts[i]);
            }
        }
    }

    update conListToUpdate;

}
1
  • The Math.floor part of this will round shareEachContact to the nearest integer which will give the incorrect result. e.g. * 2 contacts with a total of $1.99 results in $0 and $1.99 2 contacts with a total of $2.99 results in $1 and $1.99 Consider using [Decimal.divide] (developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/…) with the correct scale and rounding mode to produce the correct results
    – BarCotter
    Jul 23, 2021 at 16:49
0

If you need to divide a number that doesn't divide evenly, you'll need to use the math.mod operator. Then remove the excess. For example:

Integer totalToDivide = 50;
Integer numberOfContacts = 3;
Integer remainder = Math.mod(totalToDivide, numberOfContacts); //equals 2
Integer evenlyDivisible = totalToDivide - remainder;  //equals 48
Integer amountToAllocateToContacts = evenlyDivisible / numberOfContacts;
//then give one contact the remainder or discard if not needed
contact[0] += remainder

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .