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;
}