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
Merry SMerry S 

Cannot get trigger to update account field

I am trying to update a field on the account that comes from a case. This trigger should look at all cases related to the account and determine if there is and R/Y/G string. If there is R it should set the account field to R, if there is no R and no G it should set it to Y and if the default would be G. I have worked on this for hours - I am new to this and I just don't know enough to get it to work. I have posted this on both here and the stack exchange. Thanks in advance for any help. Let me know if you need more detail.

 

trigger CaseStatusUpdate on Case (after update, after insert) {
List<ID> AccIDs = new List<ID>();
List<Account> updateAccList = new List<Account>();
For( Case c1 : Trigger.New){
 AccIDs.add(c1.AccountId);
}
Map<ID,Account> AccUpdateMap = new Map<ID,Account>();
for(Account acc : [Select ID , AccCaseStatusCalcGRY__c from Account where ID in:AccIDs])
{
 AccUpdateMap.put(acc.ID ,acc );

}
For(Case c1: Trigger.New){
Account updateacc =AccUpdateMap.get(c1.AccountID);
if (c1.CaseStatusCalcGRY__c == 'R') {
    updateacc.AccCaseStatusCalcGRY__c = 'R';
} else if (c1.CaseStatusCalcGRY__c != 'R' && c1.CaseStatusCalcGRY__c != 'G') {
    updateacc.AccCaseStatusCalcGRY__c = 'Y';
} else {
    updateacc.AccCaseStatusCalcGRY__c = 'G';
updateAccList.add(updateacc);
}
if(updateAccList!=null && updateAccList.size()>0)
update updateAccList;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

I missed declaring the AccIds variable.

 

Try this one.

 

trigger CaseStatusUpdate on Case (after insert, after update) {
	List<Account> updateAccList = new List<Account>();
	
	//Loop through the case and update the account with appropriate category
	set<ID> AccIds = new set<ID>();
	for( Case c1 : Trigger.New){
		if(c1.AccountId != null && c1.CaseStatusCalcGRY__c != null) 
			AccIds.add(c1.AccountID);
	}
	if(!AccIds.isEmpty()){
		list<Case> caseList = [select AccountID, CaseStatusCalcGRY__c from Case where CaseStatusCalcGRY__c != null and AccountId = : AccIds];
		map<Id, Account> AccountUpdates = new map<ID, Account>();
		for(Case cs : caseList){
			Account acc = AccountUpdates.get(cs.AccountID);
			if(acc == null)
				acc = new Account(Id = cs.AccountId);

			if(cs.CaseStatusCalcGRY__c == 'R')
				acc.AccCaseStatusCalcGRY__c = 'R';
			else if(cs.CaseStatusCalcGRY__c != 'G' && acc.AccCaseStatusCalcGRY__c != 'R')
				acc.AccCaseStatusCalcGRY__c = 'Y';
			else
				acc.AccCaseStatusCalcGRY__c = 'G';

			AccountUpdates.put(cs.AccountID, acc);
		}
		
		update AccountUpdates.Values();
	}
}

 

All Answers

Bhawani SharmaBhawani Sharma

It's simple

trigger CaseStatusUpdate on Case (after insert, after update) {
	
	List<Account> updateAccList = new List<Account>();
	
	//Loop through the case and update the account with appropriate category
	for( Case c1 : Trigger.New){
		if(c1.AccountId != null) {
			
			//set account id
			Account account = new Account(Id = c1.AccountId);
			
			if(c1.CaseStatusCalcGRY__c == 'R')
				account.AccCaseStatusCalcGRY__c = 'R';
			else if(c1.CaseStatusCalcGRY__c != 'G')
				account.AccCaseStatusCalcGRY__c = 'Y';
			else
				account.AccCaseStatusCalcGRY__c = 'G';
		
			//Add account in list
			updateAccList.add(account);
	}
	
	if(updateAccList.size() > 0)
		update updateAccList;	
}

 

Bhawani SharmaBhawani Sharma

One closing bracket was missing in earlier one

trigger CaseStatusUpdate on Case (after insert, after update) {
	
	List<Account> updateAccList = new List<Account>();
	
	//Loop through the case and update the account with appropriate category
	for( Case c1 : Trigger.New){
		if(c1.AccountId != null) {
			
			//set account id
			Account account = new Account(Id = c1.AccountId);
			
			if(c1.CaseStatusCalcGRY__c == 'R')
				account.AccCaseStatusCalcGRY__c = 'R';
			else if(c1.CaseStatusCalcGRY__c != 'G')
				account.AccCaseStatusCalcGRY__c = 'Y';
			else
				account.AccCaseStatusCalcGRY__c = 'G';
		
			//Add account in list
			updateAccList.add(account);
		}
	}
	
	if(updateAccList.size() > 0)
		update updateAccList;	
}

 

Merry SMerry S

Thanks! That worked, to a point. It is now updating the field on the acccount, but it does not appear to be looking at all cases. It will set to R/Y/G based on the newest case added to the account. I am not sure how to get it to look at all cases and make the decision.

 

Jerun JoseJerun Jose

This should work for you

 

trigger CaseStatusUpdate on Case (after insert, after update) {
	List<Account> updateAccList = new List<Account>();
	
	//Loop through the case and update the account with appropriate category
	for( Case c1 : Trigger.New){
		if(c1.AccountId != null && c1.CaseStatusCalcGRY__c != null) 
			AccIds.add(c1.AccountID);
	}
	if(!AccIds.isEmpty()){
		list<Case> caseList = [select AccountID, CaseStatusCalcGRY__c from Case where CaseStatusCalcGRY__c != null and AccountId = : AccIds];
		map<Id, Account> AccountUpdates = new map<ID, Account>();
		for(Case cs : caseList){
			Account acc = AccountUpdates.get(cs.AccountID);
			if(acc == null)
				acc = new Account(Id = cs.AccountId);

			if(cs.CaseStatusCalcGRY__c == 'R')
				acc.AccCaseStatusCalcGRY__c = 'R';
			else if(cs.CaseStatusCalcGRY__c != 'G' && acc.AccCaseStatusCalcGRY__c != 'R')
				acc.AccCaseStatusCalcGRY__c = 'Y';
			else
				acc.AccCaseStatusCalcGRY__c = 'G';

			AccountUpdates.put(cs.AccountID, acc);
		}
		
		update AccountUpdates.Values();
	}
}

 

There is still a bit of work to be done to handle when case records are deleted and undeleted, but hopefully you should be able to figure those out.

Merry SMerry S

Thank You. I am getting this error now.
Error: Compile Error: Variable does not exist: AccIds at line 10 column 134

Jerun JoseJerun Jose

I missed declaring the AccIds variable.

 

Try this one.

 

trigger CaseStatusUpdate on Case (after insert, after update) {
	List<Account> updateAccList = new List<Account>();
	
	//Loop through the case and update the account with appropriate category
	set<ID> AccIds = new set<ID>();
	for( Case c1 : Trigger.New){
		if(c1.AccountId != null && c1.CaseStatusCalcGRY__c != null) 
			AccIds.add(c1.AccountID);
	}
	if(!AccIds.isEmpty()){
		list<Case> caseList = [select AccountID, CaseStatusCalcGRY__c from Case where CaseStatusCalcGRY__c != null and AccountId = : AccIds];
		map<Id, Account> AccountUpdates = new map<ID, Account>();
		for(Case cs : caseList){
			Account acc = AccountUpdates.get(cs.AccountID);
			if(acc == null)
				acc = new Account(Id = cs.AccountId);

			if(cs.CaseStatusCalcGRY__c == 'R')
				acc.AccCaseStatusCalcGRY__c = 'R';
			else if(cs.CaseStatusCalcGRY__c != 'G' && acc.AccCaseStatusCalcGRY__c != 'R')
				acc.AccCaseStatusCalcGRY__c = 'Y';
			else
				acc.AccCaseStatusCalcGRY__c = 'G';

			AccountUpdates.put(cs.AccountID, acc);
		}
		
		update AccountUpdates.Values();
	}
}

 

This was selected as the best answer
Merry SMerry S
Thanks! Life Saver!!!!