0

I have custom fields of total_number__c and total child contacts as Total_money__c in Account object and a custom field IndividualMoney__c in Contact. My aim is to distribute total amount to individual money equally between contacts.

trigger sharem on Contact(before insert, after update) {

    set<id> aid = new set<id>();

    for (Contact cont: trigger.new){
       if(cont.accountid != null)
        aid.add(cont.accountid);       
    }

    list <Account> acc = [select id, name, total_number__c, Total_money__c,(select id, name, IndividualMoney__c from contacts) from Account where id in: aid AND total_number__c!=Null AND Total_money__c !=Null ];
    list<contact> updConts = new list<contact>();
    for(account a : acc){
         decimal indivulaAmt = a.Total_money__c /(a.total_number__c+1);

        for(contact co : a.contacts){
            co.IndividualMoney__c = indivulaAmt;
            updConts.add(co);
        }


    }

    upsert updConts;
}
  • You need to have a trigger on Account object. Wherein on update of Total_Money__c you will query all the related contacts and then assign Individual_Money__c on Contact object by using your divide/distribute logic. After that update the contact list. Right now, you are writing a trigger on Contact object and updating contact list in it. – Hemant Jain Feb 20 '18 at 9:02
  • Thank you Hemant for your assistance. But my requirement says that total money is assigned at the very beginning of account creation , while associated contacts will be created thereafter. Is it not possible to do in the way I thought ? – Nicola thompson Feb 20 '18 at 9:13
  • @Nicolathompson, you've asked three very basic questions about this trigger and Apex syntax over the last few days. It's great that you're learning to code, but I wonder if it's a better plan for you to spend some time really learning the Apex language and working in Trailhead. Stack Exchange strongly rewards specific questions that show your research efforts. – David Reed Feb 20 '18 at 12:13
  • @HemantJain It can be done with a Contact trigger. Perhaps not in this exact way (I haven't read the code provided) but it can be done with a Contact trigger. It will be quite complex because it will need to account (no pun intended) for existing Contacts as well as other Contacts in the insert batch, but it can be done. – DavidSchach Feb 20 '18 at 22:10
1

Generally speaking, when you use a trigger to distribute information between child and parent objects, there are two approaches you can take:

  • Trigger on the child object
  • Trigger on the parent object

Both approaches have their issues.
If you have the trigger on Account, then the trigger on Account won't fire when a new Contact is associated to the Account.
If you have the trigger on Contact, then the trigger on Contact won't fire when Total_Money__c or Total_Number__c are updated.

You could use the trigger on one object to force the trigger on the other object to fire. However, that approach requires caution and thought (not to mention a second trigger) to avoid getting yourself into an infinite loop.

I believe one of the obstacles that you're facing here is that you're trying to propagate information both upwards and downwards in your object hierarchy (i.e. you need info from a child object on the parent object so you can update the child object).

Triggers alone can handle this situation, but I think a slightly different approach will work better here. Instead of using a trigger to evenly distribute something between child records, I'd suggest using a trigger on Contact (after insert, after update, and potentially after delete) to update the number of Contacts (I assume that's what Total_Number__c is for).

From there, a formula field on Contact to perform simple division (money/number) should suffice.

The benefits of this approach are:

  • You only need to write one trigger
  • The trigger you need to write only needs to address a simple issue (counting # of child records)
  • Formula fields are standard functionality (their behavior is well-known, and explicit testing is not required)
  • Information on Contact will always be accurate/up to date (because the formula field will be fed information from the corresponding Account)

The drawbacks to this approach:

  • Is a bad fit if you have the "allow contact to be related to multiple accounts" feature enabled
  • The cross-object formula uses an object reference (I believe the limit is 10 on any given SObject)
| improve this answer | |
  • Thank you Derek for this valuable peice of information ! Thanks alot – Nicola thompson Feb 20 '18 at 17:30
0

It was simpler than what I thought it would be . The piece of code which helped me is :

trigger sharem on Contact(before insert , before update) {

    set<id> aid = new set<id>();

    for (Contact cont: trigger.new){
       if(cont.accountid != null)
        aid.add(cont.accountid);       
    }

    Map<Id, Account> acc = new Map<Id, Account>([select id,name,total_number__c, Total_money__c from Account where id in : aid]);
    for (contact co : Trigger.new){
        Account ac = acc.get(co.accountid);
        decimal amount = ac.Total_money__c/(ac.total_number__c+1);

        co.IndividualMoney__c=amount;


    }
}

Thank you every one for your valuable suggestions . especially Derek

| improve this answer | |
  • 1
    While this may work for now, you should keep in mind that this trigger makes the assumption that you'll only ever add a single contact at a time to a given Account. Personally, such assumptions make me feel uncomfortable. I'll suggest that, on your next step with this trigger, you use a Map<Id, Integer> and a loop over trigger.new to figure out exactly how many Contacts are being added to each Account. – Derek F Feb 20 '18 at 19:44
  • 1
    You'll also probably want to update more than just the Contacts in trigger.new. As an example (one that I would encourage you turn into a unit test method), if you have an Account with Total_Money__c = 60, and 2 Contacts, each Contact should be given 30. If you add another contact, each Contact should be given 20, but I suspect that your trigger will leave you with 2 Contacts given 30 and one given 20. – Derek F Feb 20 '18 at 19:44
  • Yes it was not updating the other contact accounts. So modified a little and now it working perfectly fine. A big Thanks one again – Nicola thompson Feb 22 '18 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.