1

I am trying to create a string of address in this format: Street, City, State, Country, Zip. Assume that there is an Address object called addr which has account's shipping address.

Address addr = Account__r.ShippingAddress; // Ignore the syntax here and assume that addr has Address.

My Challenge here is I want to build a string of the addr with comma separated. Example if shipping address has

Shipping Street : 123th Street
Shipping City   : SFO
Shipping State/Province : CA
Shipping Zip/Postal Code: 12345
Shipping Country: USA

I want to form a string like String s = 123th Street, SFO, CA, 12345, USA

The catch :

There could be different possiblity that street may not be present or country may not be present in that case the String should logically display the commas example - city, state, zip -> SFO, CA, 12345. Also I do not want to display NULL in case street/country/state/zip is not available. So it is possible that any of the data might be missing and my string should accommodate for missing data and produce the correct comma separated string.

This is what I got started with but I see the logic is getting complex:

String street; String city; String state; String country; String zip;
         Address addr = Account__r.ShippingAddress; 
          street = addr.getStreet() <> NULL ? addr.getStreet() : ''; // to make sure I do not display NULL in string
          if(street == NULL){
          city = addr.getCity() <> NULL ? addr.getCity() : '';
          }else{
          city = addr.getCity() <> NULL ? ', ' + addr.getCity() : '';
          }
          if(city == NULL){
          state = addr.getState() <> NULL ? addr.getState() : '';
          }else{
          state = addr.getState() <> NULL ? ', ' + addr.getState() : '';
          }
          if(state == NULL){
          country = addr.getCountry() <> NULL ? addr.getCountry() : '';
          }else{
          country = addr.getCountry() <> NULL ? ', ' + addr.getCountry() : '';
          }
          if(country == NULL){
          zip = addr.getPostalCode() <> NULL ? addr.getPostalCode() : '';
          }else{
          zip = addr.getPostalCode() <> NULL ? ', ' + addr.getPostalCode() : '';
          }
     String addString = street + '' + city + '' + state + '' + country + '' + zip;

3 Answers 3

3

For things like this, I use an array:

String[] addr = new String[] {
  record.Street, record.City, record.State, record.PostalCode, record.Country
};

for(Integer i = 4; i >= 0; i--) {
  if(addr[i] == null) {
    addr.remove(i);
  }
}

String theAddr = String.join(addr, ',');
3
  • SF needs a List method to remove nulls.
    – Eric
    Commented May 18, 2017 at 0:06
  • @Eric yeah, or a list indexof method would help.
    – sfdcfox
    Commented May 18, 2017 at 0:14
  • Maybe make it for(Integer i = addr.size()-1; i>=0;i--) to make it easier to add or remove items from the List or more reusable and passing in a list to a utility method
    – Eric
    Commented May 18, 2017 at 1:06
2

Note: This is the current behavior, but not a guaranteed result

Not sure if this is an anomaly or not as the sets are unordered but doing this seems to put it back in the order they were added. Also removes a loop.

//Build the set of values
Set<String> tmp = New Set<String>{'C','B',null,'A',null,'Z','D'};
System.debug(tmp);

19:19:42.23 (23673744)|USER_DEBUG|[2]|DEBUG|{null, A, B, C, D, Z}

Notice how they are not in the order they were put in

//remove the null values
tmp.remove(null);

Now we join the remaining values back

System.debug(string.join(New List<String>(tmp),','));

19:19:42.23 (23854080)|USER_DEBUG|[4]|DEBUG|C,B,A,Z,D

Whala - They are back in the order they were initially added and the nulls are removed

So in your case

Set<String> tmp = New Set<String>{
   record.Street, record.City, record.State, record.PostalCode, record.Country
};
tmp.remove(null);
String theAddr = string.join(New List<String>(tmp),',');
5
  • This is the current behavior, but not a guaranteed result. We unfortunately cannot rely on this behavior.
    – sfdcfox
    Commented May 17, 2017 at 23:53
  • @sfdcfox - Thanks. Wonder if it is do to a pointer to the original list or what....
    – Eric
    Commented May 17, 2017 at 23:55
  • @Eric Interesting to see that how behavior changes with NULL.
    – SfdcBat
    Commented May 18, 2017 at 0:02
  • @SfdcBat - Not sure I understand. I already have null in there. Without null it works the same
    – Eric
    Commented May 18, 2017 at 0:03
  • @Eric Your solution is correct. I was referring to the initially part where you experimented the order with NULL
    – SfdcBat
    Commented May 18, 2017 at 0:05
1

It will be like this:

List<String> lstStr = new List<String>();
Address addr = Account__r.ShippingAddress; 
if(addr.getStreet() !=null) lstStr.add(addr.getStreet());
if(addr.getCity() !=null) lstStr.add(addr.getCity());
if(addr.getState() !=null) lstStr.add(addr.getState());
if(addr.getCountry() !=null) lstStr.add(addr.getCountry());
if(addr.getPostalCode() !=null) lstStr.add(addr.getPostalCode());

String finalAddress = String.join(lstStr,',');
3
  • @SfdcBat please don't pick up bad programming habits. The body of an if statement should always be a proper block with { and }. This is perfectly fine logic, but writing compact code like this isn't recommended. It's too easy to make mistakes.
    – sfdcfox
    Commented May 18, 2017 at 0:29
  • @sfdcfox thank you for the comment. I did feel that I could make a mistake with the curly braces missing. I have actually used what Eric had given as solution as it had just 4 lines of code
    – SfdcBat
    Commented May 18, 2017 at 0:32
  • @SfdcBat - sfdcfox's method is the one guaranteed to work. This answer while it works requires multiple line updates to add or remove values. In addition to the above comments. Also, a reusable utility method would be more useful than code for a specific scenario
    – Eric
    Commented May 18, 2017 at 1:03

You must log in to answer this question.

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