0

Can you help me, on how to continue copying from Participant to Account wherein fields are the Year and Goal, I am struggling in last part which is the year and goal this is as follows child is Participation > Personnel > Acccount, I got the ID's of the two

public class AccountParticipateTriggerHandler {
    public void CopytoAccount(List<Participation__c> partic) {
        List<id> parentIDstoUpdate = new list<id>();
        Map<Id,Account> accountMap = new Map<Id,Account>();
        Map<Id, Set<String>> mapParentIdtoString = new Map<Id, Set<String>>();
        Map<Id, Set<String>> mapParticipation = new Map<Id, Set<String>>();

        string str;

        for (Participation__c co : partic) {
            parentIDstoUpdate.add(co.AFName__c); 
            system.debug('parentIDstoUpdate' + parentIDstoUpdate);
        }

        for (Participation__c co: [
            SELECT id,Year__c, Goals__c 
            FROM Participation__c 
            WHERE AFName__c in : parentIDstoUpdate
       ]) {
            system.debug('oppco' + co);

            if (!mapParentIdtoString.containsKey(co.AFName__c)){
                mapParentIdtoString.put(co.AFName__c, new Set<String>());
            }

            mapParentIdtoString.get(co.AFName__c);
        }

        List<Personnel__c > personnels = [
            SELECT id, parentAccount__c  
            FROM Personnel__c 
            SHERE ID =: parentIDstoUpdate
        ];

        for (Personnel__c pers: personnels){
            system.debug('personnels' + pers);

            if (!mapParticipation.containsKey(pers.parentAccount__c )){
                mapParticipation.put(
                    pers.parentAccount__c, new Set<String>()
                );
            }

            mapParticipation.get(pers.parentAccount__c);
        }
    }
}
2

If I understand correctly you want to take the values of the year__c and goal__c field from the participation__c record through the personnel__c record and put it into a field on the parent account record.

I'm assuming the field name on account matches the field name on the Participation record:

public class AccountParticipateTriggerHandler {
    public void CopytoAccount(List<Participation__c> partic) {
        List<Account> accountsToUpdate = new List<Account>()

        partic = [
            SELECT Id, Year__c, Goal__c, AFName__r.ParentAccount__c
            FROM Participation__c
            WHERE Id IN :partic
        ];

        for (Participation__c p : partic) {
            Account a = new Account(
                Id = p.AFName__r.ParentAccount__c,
                Year__c = p.Year__c,
                Goal__c = p.Goal__c
            );

            accountsToUpdate.add(a);
        }

        update accountsToUpdate;
    }
}

Make sure this runs in your after trigger.

  • I am having an error with update accountsToUpdate(); it says Error: Compile Error: Method does not exist or incorrect signature: void accountsToUpdate() from the type AccountParticipateTriggerHandler at line 19 column 16 – Cloud Aug 23 '18 at 21:32
  • Sorry updated the answer. Thats what happens when you type too fast :P – gNerb Aug 23 '18 at 21:36
  • Thank you so much! I learn something new from you, based from my long code T_T – Cloud Aug 23 '18 at 21:40
  • 1
    My first draft was pretty long too but once I had something that seemed functional I reduced it to as simple as I could get it. I think that's a good rule for everyone to follow. – gNerb Aug 23 '18 at 21:42

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.