Skip to main content
4 of 4
added 21 characters in body
samdev
  • 154
  • 2
  • 10

You can use the join method from String class like below to get the string of ids seperated by '|' operator:

String.join( new List<String>(String.join(new List<id>(setId), ',').split(',')) , '|' ));

EDIT: check for isNotBlank for every entry in the for loop:

Edit 2: Added empty check for setId.

      public static string getString(Set<Id> setId){
    
    String strId = '';
    if(setId != null && setId.size() > 0 ){
        for(Id RecordId :setId){  
            String strRecordId  ='';
            if(String.isNotBlank(RecordId)){
                strRecordId = RecordId;
                if(strRecordId.length() > 15){
                    strRecordId = strRecordId.substring(0,15);
                    strRecordId = '\''+ strRecordId + '\'';
                    strId += strRecordId + '|';
                }                        
            }
            else{
                strRecordId = '\' \'';
                strId += strRecordId + '|';
            }
        }
    }
    else{
        strId = '\' \'';
    }
    strId = strId.removeEnd('|');
    return strId ;
}
samdev
  • 154
  • 2
  • 10