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
Proposal ButtonProposal Button 

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;
}

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

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

khopskhops

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. . .

zachelrathzachelrath

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:

 

 

trigger CAFoppUpadate1 on Case (after update) {

Set<Id> caseIds = new Set<Id>();

for(Case c: Trigger.new) {
   if (c.Closed_won_opportunity__c == True) caseIds.add(c.Id);
}

if (!caseIds.isEmpty()) {

   List<CAF__c> lstCAFToUpdate = new List<CAF__c>();

   // Retrieving the CAF records in a SOQL for-loop is necessary
   //    if there might be more than 10000 records,
   //    and the retrieval is conducted through the more-optimized
   //    queryMore API routine.
   //    Also, I've added code so that you'll only update the records
   //        that actually need updated (better for avoiding DML Row limits)
   for (List<CAF__c> CAFList : 
         [select Id, test_caf__c from CAF__c where Case__c in :caseIds]) {
      for(CAF__c ca : CAFList) {
         if (ca.test_caf__c != true) {
            ca.test_caf__c = true;
            lstCAFToUpdate.add(ca);
         }
      }
   } 
   if (!lstCAFToUpdate.isEmpty()) update CAFList;
}

} // end trigger

 

 

Shashikant SharmaShashikant Sharma

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.

This was selected as the best answer
Proposal ButtonProposal Button

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

Shashikant SharmaShashikant Sharma

Your welcome,

 

If you want to read more about Apex Triggers Please'see : http://forceschool.blogspot.com/search/label/Apex%20Triggers