+ Start a Discussion
Mats ErikssonMats Eriksson 

SOQL query fails in trigger called class

I have a trigger that acts On INSERT Case and calls a class. This class in it's simplest form has been working quite well for some months.

 

Now I have expanded the functionality of the class to include some IF and  ELSE IF statements inside a FOR loop. This does not work because seemingly the FOR loop fails to enter. The code compiles nicely with no errors.

 

ccs is the trigger.new object passed into the class.

Debug messages in the first FOR loop confirms that there is case data available. The debug messages in the next FOR loop never executes let alone the IsIC part.

 

I use this methodology in many places and it normally works well (iterating through the trigger object pulling Id's into a list and then a FOR loop with an SOQL statement)

 

public with sharing class ChangeCaseFieldsOnFirstSave {
	public static void ChangeCaseFields(Case[] ccs){

		List<Case> CasesToUpdate = new List<Case>(); 
		List <Id> lstCases= new List<Id>();
		
		for (Case t:ccs){
				lstCases.add(t.Id);
		}
		
		for(Case c:[Select c.id, c.Subject, c.CustomerName__c, c.Status, c.IsSS__c, c.IsIc__c, c.IsKF__c, c.account.IsSS__c, c.account.IsIc__c, c.account.IsKF__c from  Case c where (c.Id in :lstCases)]) {	
			system.debug('#### - Start');
			if (c.account.IsSS__c==true) {
				c.IsSS__c=true;
				CasesToUpdate.add(c);
			}

			else if (c.account.IsIc__c==true){
				system.debug('#### - IsIC');
				c.IsIc__c=true;
				c.Subject=c.CustomerName__c;
				CasesToUpdate.add(c);
			}
			else if (c.account.IsKF__c==true){
				c.IsKF__c=true;
				CasesToUpdate.add(c);
			}
		}
		 update CasesToUpdate;
	}
}

 

 Anyone that could tell me what is wrong?

 

 

/Mats

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
shruthishruthi

@ Mats Eriksson

I think you are doing it in a before insert trigger. That is why you are getting a null. After Insert trigger will work, I believe. Coz, the Id gets set only after Inserting a record.

 

 

All Answers

SrikanthKuruvaSrikanthKuruva

can you please post me the full error that you are getting...

Mats ErikssonMats Eriksson

No errors. Just the absence of results.

 

The execution skips over the second FOR loop even though this is an extension of the Case INSERT trigger in the database and thus per definition there is data enough for at least one iteration in the loop -if the logic holds.

 

/Mats

SrikanthKuruvaSrikanthKuruva

can you try doing the following after the first for loop and check

system.debug(lstCases );

raseshtcsraseshtcs

Might sound crazy but could you once try it by removing the alias 'c' you have used in the query in the for loop. If that does not work this should

public with sharing class ChangeCaseFieldsOnFirstSave {
	public static void ChangeCaseFields(Case[] ccs){

		List<Case> CasesToUpdate = new List<Case>(); 
                List<Case> Casesqueried = new List<Case>(); 
		List <Id> lstCases= new List<Id>();
		
		for (Case t:ccs){
				lstCases.add(t.Id);
		}
		Casesqueried =[Select c.id, c.Subject, c.CustomerName__c, c.Status, c.IsSS__c, c.IsIc__c, c.IsKF__c, c.account.IsSS__c, c.account.IsIc__c, c.account.IsKF__c from  Case c where c.Id in :lstCases];

		for(Case c:Casesqueried) {	
			system.debug('#### - Start');
			if (c.account.IsSS__c==true) {
				c.IsSS__c=true;
				CasesToUpdate.add(c);
			}

			else if (c.account.IsIc__c==true){
				system.debug('#### - IsIC');
				c.IsIc__c=true;
				c.Subject=c.CustomerName__c;
				CasesToUpdate.add(c);
			}
			else if (c.account.IsKF__c==true){
				c.IsKF__c=true;
				CasesToUpdate.add(c);
			}
		}
		 update CasesToUpdate;
	}
}

 

Mats ErikssonMats Eriksson

@Srikanth,

 

I tried that and got null as the response. I added a debug line in the loop as well to see if I'm going crazy but it yields the customer name nicely while the second debug line yields null still.

 

for (Case t:ccs){
	lstCases.add(t.Id);
	system.debug('#### '+t.CustomerName__c);
	}
system.debug('#### '+lstCases );

 /Mats

 

Mats ErikssonMats Eriksson

@raseshtcs



 

I tried your solution but no luck.

 

/Mats

shruthishruthi

@ Mats Eriksson

I think you are doing it in a before insert trigger. That is why you are getting a null. After Insert trigger will work, I believe. Coz, the Id gets set only after Inserting a record.

 

 

This was selected as the best answer
Mats ErikssonMats Eriksson

@Shruthi

 

Bingo!!

 

Naive me thought the trigger.new object would contain Id's as well but of course not. I changed to AFTER INSERT and then it worked.

 

Thank you Shruthi anfd thank you all of you guys for your input. It was valuable!

 

 

/Mats