+ Start a Discussion
Travis McDonaldTravis McDonald 

How do I clear a custom field?


I've got a trigger that is set to write geocode to a location field on the accounts page when an address is updated.

The trigger code is:
// Trigger runs getLocation() on Accounts with no Geolocation

trigger SetGeolocation on Account (after insert, after update) {
    for (Account a : trigger.new)
if (a.Location__Latitude__s == null)

This works great - but what I am running into is the trigger will not fire if the Location__Latitude__s isn't null - that is to say if it already has geocode in it from a previous address, it won't put new geocode in because the field isn't empty.

How can I set this trigger to clear the field whenever the address is updated?

Hi Travis,

If I understand corrcetly, you could just remove the if statemnet so this trigger will always fire, something like this:
trigger SetGeolocation on Account (after insert, after update) {
    for (Account a : trigger.new){}

Please remember to mark this thread as solved with the answer that best helps you.

Travis McDonaldTravis McDonald
Hey Stephen,

Thank you for your response - Unfortunetly that doesn't work, I had already tried that - you would think it would, but since the field already has a variable the way my trigger pulls from an apex class it doesn't overwrite.

I need to have something like 
acc.setFieldsToNull(new String[]{"Location__c"});

to clear the field before the trigger runs. However, that code doesn't work either....

There has got to be an easy way to clear a field....
Hi Travis,

Clearing the field is as simple as saying 'Object.FieldName = null' For example, if I wanted to clear the Account Name it would be something acc.Name = null.

Can you post the rest of the trigger so we can see what you are trying to do and better help you.

Travis McDonaldTravis McDonald

The class that the trigger calls on is called LocationCallouts, see the code below:
public class LocationCallouts {

     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
        // gather account info
        Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];

        // create an address string
        String address = '';
        if (a.BillingStreet != null)
            address += a.BillingStreet +', ';
        if (a.BillingCity != null)
            address += a.BillingCity +', ';
        if (a.BillingState != null)
            address += a.BillingState +' ';
        if (a.BillingPostalCode != null)
            address += a.BillingPostalCode +', ';
        if (a.BillingCountry != null)
            address += a.BillingCountry;

        address = EncodingUtil.urlEncode(address, 'UTF-8');

        // build callout
        Http h = new Http();
        HttpRequest req = new HttpRequest();

            // callout
            HttpResponse res = h.send(req);

            // parse coordinates from response
            JSONParser parser = JSON.createParser(res.getBody());
            double lat = null;
            double lon = null;
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){
                       parser.nextToken(); // object start
                       while (parser.nextToken() != JSONToken.END_OBJECT){
                           String txt = parser.getText();
                           if (txt == 'lat')
                               lat = parser.getDoubleValue();
                           else if (txt == 'lng')
                               lon = parser.getDoubleValue();


            // update coordinates if we get back
            if (lat != null){
                a.Location__Latitude__s = lat;
                a.Location__Longitude__s = lon;
                update a;

        } catch (Exception e) {

This converts the address of an account to geocode. The trigger is set to write that geocode to field "Location__c".

However, if someone updates the account address, it won't write to the field because there is already geocode there. I need to set the trigger to clear that field whenever a change to the address on the account is made.

Thank you for your help.
Hi Travis,

Appologies but I am not sure I understand. From the code you have posted above, there is nothing in place to check if the field is populated or not? From what I can tell, this only occurs in your trigger where you check if the lattitude field equals null. So, if you remove the if statement on line 5, then the code will always execute.