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
RstrunkRstrunk 

ERROR

Hi everyone, 

 

    I've been playing around with an apex trigger and class that will basically create a rollup field that counts the number of subCases that are related to a ParentCase.  Since I am new to APEX I am having a tough time.  I can get the trigger to work fine when I am not trying to "Bulkify" it.  But once I started trying this thing got a little out of hand lol.  Anyway HEre is the trigger and the class and the error I am getting.  Any help would be greatly appreciated.

 

 

ERROR: 

Error:Apex trigger NAME caused an unexpected exception, contact your administrator: NAME: execution of BeforeUpdate caused by: System.StringException: Invalid id: : Trigger.NAME: line 5, column 1

 

 

 

TRIGGER

 

Trigger NAME on Case (before update){
	List<String> parentIDs = new List<ID>();

	for(Integer i = 0; i < trigger.new.size(); i++){
		if(trigger.new[i].Standard_Change_Related_Case__c != null && trigger.new[i].Standard_Change_Related_Case__c != ''){
			parentIDs.add(trigger.new[i].Standard_Change_Related_Case__c);
		}
	}
	
	if(parentIDs.size() > 0 && parentIDs.size() != null){
		testClass.findAndUpdate(parentIDs);
	}
}

 

 

CLASS

Public Class testClass{	

	public static void findAndUpdate(List<String> parentIDs){
		list <case> casesTOupdate = new list <case>();
		list <case> subCaseList = new list<case>();
		
		if (parentIDs.size() > 0 && parentIDs.size() != null){
			For(integer i = 0; i < parentIDs.size(); i++){
				if(i < 100){ //limits the number of iterations for governor limits on SOQL queries
					for(list<Case> tmpCaseList: [Select id, Standard_Change_Request_Iteration__c 
												FROM Case c 
												WHERE c.Standard_Change_Related_Case__r.id =: parentIDs.get(i)]){
												
						subCaseList.addAll(tmpCaseList);
					}
					if(subCaseList.size() > 0 && subCaseList.size() != null){
						integer recordCount = subCaseList.size() + 1;
						for(integer h = 0; h < subCaseList.size(); h++){
							subCaseList.get(h).Standard_Change_Request_Iteration__c = recordCount;
						}
						case tmpCase = [Select id, Standard_Change_Request_Iteration__c 
										FROM Case c 
										WHERE c.id =: parentIDs.get(i)];
										
						tmpCase.Standard_Change_Request_Iteration__c = recordCount;
						subCaseList.add(tmpCase);
						casesTOupdate.addAll(subCaseList);
					}
					subCaseList.clear();
				}
			}			
			update casesTOupdate;
		}
	}
}

 

 

Sorry for the bad Formatting, When I paste it over it gets all screwy.

simple-forcesimple-force

Hi Rstrunk,

 

you can try to use this project: https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries

 

cu

Christian

SaraagSaraag

You have List<String> parentIDs = new List<ID>() and in the If statement you're checking for !='' ; '' is not a valid Id. I would remove !='' check from the if clause.

 

Trigger NAME on Case (before update){
List<String> parentIDs = new List<ID>();

for(Integer i = 0; i < trigger.new.size(); i++){
if(trigger.new[i].Standard_Change_Related_Case__c != null){
parentIDs.add(trigger.new[i].Standard_Change_Related_Case__c);
}
}

if(parentIDs.size() > 0 && parentIDs.size() != null){
testClass.findAndUpdate(parentIDs);
}
}

 

Saraag

RstrunkRstrunk

Thanks for the response.  Yea I will admit I have made a lot of changes to the code trying to figure out what the problem is, and at some point I forgot to change the data type in the list constructor.  Fixed, Thanks.  Also, I will remove the not equals check and see what happens.  If this is it I will come back and mark this as the solution.  

 

 

 

Thanks, 

SaraagSaraag

np. so did  removing !=''  help?