You need to sign in to do that
Don't have an account?
trigger not firing
Hi All
I wrote a simple trigger on case. I am able to save the trigger but it is not working when i am updating the records. I have a checkbox on Case whenever this checkbox becomes true the checkbox in CAF(Custom object) should become true. For this i wrote this trigger. I think i had made mistake in the where clause of the SOQL query. I am mentioning in Red. Please let me know. the relation between CAF__c and Case is lookup.
trigger CAFoppUpadate1 on Case (after update)
{
List <CAF__c> CAFList;
List <Id> CaseIdList = new List <Id>();
for(Case c:Trigger.new)
{
if (c.Closed_won_opportunity__c == True)
caseIdList.add(c.Id);
}
CAFList = [select Id, test_caf__c from CAF__c where ID in :Caseidlist];
for(CAF__c ca : CAFList)
{
if (ca.test_caf__c != true)
ca.test_caf__c = true;
}
update CAFList;
}
Do not use
where ID in :Caseidlist];
Change it to where referneceFieldAPIName in :Caseidlist];
referneceFieldAPIName is the name of reference field on CAF object for case object.
This will fix your issue.
All Answers
Your where clause looks okay. when you create the CAF__C List, instead of:
List<CAF__c> CAFList;
CAFLIst=[Selected ID,test_caf__c from CAF__c where ID in:Caseidlist];
try:
List<CAF__c> CAFLIST = new List<CAF__C>( [Select ID,test_caf__c
from CAF__C
where ID in:CaseIDList]);
My guess is since you didn't declare the CAFList as a new object at any point that it was returning nulls to your
caseidlist and so your update was returning nothing. Could be wrong though. . .
You are correct, the problem is in the WHERE clause of your SOQL query. I'm guessing that there is a field on your CAF__c object called something like "Case__c" or "Related_Case_Id__c". This is the field you need to be limiting your SOQL statement on. Right now you are trying to retrieve all CAF__c records whose Ids are equal to the Ids of Case records --- which is impossible, and will understandably yield you no results. Your query should look like this:
Do not use
where ID in :Caseidlist];
Change it to where referneceFieldAPIName in :Caseidlist];
referneceFieldAPIName is the name of reference field on CAF object for case object.
This will fix your issue.
Thanks guys got the solution i did a mistake in the where clause. The related list in the CAF__c is Related_SSR__c so my SOQL query is
List<CAF__c> CAFLIST = new List<CAF__C>[Select ID,test_caf__c from CAF__c where Related_SSR__c in:CaseIdList]);
Appreciate your help
Your welcome,
If you want to read more about Apex Triggers Please'see : http://forceschool.blogspot.com/search/label/Apex%20Triggers