+ Start a Discussion
DJP1SDJP1S 

Method does not exist or incorrect signature on leftPad operation?

I've got this piece of code in my sandbox which is working just fine 

      Page1Project__c[] updates = new Page1Project__c[]{};
      Project_Template__c[] templates = new  Project_Template__c[]{};
       	
      for (Page1Project__c pu : projects){
      			updates.add(pu);
      }
    ; 
      	    for (Project_Template__c pt : [SELECT Name, Id, Times_Used__c, Last_Used__c FROM Project_Template__c]){
           	 	for(Page1Project__c u : updates){
                	if(pt.Id == u.Project_Template__c){
                 		decimal i = pt.Times_Used__c;
                 		pt.Times_Used__c = i + 1;
                        integer m = u.CreatedDate.date().month();
                        string ms = string.valueOf(m);
                        integer d = u.CreatedDate.date().day();
                        string ds = string.valueOf(d);
                    	pt.Last_Used__c = '' + u.CreatedDate.date().year() + '-' + ms.leftPad(2, '0') + '-' + ds.leftPad(2, '0') + ' - ' + UserInfo.getFirstName() + UserInfo.getLastName().SubString(0,1);
                        //string d = u.createdDate.date().year();();
                        templates.add(pt);
                	}
                }
            }
    if (templates.size() >0){
    	update templates;
    }

 But whenever I try to deploy I get a save error that the "Method does not exist or incorrect signature: [String].leftPad(Integer, String)"

 

It's throwing it at this line:

 

pt.Last_Used__c = '' + u.CreatedDate.date().year() + '-' + ms.leftPad(2, '0') + '-' + ds.leftPad(2, '0') + ' - ' + UserInfo.getFirstName() + UserInfo.getLastName().SubString(0,1);

 I'm trying to get the string writted like this: 2012-09-26 - FirstnameL

 

Best Answer chosen by Admin (Salesforce Developers) 
DJP1SDJP1S

I ende up checking the length with an IF statement and changing the length accordingly.

 

      Page1Project__c[] updates = new Page1Project__c[]{};
      Project_Template__c[] templates = new  Project_Template__c[]{};
       	
      for (Page1Project__c pu : projects){
      			updates.add(pu);
      }
    
		system.debug('@@@@@@@@@@@updates: ' + updates); 
      	    for (Project_Template__c pt : [SELECT Name, Id, Times_Used__c, Last_Used__c FROM Project_Template__c]){
           	 	for(Page1Project__c u : updates){
                	if(pt.Id == u.Project_Template__c){
                 		decimal i = pt.Times_Used__c;
                 		pt.Times_Used__c = i + 1;
                        integer m = u.CreatedDate.date().month();
                        string ms = string.valueOf(m);
                        if(ms.length() < 2){
                            ms = '0' + ms;}
                        integer d = u.CreatedDate.date().day();
                        string ds = string.valueOf(d);
                        if(ds.length() < 2){
                            ds = '0' + ds;}
                    	pt.Last_Used__c = '' + u.CreatedDate.date().year() + '-' + ms + '-' + ds + ' - ' + UserInfo.getFirstName() + UserInfo.getLastName().SubString(0,1);
                        //string d = u.createdDate.date().year();();
                        templates.add(pt);
                	}
                }
            }
    if (templates.size() >0){
    	update templates;
    }

 

All Answers

Jia HuJia Hu
leftPad is a new function from API ver. 26, check the API version of your target Org.
I guess it is not updated now.
DJP1SDJP1S

Thanks! How can I achieve the same result in API 25.0?

Jia HuJia Hu
You have to wait Salesforce to update itself to API 26 and then you will get this method.
DJP1SDJP1S

I ende up checking the length with an IF statement and changing the length accordingly.

 

      Page1Project__c[] updates = new Page1Project__c[]{};
      Project_Template__c[] templates = new  Project_Template__c[]{};
       	
      for (Page1Project__c pu : projects){
      			updates.add(pu);
      }
    
		system.debug('@@@@@@@@@@@updates: ' + updates); 
      	    for (Project_Template__c pt : [SELECT Name, Id, Times_Used__c, Last_Used__c FROM Project_Template__c]){
           	 	for(Page1Project__c u : updates){
                	if(pt.Id == u.Project_Template__c){
                 		decimal i = pt.Times_Used__c;
                 		pt.Times_Used__c = i + 1;
                        integer m = u.CreatedDate.date().month();
                        string ms = string.valueOf(m);
                        if(ms.length() < 2){
                            ms = '0' + ms;}
                        integer d = u.CreatedDate.date().day();
                        string ds = string.valueOf(d);
                        if(ds.length() < 2){
                            ds = '0' + ds;}
                    	pt.Last_Used__c = '' + u.CreatedDate.date().year() + '-' + ms + '-' + ds + ' - ' + UserInfo.getFirstName() + UserInfo.getLastName().SubString(0,1);
                        //string d = u.createdDate.date().year();();
                        templates.add(pt);
                	}
                }
            }
    if (templates.size() >0){
    	update templates;
    }

 

This was selected as the best answer