8

I'd like to create a lead via apex and set it's address. I've read that Address is a compound field and that it is totally read only unless via SOAP/REST API. Looking at the docs, I could only find getter methods - nothing to set a value.

How do I go about setting an address via apex?

Thank you!

3 Answers 3

7

You set the individual fields, such as BillingStreet, BillingCity, BillingState, BillingPostalCode, and BillingCountry (plus the geolocation codes, as well, if you prefer). The address complex field is a convenience field derived from the parts, just as a contact or lead's "Name" field is really a concatenation of the various parts of their name (first, last, etc).

1
  • 3
    ok but what if the address is a custom address? and what if there is many custom addresses on an object?
    – 2095377
    Jun 22, 2023 at 10:42
2

Here's an example for a new Lead:

Lead lead = new Lead(

                    OwnerId = u.Id,

                    FirstName = 'John',
                    LastName = 'Doe',
                    Email = '[email protected],
                    Company = 'USA,
                    Title = 'Senator',

                    LeadSource = 'Website',

                    Street = '2100 Woodward Ave',
                    City = 'Detroit',
                    State = 'MI',
                    PostalCode = '48201',
                    Country = 'US',

                    RecordTypeId = recordTypeId
            );
1
  • 2
    How do you do the same if the address is a custom address?
    – 2095377
    Jun 22, 2023 at 10:42
2

If your custom address field is called Delivery_Address__c, then you can do something like this...

obj.Delivery_Address__City__s = 'your city';
obj.Delivery_Address__StateCode__s='ON';
obj.Delivery_Address__CountryCode__s='CA';
obj.Delivery_Address__Street__s='1234 Test Street';
obj.Delivery_Address__PostalCode__s='P1P1P1';

You must log in to answer this question.

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