---
schema_version: "1"
id: 8ec7676c1ec8802f02938fb9c811718789b5a4e1127b445ab49b4b825ecbe41a
title: Apex Examples
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/caf-dev/guides/caf/caf-dev-examples-apex.md
canonical_url: https://developer.salesforce.com/docs/platform/caf-dev/guide/caf-dev-examples-apex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: b1141a06e2ee0915c594232ef82a0b99a20842ffc454b975141586eb2b93529c
products:
  - Custom Address Fields Developer Guide
  - Platform Developer Content
---

# 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.

|Required Editions|
|-----------------|
|Available in: all editions.|

**note:** Before you create a custom address field, review the [Custom Address Fields Requirements and Limitations](https://developer.salesforce.com/docs/platform/caf-dev/guide/caf-dev-limitations.md). To discuss the feature and ask questions, join the [Custom Address Fields Discussion](https://trailhead.salesforce.com/trailblazer-community/groups/0F94S000000H3KISA0) group on the Trailblazer Community.

## Insert a Record

This example code creates an Opportunity record which includes address data stored in the custom address field, “Mailing Address”.

```apex
Opportunity a = new Opportunity();
a.StageName='Prospecting';
a.CloseDate=System.today();
a.Name = 'Dublin Order';
a.Mailing_Address__StateCode__s='CA';
a.Mailing_Address__CountryCode__s='US';
a.Mailing_Address__Street__s='1234 Dublin Blvd';
a.Mailing_Address__PostalCode__s='12345';
a.Mailing_Address__City__s='Dublin';
a.Mailing_Address__Latitude__s=80.34;
a.Mailing_Address__Longitude__s=80.35;
a.Mailing_Address__GeocodeAccuracy__s='Address';
insert 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”.

```apex
Gas_Station__c a = new Gas_Station__c();
a.Name = 'Amador Valley';
a.Mailing_Address__StateCode__s='CA';
a.Mailing_Address__CountryCode__s='US';
a.Mailing_Address__Street__s='1234 Dublin Blvd';
a.Mailing_Address__PostalCode__s='12345';
a.Mailing_Address__City__s='Dublin';
a.Mailing_Address__Latitude__s=80.34;
a.Mailing_Address__Longitude__s=80.35;
a.Mailing_Address__GeocodeAccuracy__s='Address';
insert a;
```

## Update an Existing Record

This example code updates the custom address field “Mailing Address” on an Opportunity record with ID 006XXXXXXXXXXXXXXX.

```apex
Opportunity o = [select Id from Opportunity where Id='006XXXXXXXXXXXXXXX'];
o.Mailing_Address__StateCode__s='CA';
o.Mailing_Address__CountryCode__s='US';
o.Mailing_Address__Street__s='1234 Dublin Blvd';
o.Mailing_Address__PostalCode__s='12345';
o.Mailing_Address__City__s='Dublin';
o.Mailing_Address__Latitude__s=80.34;
o.Mailing_Address__Longitude__s=80.35;
o.Mailing_Address__GeocodeAccuracy__s='Address';
update 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”.

```apex
Gas_Station__c a = [select Id from Gas_Station__c where Id='aIsXXXXXXXXXXXXXXX'];
a.Mailing_Address__StateCode__s='CA';
a.Mailing_Address__CountryCode__s='US';
a.Mailing_Address__Street__s='1234 Dublin Blvd';
a.Mailing_Address__PostalCode__s='12345';
a.Mailing_Address__City__s='Dublin';
a.Mailing_Address__Latitude__s=80.34;
a.Mailing_Address__Longitude__s=80.35;
a.Mailing_Address__GeocodeAccuracy__s='Address';
update 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.

```apex
Opportunity o = [select Id from Opportunity where Id='006XXXXXXXXXXXXXXX'];
o.Mailing_Address__StateCode__s= null;
o.Mailing_Address__CountryCode__s= null;
o.Mailing_Address__Street__s=null;
o.Mailing_Address__PostalCode__s=null;
o.Mailing_Address__City__s=null;
o.Mailing_Address__Latitude__s=null;
o.Mailing_Address__Longitude__s=null;
o.Mailing_Address__GeocodeAccuracy__s=null;
update 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.

```nolang
Gas_Station__c a = [select Id from Gas_Station__c where Id='aIsXXXXXXXXXXXXXXX'];
delete a;
```

