0

I have this method below that Returns a String of Id's separated by '|' operator and it works. However now I would like to add on that if an Id is null or blank replace with just single quotes (''). Not sure how to do that. How can i modify this method to do that.

I expect just to see the single quotes only. Example if have strings of ids my result = ('46734'|'3435') What I am looking for if no Ids then I would like to see result =('') or (''|'') not result = ()

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

2 Answers 2

0

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 ;
}
6
  • My method above does that already. What I am asking is how can modify it to insert or pass just ('') singles quotes if the string of Id's is null/blank? –
    – user58
    Commented Mar 2, 2020 at 22:25
  • I am getting a variable does not exist: finalList
    – user58
    Commented Mar 2, 2020 at 22:42
  • @user58 I made changes according to your code. Let me know if it works. I have checked for isNotBlank for every entry and based on that I have added sub-string to strId.
    – samdev
    Commented Mar 2, 2020 at 22:47
  • it did not work.
    – user58
    Commented Mar 2, 2020 at 23:20
  • Let me rephrase. What i am looking for is if no records are returned I want to insert '' or ('').
    – user58
    Commented Mar 2, 2020 at 23:58
0

Here is an alternate approach:

public static String getString(Set<Id> setId){
  if (setId.isEmpty()) {
     return '\'\'';
  }

   List<String> allStrings = new List<String>();

   for (Id currentId : setID) {
      String cleanString = '\'' + (String.isBlank(currentId) ? '' : currentId) + '\'';

      allStrings.add(cleanString);          
   }

   return String.join(allStrings, '|');

}
5
  • Thanks .This didn't work for me either. Im just for just singles quotes and nothing inbetween the single quotes if there is no Id.
    – user58
    Commented Mar 3, 2020 at 0:20
  • That is exactly what this string gives you. You're not being clear on EXACTLY what you expect (and why this "doesn't work"). I can't help you further until and unless you provide those details Commented Mar 3, 2020 at 0:22
  • I expect just to see the single quotes only outputted. I don't know how else to explain it. The results are not showing any single quotes at all Example if have string of id i get this ('46734'.|'3435'). What I am looking for if no Ids then literally this ('') or (''|'')
    – user58
    Commented Mar 3, 2020 at 0:26
  • Explain it by giving examples in your original post. You've said the same thing three times and when two people helped you your only answer is "doesn't work". At this point, it is a matter of the problem not being properly defined. I will be available for a little longer if you can spend the time to expand. Commented Mar 3, 2020 at 0:28
  • try now, please Commented Mar 3, 2020 at 0:44

You must log in to answer this question.

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