I have following trigger which runs on (after insert, after update, before update, before insert). The problem is SOQL query does not return anything on first time update. But when i update it second time then it return size. I am unable to get why it is happening. Need your support what could be possible reason ?


   trigger SharingCaseWithLA on Case (after insert, after update, before update, before insert) {

for(Case csRediness : trigger.new){

    List<Case> rediness = [Select id, Readiness_Activity_ID__r.ERS_ID__c, Readiness_Activity_ID__r.Course_Activity__r.Course_Name__c 
                           FROM Case WHERE Readiness_Activity_ID__c =: csRediness.Readiness_Activity_ID__c AND Readiness_Activity_ID__c != null];


  • What is csRediness.Readiness_Activity_ID__c? It is not declared anywhere – BarCotter Feb 10 '15 at 10:39
  • @Barcotter - csRediness is loop variable. – MIX DML Feb 10 '15 at 12:06
  • What loop? There is no loop in your code – BarCotter Feb 10 '15 at 16:49
  • @Barcotter- Please see updated code. – MIX DML Feb 11 '15 at 1:49
  • 2
    AHH! Queries in for loops! Code in Trigger body! At the very least you need to look into the Aggregate-Query-Update pattern. – Adrian Larson Feb 11 '15 at 2:10

This trigger is firing on before insert also - are you sure that you are only looking for result after initial update ?

Edit - if you only perform the soql within a if(trigger.isupdate) condition you may get a more predictable result.

  • Did you intend to comment ? – Samuel De Rycke Feb 10 '15 at 11:03
  • Is probably both a comment and answer... I expect answer is that is looking at result of before insert. Will clarify – CloudHugger Feb 10 '15 at 11:15
  • I have to update related object fields on Case. As far as i know you can not update related fields without querying it. – MIX DML Feb 10 '15 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.