+ Start a Discussion
shravani milshravani mil 

List index out of bound 1 error

list out of bond error at line 

 String userId = leadUsrStr.split('_')[1];
Please apply contains to check '_' is contained by this leadUsrStr or not ? and then also check list size.

Sandeep Singhal
shravani milshravani mil
didnt get you sandeep
Rahul SharmaRahul Sharma
Just thought to elaborate Sandeep's post with some code, it is as follows:
String leadUsrStr = 'your string with a User Id';
Id userId;

// check if the leadUsrStr is not empty and contains an "_"
if(String.isNotEmpty(leadUsrStr) && leadUsrStr.contains('_')) {
    // fetch the second value based on an underscore
    String strUserId = leadUsrStr.split('_')[1]; 

    // try catch to validate if the its a valid Salesforce Id
    try {
        strUserId = String.isNotEmpty(strUserId) ? strUserId.trim() : null;
        // check  if the Id is a valid User Id by comparing the prefix
        if(strUserId.length() > 0 &&
            User.sObjectType.getDescribe().getKeyPrefix() == strUserId.substring(0,3)) {
            userId = strUserId;
    } catch(Exception objEx) {
        // some exception handling, if you'd like to
        System.debug('Invalid Id: ' + strUserId);

System.debug('valid userId is: ' + userId);

Hope it helps and let us know in case of issues.
Request you to invest more time on understanding the error yourself first :)