Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Apex Examples
Apex code examples for Custom Address Fields. The examples create a record with custom
address data, update the custom address on an existing record, and delete a record that contains
custom address data.
| Available in: all editions. |
Insert a Record
This example code creates an Opportunity record which includes address data stored in the
custom address field, “Mailing
Address”.
1Opportunity a = new Opportunity();
2a.StageName='Prospecting';
3a.CloseDate=System.today();
4a.Name = 'Dublin Order';
5a.Mailing_Address__StateCode__s='CA';
6a.Mailing_Address__CountryCode__s='US';
7a.Mailing_Address__Street__s='1234 Dublin Blvd';
8a.Mailing_Address__PostalCode__s='12345';
9a.Mailing_Address__City__s='Dublin';
10a.Mailing_Address__Latitude__s=80.34;
11a.Mailing_Address__Longitude__s=80.35;
12a.Mailing_Address__GeocodeAccuracy__s='Address';
13insert a;This example code add a record for a custom object, “Gas Station” (Gas_Station__c). The new
record includes address data stored in the the custom address field, “Mailing
Address”.
1Gas_Station__c a = new Gas_Station__c();
2a.Name = 'Amador Valley';
3a.Mailing_Address__StateCode__s='CA';
4a.Mailing_Address__CountryCode__s='US';
5a.Mailing_Address__Street__s='1234 Dublin Blvd';
6a.Mailing_Address__PostalCode__s='12345';
7a.Mailing_Address__City__s='Dublin';
8a.Mailing_Address__Latitude__s=80.34;
9a.Mailing_Address__Longitude__s=80.35;
10a.Mailing_Address__GeocodeAccuracy__s='Address';
11insert a;Update an Existing Record
This example code updates the custom address field “Mailing Address” on an Opportunity
record with ID
006XXXXXXXXXXXXXXX.
1Opportunity o = [select Id from Opportunity where Id='006XXXXXXXXXXXXXXX'];
2o.Mailing_Address__StateCode__s='CA';
3o.Mailing_Address__CountryCode__s='US';
4o.Mailing_Address__Street__s='1234 Dublin Blvd';
5o.Mailing_Address__PostalCode__s='12345';
6o.Mailing_Address__City__s='Dublin';
7o.Mailing_Address__Latitude__s=80.34;
8o.Mailing_Address__Longitude__s=80.35;
9o.Mailing_Address__GeocodeAccuracy__s='Address';
10update o;This example code updates an existing record for a custom object, “Gas Station”
(Gas_Station__c) with ID aIsXXXXXXXXXXXXXXX. It updates custom address field “Mailing
Address”.
1Gas_Station__c a = [select Id from Gas_Station__c where Id='aIsXXXXXXXXXXXXXXX'];
2a.Mailing_Address__StateCode__s='CA';
3a.Mailing_Address__CountryCode__s='US';
4a.Mailing_Address__Street__s='1234 Dublin Blvd';
5a.Mailing_Address__PostalCode__s='12345';
6a.Mailing_Address__City__s='Dublin';
7a.Mailing_Address__Latitude__s=80.34;
8a.Mailing_Address__Longitude__s=80.35;
9a.Mailing_Address__GeocodeAccuracy__s='Address';
10update a;Delete Data Within a Custom Address Field from a Record
To delete an address stored in a custom address field from a record, update the record.
This example code removes the data stored the custom address field “Mailing Address” on an
Opportunity record with ID
006XXXXXXXXXXXXXXX.
1Opportunity o = [select Id from Opportunity where Id='006XXXXXXXXXXXXXXX'];
2o.Mailing_Address__StateCode__s= null;
3o.Mailing_Address__CountryCode__s= null;
4o.Mailing_Address__Street__s=null;
5o.Mailing_Address__PostalCode__s=null;
6o.Mailing_Address__City__s=null;
7o.Mailing_Address__Latitude__s=null;
8o.Mailing_Address__Longitude__s=null;
9o.Mailing_Address__GeocodeAccuracy__s=null;
10update o;Delete a Record
This code deletes a record for the custom object, “Gas Station” (Gas_Station__c) with ID
aIsXXXXXXXXXXXXXXX. When a record is deleted, all data for that record is deleted, including
the custom address field
information.
1Gas_Station__c a = [select Id from Gas_Station__c where Id='aIsXXXXXXXXXXXXXXX'];
2delete a;