Accessing compound fields in salesforce

Accessing compound fields in salesforce,sfdcpanda,sfdc panda, salesforce examples, salesforce address field access, sfdc panda, salesforce apex examples, visualforce, salesforce chennai, training institute in chennai

Accessing compound fields in salesforce Explained:-

Compound fields groups more than one primitive data types to represent complex data types like Location or Address

Address compound fields are available after API version v.30 and later, If you’re trying to accessing address field in your trigger or apex code make sure the API version is not below v.30, If it is below that you will be getting ‘Address is not a variable’ Error.

The list of available Address fields are,

  • Accuracy
  • City
  • Country
  • CountryCode
  • Latitude
  • Longitude
  • PostalCode
  • State
  • StateCode
  • Street

How to access each field in Apex :-

   for(user userObj : [Select id,address from User where id in : trigger.new])

   {

 Address addrObj = (Address) userObj.address;
           if(addrObj != null){
           String city = addrObj.city;
            String street = addrObj.street;
            String state = addrObj.state;
            String postalcode = addrObj.postalcode;
            String country = addrObj.country;
   }}

Note :-  Address value will be available in SOQL retuned query but if you try to access address field from trigger.new it will return null value.

Geolocation Compound Field :-

Geolocation fields are accessible in the SOAP and REST APIs as a Location—a structured compound data type—or as individual latitude and longitude elements.

In API versions 26.0 and later, geolocation fields are available in the SOAP and REST APIs as a compound field of typeLocation. This structured data type contains the following fields.

  • latitude
  • longitude

With API versions 26.0 and later, use the individual latitude and longitude fields.

SELECT  location__latitude__s, location__longitude__s 
FROM Warehouse__c

Salesforce developer guide reference(Accessing compound fields in salesforce:-

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/compound_fields_address.htm

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*