I'm trying to convert String to Integer datatype. But I'm unable to convert. I know its simple, but getting me problem here.

It giving me below error

FATAL_ERROR|System.TypeException: Invalid integer:


trigger CreatePckdDime on Package__c (after update) {

List<String> len = new List<String>();
List<String> wdt= new List<String>();
List<String> hgt= new List<String>();
List<String> cnt = new List<String>();
List<Package_Dimension__c> updPkgDim = new List<Package_Dimension__c>();

for(Package__c p : trigger.New)

string height;
string width;
string length;
string count; 

for(Integer i=0; i<len.size();i++)
    for( integer j = 0; j < 20; j++ )
        height = hgt[i].substring(j*3,3);
        length = len[i].substring(j*3,3);
        width = wdt[i].substring(j*3,3);
        count = cnt[i].substring(j*4,4);

       System.debug('@@@'+Integer.valueOf(height)); // this giving error
       System.debug('@@@'+Double.valueOf(height));  // this is also not wokring

      //I'm getting value in height 000 in string format. 
      //I'm trying to compare like
     if(height  == '000')   // this statement executing always false even if value in height is 000. 




I'm getting value for height is 000 in string format at some situation.

I'm trying to compare like

 if(height  == '000')   // this statement executing always false even if value for height is 000. 


Can you please help me to solve my problem.

Edited Part:

This part creating the problem but why substring is giving null value.


        string a='1234567890';


14:53:41:004 USER_DEBUG [2]|DEBUG|@@@a123
14:53:41:004 USER_DEBUG [3]|DEBUG|@@@b               //here getting null value thus its unable to convert into integer. 

3 Answers 3


When we do Type cas from String to other format we need to make sure that the String is correct format so that it can be Trypecast.

In current case you can do something like this. Also remember In Integer it always remove leading 0 before any digit so 000 will be 0 only.

// first two check is to validate String is not null and is of type numeric and if it is correct then compare it with integer.
if(height != null && height.trim().isNumeric() && Integer.valueOf(height.trim()) == 0) {
  //perform code here
  • substring part is giving me null value, but why its giving null. can you please help. please check my edited Part.
    – user29314
    Mar 16, 2018 at 9:32
  • @user29314 its because your start and end point is same. You need to do a.substring(3,6)) Mar 16, 2018 at 9:50

Firstly, check this logic hgt[i].substring(j*3,3) when j becomes 20 ,the value becomes hgt[i].substring(60,3) ,check if the string you have contain 60+ characters once.

Secondly , Always use if(height.equals('000')) Idealy this should work. Thanks and Regards, Shiva RV


Here what you can do is to convert both height and '000' in integer and then compare them because when you try to convert '000' into an integer it will give you 0, not 000.

I also tried the following code in an anonymous block it will work fine for me.

integer height = 000;
string heightStr = '000';
System.debug(heightStr == '000');
System.debug(height == integer.valueof('000'));
System.debug(integer.valueof(heightStr) == integer.valueof('000'));

all the values are true in log.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .