• CKN_1
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

I am running into the governor limit issue since I have the SQls inside the for loop. I am trying to optimize the code to get the case and owner information outside the for loop.

 

New to Apex development. Would appreciate some pointers on writing efficient SOQls outside the For loop in a trigger.

 

/* Trigger logic.. If transportation case type, and status = "awaiting internal response, case creator alias = "CS agents" and case owner = regional call center, MVC agent field = TASR agent, then assign then case back to the MVC agent *./

'

 

trigger TR_2 on Case (before update) {
   Case[] cases = Trigger.new;
   for (Case a:cases){ 
    string creatorid;
    string casecreatoriwdalias;
      String caseowner;
    string iWdAliasCaseOwner;
    String caseno;
    String err_msg = '';
    String MVCowner;
      String iWDMVCOwner;
                     
             creatorid = a.CreatedById;
          caseno = a.CaseNumber;
          
          case c = [select ownerid from case where casenumber = :caseno limit 1];
           caseowner = a.ownerid;
          MVCowner  = a.MVC_Owner__c;
    
         // get user alias information for the case creator   
    User userinfor = [select alias, iWD_Alias_Case_Owner__c, Iwd_Case_Creator__c from User where id = :creatorid];
    casecreatoriwdalias = userinfor.Iwd_Case_Creator__c;
    
      if (caseowner.substring(0,3) == '005'){
    // get case owner alias information
    User caseowneruserinfor = [select alias, iWD_Alias_Case_Owner__c, Iwd_Case_Creator__c from User where id = :caseowner limit 1];
    iWdAliasCaseOwner = caseowneruserinfor.iWD_Alias_Case_Owner__c;
     
     // check for status and SRCL Transportation record type and case owner and case creator alias
        if ((a.STATUS == 'Awaiting Internal Response'&& a.RecordTypeID == '012300000004rDZ')&& 
            (casecreatoriwdalias =='CAS' ||casecreatoriwdalias =='TASR/P1')&& 
                 (iWdAliasCaseOwner == 'DISP'|| iWdAliasCaseOwner == 'RCC'))  {
       a.IWD_Testing__c = 'trigger 2 enter the loop'; 
          }// status 'internal response'
                   
   if ((a.STATUS == 'Closed'&& a.RecordTypeID == '012300000004rDZ')&& 
            (MVCowner =='CAS' ||MVCowner =='TASR/P1')&& 
                 (iWdAliasCaseOwner == 'DISP'|| iWdAliasCaseOwner == 'RCC'))  {
       a.IWD_Testing__c = 'trigger2 working!!'; 
       a.ownerid = creatorid;  
       a.status = 'Awaiting internal response';   
        }// status 'closed'
                    } //if case owner 
      }// FOR LOOP
} // EOF

  • September 29, 2010
  • Like
  • 0

I am running into the governor limit issue since I have the SQls inside the for loop. I am trying to optimize the code to get the case and owner information outside the for loop.

 

New to Apex development. Would appreciate some pointers on writing efficient SOQls outside the For loop in a trigger.

 

/* Trigger logic.. If transportation case type, and status = "awaiting internal response, case creator alias = "CS agents" and case owner = regional call center, MVC agent field = TASR agent, then assign then case back to the MVC agent *./

'

 

trigger TR_2 on Case (before update) {
   Case[] cases = Trigger.new;
   for (Case a:cases){ 
    string creatorid;
    string casecreatoriwdalias;
      String caseowner;
    string iWdAliasCaseOwner;
    String caseno;
    String err_msg = '';
    String MVCowner;
      String iWDMVCOwner;
                     
             creatorid = a.CreatedById;
          caseno = a.CaseNumber;
          
          case c = [select ownerid from case where casenumber = :caseno limit 1];
           caseowner = a.ownerid;
          MVCowner  = a.MVC_Owner__c;
    
         // get user alias information for the case creator   
    User userinfor = [select alias, iWD_Alias_Case_Owner__c, Iwd_Case_Creator__c from User where id = :creatorid];
    casecreatoriwdalias = userinfor.Iwd_Case_Creator__c;
    
      if (caseowner.substring(0,3) == '005'){
    // get case owner alias information
    User caseowneruserinfor = [select alias, iWD_Alias_Case_Owner__c, Iwd_Case_Creator__c from User where id = :caseowner limit 1];
    iWdAliasCaseOwner = caseowneruserinfor.iWD_Alias_Case_Owner__c;
     
     // check for status and SRCL Transportation record type and case owner and case creator alias
        if ((a.STATUS == 'Awaiting Internal Response'&& a.RecordTypeID == '012300000004rDZ')&& 
            (casecreatoriwdalias =='CAS' ||casecreatoriwdalias =='TASR/P1')&& 
                 (iWdAliasCaseOwner == 'DISP'|| iWdAliasCaseOwner == 'RCC'))  {
       a.IWD_Testing__c = 'trigger 2 enter the loop'; 
          }// status 'internal response'
                   
   if ((a.STATUS == 'Closed'&& a.RecordTypeID == '012300000004rDZ')&& 
            (MVCowner =='CAS' ||MVCowner =='TASR/P1')&& 
                 (iWdAliasCaseOwner == 'DISP'|| iWdAliasCaseOwner == 'RCC'))  {
       a.IWD_Testing__c = 'trigger2 working!!'; 
       a.ownerid = creatorid;  
       a.status = 'Awaiting internal response';   
        }// status 'closed'
                    } //if case owner 
      }// FOR LOOP
} // EOF

  • September 29, 2010
  • Like
  • 0