+ Start a Discussion
DhairyaDhairya 

Task Based Trigger

Hi, 

 

    A scenario is i am receiving values like below in text field at opp. line level for 7 products

 

      Qty 7 (2010-11-14)   or Qty 5 (2010-11-14)  Qty 2 (2010-11-14)  or Qty 4 (2010-11-14) Qty 2 (2010-11-14) Qty 1(2010-11-14)

 

   [ number of receiving date are vary so i am not sure that we can use workflow or not ]

 

 So, i wrote a trigger for that :

 

 

 

trigger OppotunityProductFulfillmentDateTrigger on OpportunityLineItem (before update) {
  
    // list<opportunityLineItem> lineItems = Trigger.new;
     
     user  u = [select username from user where lastname = 'view'];
     
     
    for (opportunityLineItem line : trigger.new)
    {
         if (line.Delivery_Details__c != NULL && (trigger.oldmap.get(line.id).Delivery_Details__c!= line.Delivery_Details__c))
         {
       
         Integer len = line.Delivery_Details__c.length();
        
         string s = line.Delivery_Details__c;
        
            String[] s1 = s.split('\\(');
            
            for(Integer i=1;i<=s1.size();i=i+2)
         {    
         s1[i]=s1[i].replace('\\)','');         
         task t= new task();
         t.subject='Training Required';
         t.ownerid= u.id;
         t.status='In Progress';
         t.priority='high';
         t.ActivityDate = date.valueof(s1[i].substring(0,10));
         t.whatid=line.OpportunityID;
          //  t.description+=s1[i].substring(0,10);
         insert t;
        
         }
    
         }
    }
    
    
It work fine when text field has one date say Qty 5  (2010-11-14). But it the value is  Qty2(2010-12-11) Qty3(2010-12-14)
it throw following error
OppotunityProductFulfillmentDateTrigger: execution of BeforeUpdate

caused by: System.ListException: List index out of bounds: 3

Trigger.OppotunityProductFulfillmentDateTrigger: line 21, column 14
I am not sure abt. this error.
Appreciate your response.
Dhairya

 

Best Answer chosen by Admin (Salesforce Developers) 
DhairyaDhairya

Hi Thanks for that,

 

     And really sorry for using variable name like this.

 

      As per logic i have to use index '1' instead of  '0'. so i < s1.size()-1 satisfied the requirement.

 

      Again very thankful for replying.

 

Thanks,

Dhairya

 

 

All Answers

*werewolf**werewolf*

Apex uses 0-indexed arrays.  You are starting at 1.  Seems like your loop should be more like

 

for(Integer i=0;i<s1.size();i++)

 

And as an aside you should probably use more descriptive variable names when you're doing stuff like this, otherwise it becomes hard to read very quickly.

DhairyaDhairya

Hi Thanks for that,

 

     And really sorry for using variable name like this.

 

      As per logic i have to use index '1' instead of  '0'. so i < s1.size()-1 satisfied the requirement.

 

      Again very thankful for replying.

 

Thanks,

Dhairya

 

 

This was selected as the best answer