function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anoop Patel 11Anoop Patel 11 

Update parent record field based on various child records with different criteria

I'm trying to set a value on the parent record if a child record is live. However the field on the Account is pick list and there are 4 different record types which means there needs to be criteria before updating the parent. I.e. if child record is record type a, status is live and part of a certain department then update account to A.

Essentially, I just need to find at least one child record that matches the specified criteria even if there are multiple child records that match the criteria and update the pick list to a value. If there is a live child record with record type A and B that are both live and match the other criteria then A takes priority and the field on Account should update to A. If A is inactive then it should show B, and if B is inactive it should show A.

My trigger for one business line is below;

trigger UpdateBusinessHighlights on Activity__c (after delete, after insert, after update){

Map<ID, Account> parentAccs = new Map<ID, Account>();
Map<ID, Account> parentAccsClients = new Map<ID, Account>();
List<Id> MAIds = new List<Id>();
List<Id> MAClientIds = new List<Id>();
Boolean isExists = false;

if(trigger.isInsert || trigger.isUpdate){
for (Activity__c MA: Trigger.new) {
MAIds.add(MA.CMember_ID__c);
MAClientIds.add(MA.Participant__c);
}
}

if(trigger.isDelete){
for(Activity__c MA: trigger.old){
MAIds.add(MA.CMember_ID__c);
MAClientIds.add(MA.Participant__c);
}
}

parentAccs = new Map<Id, Account>([SELECT id, CEStatus__c,CDSCStatus__c,DStatus__c,FCStatus__c,FIStatus__c,SCStatus__c FROM Account WHERE Id IN :MAIds]);


parentAccsClients = new Map<Id, Account>([SELECT id, CEStatus__c,CDSCStatus__c,DStatus__c,FCStatus__c,FIStatus__c,SCStatus__c FROM Account WHERE Id IN :MAClientIds]);

for (Activity__c MA: Trigger.new){
isExists = False;
if(MA.Status__c == 'Live' && MA.Business_Line__c == 'CE' && (MA.RecordTypeid == '01220000000JTeP' || MA.RecordTypeid == '01220000000Q6r5')){
Account myParentAcc = parentAccs.get(MA.CMember_ID__c);
myParentAcc.CEStatus__c = 'Live A';
isExists = True;
}
else if(MA.Status__c == 'Live' && MA.Business_Line__c == 'CE' && (MA.RecordTypeid == '01220000000JTZA' || MA.RecordTypeid == '01220000000JTZB')){
Account myParentAccClients = parentAccsClients.get(MA.Participant__c);
myParentAccClients.CEStatus__c = 'Live B';
isExists = True;
}
else if(isExists == false && MA.Business_Line__c == 'CE' && (MA.RecordTypeid == '01220000000JTeP' || MA.RecordTypeid == '01220000000Q6r5')){
Account myParentAcc = parentAccs.get(MA.CMember_ID__c);
myParentAcc.CEStatus__c = 'Prospect - A;
}
else if(isExists == false && MA.Business_Line__c == 'CE' && (MA.RecordTypeid == '01220000000JTZA' || MA.RecordTypeid == '01220000000JTZB')){
Account myParentAccClients = parentAccsClients.get(MA.Participant__c);
myParentAccClients.CEStatus__c = 'Prospect – B’;
}
}
if(!parentAccs.isEmpty())
{
update parentAccs.values();
}
if(!parentAccsClients.isEmpty())
{
update parentAccsClients.values();
}
}