0

This is the example I am attempting to follow: Link Here

I have the parent object Accounts with a field for the concatenated information labeled Systems_Pick_Text__c

My children objects of Opportunities with a multi-picklist of Systems_Pick__c.

I want to show all chosen system pick items from the child objects in one field, (Systems_Pick_Text__c ) on the parent object (Account).

Here is what I have currently in the Apex class.

public class SystemsPickText {
    {
        @InvocableMethod
        //Method that accepts the AccountIds from the Process and executes the de-dupe and update logic
        public static void updateSystemsPickInfo(List<Id> AccountIds){
            Id accountId = AccountIds[0];
            
            Account acc = [SELECT Id, Systems_Pick_Text__c FROM Account WHERE Id=: accountId];
            acc.Systems_Pick_Text__c = null;
            list<Opportunity> oppSystemsPick = new List<Opportunity>();
            oppSystemsPick = [SELECT Id, Systems_Pick__c, AccountId FROM Opportunity WHERE AccountId =: accountId];
            
            //Use of Set allows us to de-dupe System Pick field values on the Opp object
            if (oppSystemPick.size()>0){
                Set<String> systems = new Set<String>();
                for(Opportunity oppObj : oppSystemPick){
                    if(!String.isBlank(oppObj.Systems_Pick__c)){
                        systems.add(oppObj.Systems_Pick__c);
                    }
                }
            if (!systems.isEmpty()){
                for(String system:systems){
                    if(!String.isBlank(acc.Systems_Pick_Text__c)){
                        //Concatenation of all system values and dumping onto the Account field
                        acc.Systems_Pick_Text__c = acc.Systems_Pick_Text__c + ' , ' +system;
                    }
                    else{
                        acc.Systems_Pick_Text__c = system;
                    }
                }
                    
                }    
            }
            try{
                update acc;
            }catch(Exception e){
                system.debug('Exception:::'+e.getMessage());
            } 
            
        }
    }
}
2
  • Is the process builder created upon the opportunity object? Whenever an opp record is created or updated, it calls apex with tha parent AccountId? Commented Dec 1, 2020 at 17:42
  • @GabrielSerranoSalas - Yes, that is correct Commented Dec 1, 2020 at 17:43

1 Answer 1

1

Try the following code:

public class SystemsPickText {
@InvocableMethod
public static void updateSystemsPickInfo(List<Id> AccountIds) {
    Id accountId = AccountIds[0];

    // NOTE: compareTo method for Set collenction is case sensitive
    Set<String> systems = new Set<String>();
    for (Opportunity oppObj : [SELECT Id, Systems_Pick__c, AccountId FROM Opportunity WHERE AccountId = :accountId]) {
        if (String.isNotBlank(oppObj.Systems_Pick__c)) {
            systems.addAll(oppObj.Systems_Pick__c.split(';'));
        }
    }
    String accountSystems = '';
    for (String value : systems) {
        accountSystems += value + ';';
    }
    accountSystems = accountSystems.removeEnd(';');
    try {
        update new Account(Id = accountId, Systems_Pick_Text__c = accountSystems);
    } catch (Exception e) {
        System.debug('Exception: ' + e.getMessage());
    }
}

It should update the related account with all values selected in the child opps

3
  • I receive this error on line 15 - Identifier name is reserved: system Commented Dec 1, 2020 at 19:01
  • replace it with 'value' for instance, I have updated that part Commented Dec 1, 2020 at 19:02
  • 1
    This works perfectly - THANK YOU Commented Dec 1, 2020 at 20:56

You must log in to answer this question.

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