+ Start a Discussion
freddyloufreddylou 

HELP - such a simple example of updating why doesn't it work - 3rd related posting!

I think this is a buikify problem -

but confused how to do this - it is such a simple trigger.

 

it works one off - and u to 47 of a list of 247  -

 

i am not sure if I should use list or map - I took the 501 class even and still am very confused.

 

this blows up with a

updateappnew: System.Exception: Too many script statements: 50001

 

 

 

trigger updateappnew on Contact (before update, before insert) {

 


 
   //Loop through all records in the Trigger.new collection
   integer count = 18;
   integer s=64;
 
 for(Contact a: Trigger.new){

try {
   
if (a.clientparams__c.length()==18) {
  string buildbinary='';
    Integer i = 2;
 
    
    do  {
            
    if (a.clientparams__c.substring(i,i+1) == '0') {
    buildbinary=buildbinary+'0000';
    } if (a.clientparams__c.substring(i,i+1) == '1'){
    buildbinary=buildbinary+'0001';
    } if (a.clientparams__c.substring(i,i+1) == '2'){
    buildbinary=buildbinary+'0010';
    } if (a.clientparams__c.substring(i,i+1) == '3'){
    buildbinary=buildbinary+'0011';
    } if (a.clientparams__c.substring(i,i+1) == '4'){
    buildbinary=buildbinary+'0100';
    } if (a.clientparams__c.substring(i,i+1) == '5'){
    buildbinary=buildbinary+'0101';
    } if (a.clientparams__c.substring(i,i+1) == '6'){
    buildbinary=buildbinary+'0110';
    } if (a.clientparams__c.substring(i,i+1) == '7'){
        buildbinary=buildbinary+'0111';
    } if (a.clientparams__c.substring(i,i+1) == '8'){
    buildbinary=buildbinary+'1000';    } if
    (a.clientparams__c.substring(i,i+1) == '9'){
    buildbinary=buildbinary+'1001';
    } if (a.clientparams__c.substring(i,i+1) == 'A'){
    buildbinary=buildbinary+'1010';
      } if (a.clientparams__c.substring(i,i+1) == 'B'){
    buildbinary=buildbinary+'1011';
        } if (a.clientparams__c.substring(i,i+1) == 'C'){
    buildbinary=buildbinary+'1100';
        } if (a.clientparams__c.substring(i,i+1) == 'D'){
    buildbinary=buildbinary+'1101';
    } if (a.clientparams__c.substring(i,i+1) == 'E'){
    buildbinary=buildbinary+'1110';
    } if (a.clientparams__c.substring(i,i+1) == 'F'){
    buildbinary=buildbinary+'1111';    }
    i++;  system.debug(i);
} while (i<count); //while loop ends here
   
if (buildbinary.substring(s-3,s-2)=='1')  a.Basket_Trader_On__c=true;                           
else a.Basket_Trader_On__c=false;                           
if (buildbinary.substring(s-4,s-3)=='1')     a.Market_Maker_On__c=true;                             
else a.Market_Maker_On__c=false;                        
if (buildbinary.substring(s-5,s-4)=='1')    a.Spread_Trader_On__c=true;                           
else a.Spread_Trader_On__c=false;                           
if (buildbinary.substring(s-6,s-5)=='1')    a.Trade_Ripper_On__c=true;                            
else a.Trade_Ripper_On__c=false;                        
if (buildbinary.substring(s-7,s-6)=='1')    a.Time_Slicer_on__c=true;                             
else a.Time_Slicer_on__c=false;                         
if (buildbinary.substring(s-8,s-7)=='1')    a.Managed_Order_Admin_On__c=true;                             
else a.Managed_Order_Admin_On__c=false;                         
if (buildbinary.substring(s-9,s-8)=='1')    a.Exempt_from_short_locate_id_On__c=true;                             
else a.Exempt_from_short_locate_id_On__c=false;                         
if (buildbinary.substring(s-10,s-9)=='1')    a.Order_Ticket_On__c=true;                           
else a.Order_Ticket_On__c=false;                        
if (buildbinary.substring(s-11,s-10)=='1')    a.Wave_Grid_On__c=true;                             
else a.Wave_Grid_On__c=false;                           
if (buildbinary.substring(s-12,s-11)=='1')    a.News_Viewer_On__c=true;                           
else a.News_Viewer_On__c=false;                         
if (buildbinary.substring(s-13,s-12)=='1')    a.Equity_Quote_Cube_On__c=true;                             
else a.Equity_Quote_Cube_On__c=false;                           
if (buildbinary.substring(s-14,s-13)=='1')    a.Option_Quote_Cube_On__c=true;                             
else a.Option_Quote_Cube_On__c=false;                           
if (buildbinary.substring(s-15,s-14)=='1')    a.Quote_Grid_On__c=true;                            
else a.Quote_Grid_On__c=false;                          
if (buildbinary.substring(s-16,s-15)=='1')    a.Quote_Explorer_On__c=true;                            
else a.Quote_Explorer_On__c=false;                          
if (buildbinary.substring(s-17,s-16)=='1')    a.Equity_Time_and_Sales_On__c=true;                             
else a.Equity_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-18,s-17)=='1')    a.Option_Time_and_Sales_On__c=true;                             
else a.Option_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-26,s-25)=='1')    a.Instant_Messenger_On__c=true;                             
else a.Instant_Messenger_On__c=false;                            

   
    
  
    a.testbinary__c = buildbinary;
   
} // end of not valid hex number
}   catch(Exception e) { //if error is encountered
      System.Debug('Trigger failed: '+e.getMessage()); }//write error to the debug log

}
}

MandyKoolMandyKool

Hi,

This problem is occuring as trigger is crossing the script statements governor limit.

 

You should try to optimize your code and it will start working.

 

Thanks,

Kulkarni.

freddyloufreddylou

My code seems optimzed.  It is so straightforward.  How do I make it more optimal?????

 

1. I loop through all new records using

 

2.convert a records hexadecimal number to binary - using a loop - only way to do it.

 

3. I parse  the binary number to figure out what applications are turned on

 

I was thinking maybe I needed to use map or list - somehow is that the way to optimize it?????

 

I could not figure out how to do this. 

 

 

 

 

rscottrscott

At a first glance, the if statements in your first do-while loop are all individual if statements, not elses. Therefore even if the first one validates true, you will still run all the rest of them even though it appears that none of the others can possibly be true after you find one that is.

 

To cut script statements even further, I would think using a map of hex to binary would make that loop even tighter.

freddyloufreddylou

amazing !!!

 

adding the 'else' statements made it work. 

 

I know it is more eloquent using a map??

 

any idea how to do this?

rscottrscott

Syntax may not be 100%. This should give you the idea though.

 

 

Map<string, string> hexBinaryMap = new Map<string, string>();
// TODO: populate map
// ex. key / value:  '0' / '0000', '1' / '0001', ...

do {
	buildbinary=buildbinary+ hexBinaryMap.get(a.clientparams__c.substring(i,i+1));
} while (i<count);

 

 

freddyloufreddylou

this is crazy - never tried the map yet - but the dataloader now runs error free uploading all records BUT it looks like the trigger is not working.  the only way I can convert a hex to binary and parse as if I go the long way and bring up a contact record hit - edit - save

which is not what I want to do.

it worked fine when 47 out of 200 were successses now doesn't work when 247 out of 247 are succcess.

note: i built this in the sandbox and are running it now in the sandbox - but production should be the same - i will  try deploying to see if makes a difference

code:

trigger updateappnew on Contact (before update, before insert) {

 


 
   //Loop through all records in the Trigger.new collection
   integer count = 18;
   integer s=64;
 
 for(Contact a: Trigger.new){

try {
   
if (a.clientparams__c.length()==18) {
  string buildbinary='';
    Integer i = 2;
 
    
    do  {
            
    if (a.clientparams__c.substring(i,i+1) == '0')
    buildbinary=buildbinary+'0000';
    else  if (a.clientparams__c.substring(i,i+1) == '1')
    buildbinary=buildbinary+'0001';
    else if (a.clientparams__c.substring(i,i+1) == '2')
    buildbinary=buildbinary+'0010';
    else if (a.clientparams__c.substring(i,i+1) == '3')
    buildbinary=buildbinary+'0011';
    else if (a.clientparams__c.substring(i,i+1) == '4')
    buildbinary=buildbinary+'0100';
    else if (a.clientparams__c.substring(i,i+1) == '5')
    buildbinary=buildbinary+'0101';
    else if (a.clientparams__c.substring(i,i+1) == '6')
    buildbinary=buildbinary+'0110';
    else if (a.clientparams__c.substring(i,i+1) == '7')
        buildbinary=buildbinary+'0111';
    else if (a.clientparams__c.substring(i,i+1) == '8')
    buildbinary=buildbinary+'1000';   
    else if (a.clientparams__c.substring(i,i+1) == '9')
    buildbinary=buildbinary+'1001';
    else if (a.clientparams__c.substring(i,i+1) == 'A')
    buildbinary=buildbinary+'1010';
    else if (a.clientparams__c.substring(i,i+1) == 'B')
    buildbinary=buildbinary+'1011';
    else if (a.clientparams__c.substring(i,i+1) == 'C')
    buildbinary=buildbinary+'1100';
    else if (a.clientparams__c.substring(i,i+1) == 'D')
    buildbinary=buildbinary+'1101';
    else if (a.clientparams__c.substring(i,i+1) == 'E')
    buildbinary=buildbinary+'1110';
    else if (a.clientparams__c.substring(i,i+1) == 'F')
    buildbinary=buildbinary+'1111';   
    i++;  system.debug(i);
} while (i<count); //while loop ends here


    if (buildbinary.substring(s-3,s-2)=='1')  a.Basket_Trader_On__c=true;                           
else a.Basket_Trader_On__c=false;                           
if (buildbinary.substring(s-4,s-3)=='1')     a.Market_Maker_On__c=true;                             
else a.Market_Maker_On__c=false;                        
if (buildbinary.substring(s-5,s-4)=='1')    a.Spread_Trader_On__c=true;                           
else a.Spread_Trader_On__c=false;                           
if (buildbinary.substring(s-6,s-5)=='1')    a.Trade_Ripper_On__c=true;                            
else a.Trade_Ripper_On__c=false;                        
if (buildbinary.substring(s-14,s-13)=='1')    a.Option_Quote_Cube_On__c=true;                             
else a.Option_Quote_Cube_On__c=false;                           
if (buildbinary.substring(s-15,s-14)=='1')    a.Quote_Grid_On__c=true;                            
else a.Quote_Grid_On__c=false;                          
if (buildbinary.substring(s-16,s-15)=='1')    a.Quote_Explorer_On__c=true;                            
else a.Quote_Explorer_On__c=false;                          
if (buildbinary.substring(s-17,s-16)=='1')    a.Equity_Time_and_Sales_On__c=true;                             
else a.Equity_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-18,s-17)=='1')    a.Option_Time_and_Sales_On__c=true;                             
else a.Option_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-26,s-25)=='1')    a.Instant_Messenger_On__c=true;                             
else a.Instant_Messenger_On__c=false;                           
if (buildbinary.substring(s-27,s-26)=='1')    a.Allocator_On__c=true;                             
else a.Allocator_On__c=false;                           
if (buildbinary.substring(s-28,s-27)=='1')    a.Instant_Messenger_Admin_On__c=true;                           
else a.Instant_Messenger_Admin_On__c=false;                         
if (buildbinary.substring(s-29,s-28)=='1')    a.Report_Trade_On__c=true;                              
else a.Report_Trade_On__c=false;                        
if (buildbinary.substring(s-30,s-29)=='1')    a.Adjust_Position_On__c=true;                           
else a.Adjust_Position_On__c=false;                         
if (buildbinary.substring(s-31,s-30)=='1')    a.Order_Grid_On__c=true;                            
else a.Order_Grid_On__c=false;                          
if (buildbinary.substring(s-32,s-31)=='1')    a.Position_Grid_On__c=true;                             
else a.Position_Grid_On__c=false;                           
if (buildbinary.substring(s-33,s-32)=='1')    a.Percentage_Order_On__c=true;                              
else a.Percentage_Order_On__c=false;                        
if (buildbinary.substring(s-34,s-33)=='1')    a.Override_Give_Up_On__c=true;                              
else a.Override_Give_Up_On__c=false;                        
if (buildbinary.substring(s-35,s-34)=='1')    a.Proprietary_Theoriticals_On__c=true;                              
else a.Proprietary_Theoriticals_On__c=false;                        
if (buildbinary.substring(s-36,s-35)=='1')    a.Vol_Trader_On__c=true;                            
else a.Vol_Trader_On__c=false;                          
if (buildbinary.substring(s-37,s-36)=='1')    a.Override_CMTA_On__c=true;                             
else a.Override_CMTA_On__c=false;                           
if (buildbinary.substring(s-38,s-37)=='1')    a.Override_Account_Type_On__c=true;                             
else a.Override_Account_Type_On__c=false;                           
if (buildbinary.substring(s-39,s-38)=='1')    a.EOP_Trader_On__c=true;                            
else a.EOP_Trader_On__c=false;                          
if (buildbinary.substring(s-40,s-39)=='1')    a.Sector_Viewer_On__c=true;                             
else a.Sector_Viewer_On__c=false;                           
if (buildbinary.substring(s-41,s-40)=='1')    a.Manual_Liquidity_On__c=true;                              
else a.Manual_Liquidity_On__c=false;                        
if (buildbinary.substring(s-42,s-41)=='1')    a.Spread_Book_On__c=true;                           
else a.Spread_Book_On__c=false;                         
if (buildbinary.substring(s-43,s-42)=='1')    a.Spread_Builder_On__c=true;                            
else a.Spread_Builder_On__c=false;                          
if (buildbinary.substring(s-44,s-43)=='1')    a.Facilition_Orders_On__c=true;                             
else a.Facilition_Orders_On__c=false;                           
if (buildbinary.substring(s-45,s-44)=='1')    a.Instant_Messenger_Support_On__c=true;                             
else a.Instant_Messenger_Support_On__c=false;                           
if (buildbinary.substring(s-46,s-45)=='1')    a.HLOC_Chart_On__c=true;                            
else a.HLOC_Chart_On__c=false;                          
if (buildbinary.substring(s-47,s-46)=='1')    a.Imbalance_Viewer_On__c=true;                              
else a.Imbalance_Viewer_On__c=false;                                                 
if (a.Demo_Enabled__c) a.Demo_Status__c='Active'; else a.Demo_Status__c='Canceled-Expired';
   
 
    a.testbinary__c = buildbinary;
   
} // end of not valid hex number
}   catch(Exception e) { //if error is encountered
      System.Debug('Trigger failed: '+e.getMessage()); }//write error to the debug log

}
}

 

freddyloufreddylou

this is crazy - never tried the map yet - but the dataloader now runs error free uploading all records BUT it looks like the trigger is not working.  the only way I can convert a hex to binary and parse as if I go the long way and bring up a contact record hit - edit - save

which is not what I want to do.

it worked fine when 47 out of 200 were successses now doesn't work when 247 out of 247 are succcess.

note: i built this in the sandbox and are running it now in the sandbox - but production should be the same - i will  try deploying to see if makes a difference

code:

trigger updateappnew on Contact (before update, before insert) {

 


 
   //Loop through all records in the Trigger.new collection
   integer count = 18;
   integer s=64;
 
 for(Contact a: Trigger.new){

try {
   
if (a.clientparams__c.length()==18) {
  string buildbinary='';
    Integer i = 2;
 
    
    do  {
            
    if (a.clientparams__c.substring(i,i+1) == '0')
    buildbinary=buildbinary+'0000';
    else  if (a.clientparams__c.substring(i,i+1) == '1')
    buildbinary=buildbinary+'0001';
    else if (a.clientparams__c.substring(i,i+1) == '2')
    buildbinary=buildbinary+'0010';
    else if (a.clientparams__c.substring(i,i+1) == '3')
    buildbinary=buildbinary+'0011';
    else if (a.clientparams__c.substring(i,i+1) == '4')
    buildbinary=buildbinary+'0100';
    else if (a.clientparams__c.substring(i,i+1) == '5')
    buildbinary=buildbinary+'0101';
    else if (a.clientparams__c.substring(i,i+1) == '6')
    buildbinary=buildbinary+'0110';
    else if (a.clientparams__c.substring(i,i+1) == '7')
        buildbinary=buildbinary+'0111';
    else if (a.clientparams__c.substring(i,i+1) == '8')
    buildbinary=buildbinary+'1000';   
    else if (a.clientparams__c.substring(i,i+1) == '9')
    buildbinary=buildbinary+'1001';
    else if (a.clientparams__c.substring(i,i+1) == 'A')
    buildbinary=buildbinary+'1010';
    else if (a.clientparams__c.substring(i,i+1) == 'B')
    buildbinary=buildbinary+'1011';
    else if (a.clientparams__c.substring(i,i+1) == 'C')
    buildbinary=buildbinary+'1100';
    else if (a.clientparams__c.substring(i,i+1) == 'D')
    buildbinary=buildbinary+'1101';
    else if (a.clientparams__c.substring(i,i+1) == 'E')
    buildbinary=buildbinary+'1110';
    else if (a.clientparams__c.substring(i,i+1) == 'F')
    buildbinary=buildbinary+'1111';   
    i++;  system.debug(i);
} while (i<count); //while loop ends here


    if (buildbinary.substring(s-3,s-2)=='1')  a.Basket_Trader_On__c=true;                           
else a.Basket_Trader_On__c=false;                           
if (buildbinary.substring(s-4,s-3)=='1')     a.Market_Maker_On__c=true;                             
else a.Market_Maker_On__c=false;                        
if (buildbinary.substring(s-5,s-4)=='1')    a.Spread_Trader_On__c=true;                           
else a.Spread_Trader_On__c=false;                           
if (buildbinary.substring(s-6,s-5)=='1')    a.Trade_Ripper_On__c=true;                            
else a.Trade_Ripper_On__c=false;                        
if (buildbinary.substring(s-14,s-13)=='1')    a.Option_Quote_Cube_On__c=true;                             
else a.Option_Quote_Cube_On__c=false;                           
if (buildbinary.substring(s-15,s-14)=='1')    a.Quote_Grid_On__c=true;                            
else a.Quote_Grid_On__c=false;                          
if (buildbinary.substring(s-16,s-15)=='1')    a.Quote_Explorer_On__c=true;                            
else a.Quote_Explorer_On__c=false;                          
if (buildbinary.substring(s-17,s-16)=='1')    a.Equity_Time_and_Sales_On__c=true;                             
else a.Equity_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-18,s-17)=='1')    a.Option_Time_and_Sales_On__c=true;                             
else a.Option_Time_and_Sales_On__c=false;                           
if (buildbinary.substring(s-26,s-25)=='1')    a.Instant_Messenger_On__c=true;                             
else a.Instant_Messenger_On__c=false;                           
if (buildbinary.substring(s-27,s-26)=='1')    a.Allocator_On__c=true;                             
else a.Allocator_On__c=false;                           
if (buildbinary.substring(s-28,s-27)=='1')    a.Instant_Messenger_Admin_On__c=true;                           
else a.Instant_Messenger_Admin_On__c=false;                         
if (buildbinary.substring(s-29,s-28)=='1')    a.Report_Trade_On__c=true;                              
else a.Report_Trade_On__c=false;                        
if (buildbinary.substring(s-30,s-29)=='1')    a.Adjust_Position_On__c=true;                           
else a.Adjust_Position_On__c=false;                         
if (buildbinary.substring(s-31,s-30)=='1')    a.Order_Grid_On__c=true;                            
else a.Order_Grid_On__c=false;                          
if (buildbinary.substring(s-32,s-31)=='1')    a.Position_Grid_On__c=true;                             
else a.Position_Grid_On__c=false;                           
if (buildbinary.substring(s-33,s-32)=='1')    a.Percentage_Order_On__c=true;                              
else a.Percentage_Order_On__c=false;                        
if (buildbinary.substring(s-34,s-33)=='1')    a.Override_Give_Up_On__c=true;                              
else a.Override_Give_Up_On__c=false;                        
if (buildbinary.substring(s-35,s-34)=='1')    a.Proprietary_Theoriticals_On__c=true;                              
else a.Proprietary_Theoriticals_On__c=false;                        
if (buildbinary.substring(s-36,s-35)=='1')    a.Vol_Trader_On__c=true;                            
else a.Vol_Trader_On__c=false;                          
if (buildbinary.substring(s-37,s-36)=='1')    a.Override_CMTA_On__c=true;                             
else a.Override_CMTA_On__c=false;                           
if (buildbinary.substring(s-38,s-37)=='1')    a.Override_Account_Type_On__c=true;                             
else a.Override_Account_Type_On__c=false;                           
if (buildbinary.substring(s-39,s-38)=='1')    a.EOP_Trader_On__c=true;                            
else a.EOP_Trader_On__c=false;                          
if (buildbinary.substring(s-40,s-39)=='1')    a.Sector_Viewer_On__c=true;                             
else a.Sector_Viewer_On__c=false;                           
if (buildbinary.substring(s-41,s-40)=='1')    a.Manual_Liquidity_On__c=true;                              
else a.Manual_Liquidity_On__c=false;                        
if (a.Demo_Enabled__c) a.Demo_Status__c='Active'; else a.Demo_Status__c='Canceled-Expired';
   
 
    a.testbinary__c = buildbinary;
   
} // end of not valid hex number
}   catch(Exception e) { //if error is encountered
      System.Debug('Trigger failed: '+e.getMessage()); }//write error to the debug log

}
}

 

SuperfellSuperfell

In what way does it not work? (you should probably start by not swallowing exceptions)

freddyloufreddylou

the trigger only runs now when I goto into an individual contact press edit then save.

 

it does not work in conjuncton with the dataloader - before when I had a scripting error - the trigger ran on 47 and then bombed out on 200 - now it runs succesfully on all 247 - the only difference I made was I added 'else' statements so that the number of if statements it looked at was reduced - and that fixed the scripting error.  i can't believe it now doesn't run correctly on all 247. 

 

I am spending far too much time on this but really need it to work and embarassing myself in front of management that I can't deliver this ontime.  they are very mad at me and salesforce.

 

 

please help thanks!

SuperfellSuperfell

Sorry, but can you expand on the not working correctly, are you getting an error in the errors.csv file from the DL, some field on your object is not updated as expected? Have you looked at the debug logs from the data loader runs ? have you tried writing some unit tests to help you diagnose what's going on?

Nick34536345Nick34536345

An immediate workaround would be to configure the data loader to use a smaller batch size

 

each iteration of your do/while loop is at most 19 statements

for 16 iterations and 200 records thats 60,800

 

a batch size of 100 will be easily under the limit

 

edit: actually I forgot the limit will be less for smaller batch size, so probably a size of 20 or so will be needed

freddyloufreddylou

I am not sure what happened, but now it seems to work.  I did not change anything.

 

Thank you for all your thoughtful help!