TransWikia.com

SOQL Error Only When Accessing Lead.Address Standard Compound Address Field From WorkFlow Triggered Task Insert

Salesforce Asked by KB145 on December 17, 2021

I’ve added a section of code to our task trigger that retrieves two sets of address fields from the lead or contact the task is being made on – see below-.

private static void setPhysicalAndMailingAddress(){
    Map<sObject, Id> whoIdByTask = new Map<SObject, Id>();
    Map<sObject, sObject> whoObjectByTask = new Map<sObject, sObject>();
    Map<Id, sObject> whoObjectById = new Map<Id, sObject>();

    /*Create map of tasks by their lead/contact id */
    for (Task task : (List<Task>) Trigger.new) {
        whoIdByTask.put(task, task.whoId);
    }

    /*Query the contacts and objects */
    whoObjectById.putAll(new Map<ID, sObject>([SELECT Id, MailingAddress, OtherAddress FROM Contact WHERE Id IN :whoIdByTask.Values()]));
    whoObjectById.putAll(new Map<Id, sObject>([SELECT Id, Address, Mailing_Address__c, Mailing_Address_2__c, Mailing_City__c, Mailing_Frequency__c, Mailing_State__c, Mailing_Zipcode__c FROM Lead WHERE Id IN :whoIdByTask.Values()]));

    /*Create a new map of tasks to lead/contact objects (instead of just their ids) */
    for(sObject task :whoIdByTask.KeySet()){
        if(whoObjectById.get(whoIdByTask.get(task)) != NULL){
            whoObjectByTask.put(task, whoObjectById.get(whoIdByTask.get(task)));
        }
    }
        if(!whoObjectByTask.isEmpty()) ActivityUtils.setPhysicalAndMailingAddress(whoObjectByTask);
}

This works just fine when you go to a lead or a contact, click the “new task” button, and create a task.

But when the second query is attempted when a workflow rule inserts a task, it fails with the error “No such column ‘Address’ on entity ‘Lead’. If you are attempting to use a custom field, be sure to append the ‘__c’ after the custom field name. Please reference your WSDL or the describe call for the appropriate names.”

I’ve also found if I attempt to insert the task via a small function called from the lead trigger instead of having it called from the task trigger or having the workflow handle it, the same error results. (Small function shown below for reference).

private void checkIfInvalid(){
    List<Task> taskList = new List<Task>();
    for (Lead obj : (List<Lead>) Trigger.new) {  
      if(obj.Status =='Unqualified'){
        Task task = new Task();
        task.ownerid = '005xxxxxxxxxxxxxxx';
        task.Status = 'Open';
        task.Priority = 'Normal';
        task.ActivityDate = Date.Today();
        task.Subject = 'Invalid Lead';
      }
  }
}

Also note, if I alter the original task trigger code to add in statements to check if the source of the task being inserted is from that workflow and to not attempt the query if so, (since only that workflow inserts tasks with the subject “Invalid Lead”), it works just fine again without errors.

    /*Create map of tasks by their lead/contact id */
    for (Task task : (List<Task>) Trigger.new) {
        if(task.Subject != 'Invalid Lead'){
            whoIdByTask.put(task, task.whoId);
        }
    }
    if(whoIdByTask.size() > 0){ ...

I’ve gone through and updated the Api Version to 34.0 on all classes and pages that are not from managed applications, as my research shows any Api version prior to 30.0 couldn’t access lead.Address in its compound form through SOQL, which is supposed to be the cause of that error, but it hasn’t solved the problem.

Any help on determining the cause of the error would be greatly appreciated!

One Answer

You can not reference Address field directly in Apex. You have to refer all five fields like state , country, postalcode , street,City.

Answered by NEHA GUPTA on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP