0

I am working on an assignment where we enabled an external system to search for contacts in Salesforce based on lastName, firstName, BirthDate. I have developed a webservice which queries the Contact object based on the input from request payload and returning the list of Contacts in the form of the webservice response. The service is working fine and they are getting what they want. Since the address fields in Salesforce are compound fields, the street address field is sent as shown below.

"street": "1608 Lewis Creek\r\nStreet 2\r\nStreet 3". 

However, the team wants these fields broken down into individual fields.

The code for my response class is below:

global class ContactRest {    
    global String status; // Status of the request
    global String message;
    global List<contact> conList; //status of the message      
}

What is the easiest way to address this request? Can I use JSON2Apex or something else?

1

You should query the component fields of the address, rather than the compound field. Then, your remote caller will get back the separated details.

There are a total of four standard Address fields spread across the Contact and Account objects). To take one for example, the components of Contact.OtherAddress are

  • OtherStreet
  • OtherCity
  • OtherState
  • OtherPostalCode
  • OtherCountry
  • OtherStateCode
  • OtherCountryCode
  • OtherLatitude
  • OtherLongitude
  • OtherGeocodeAccuracy

The -Code variants will only be present if your org is using State and Country Picklists. The child fields for other Address fields have the same naming pattern, i.e., MailingAddress => MailingStreet and company.

If your issue is not with the compound field itself, but with multi-line data present within the MailingStreet or similar component field, you can use the String.split() function to break this data into separate lines. However, by doing so, you'll sacrifice the simplicity of your service's response in returning a List<Contact>, as you'll have to build your own wrapper class that includes a List<String> element to hold those street lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.