function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lopezclopezc 

Can I create a a new lookup field into the Task Object?

hi All,

I will like to create a new lookup field in the task Object. I know that for creating new custom fields I have to go to Activities custom fields in the menu bar. However, I don't have the option to create a lookup field.

My problem is that I would like to add to the task views a new field (Account Number) from the Lead/Contact related to the task. I don't know a priori if the task will be related to a Lead or Contact.

Any Ideas?

Many Thanks!
werewolfwerewolf
Unfortunately, no, as you've found, you can't add more lookup fields to that object.  You could add an Account Number field to it that is just a number and make an Apex trigger on Activity which goes and looks up the parent and gets the Account Number from it if it's available.
lopezclopezc
Hi,

I created a triggered on Task, but this is giving me an error:

caused by: System.Exception: Too many SOQL queries: 21
Trigger.TaskAccountNumber: line 28, column 33

The problem is that I don't know a priori if the task is related to a Lead or a Contact. Here is my code:

Code:
trigger TaskAccountNumber on Task (before insert, before update) {

   /***************************************************************************
   * TaskBefore Trigger - TaskCoverage 29/9/08
   * 
   * Description:
   *   Before a task is inserted/updated, look for a matching contact/lead and update 
   *   Account_Number__c on the task.
   *
   * Notes:
   *   To avoid reaching Apex code built-in limits, the code is structured to keep 
   *   the number of SOQL queries to an absolute minimum. 
   *
   * Revision History
   * none
   ****************************************************************************/  
   
// List to hold the WhoId of all incoming task records
  List<String>  whoIdList = new String [] {};
   for(Integer i = 0; i<Trigger.new.size(); i++) {
        if(Trigger.new[i].WhoId != null){
            if(Trigger.isInsert){
                whoIdList.add(Trigger.new[i].WhoId);
            }else{
                if(Trigger.new[i].WhoId != Trigger.old[i].WhoId){
                    whoIdList.add(Trigger.new[i].WhoId);
                }
            }
        }else{
            Trigger.new[i].Account_Number__c = null;
        }
    }
    //issue one query to get all contacts for all incoming who ids (to avoid Apex restrictions)
    List<Contact> matchingContacts = [SELECT Id, Account_Number__c from Contact where Id IN :whoIdList];
    // issue one query to get all leadsfor all incoming who ids (to avoid Apex restrictions)
    List<Lead> matchingLeads =  [SELECT Id, Demo_Account__c from Lead where Id IN :whoIdList];
    
    Map<String, Contact> foundContacts = new Map<String, Contact>();  
    for (Contact loopContact : matchingContacts) {
        foundContacts.put(loopContact.Id, loopContact);
    }
  
    Map<String, Lead> foundLeads = new Map<String, Lead>();  
    for (Lead loopLead : matchingLeads) {
        foundLeads.put(loopLead.Id, loopLead);
    }
    // loop through the incoming tasks again, for each, loop through the found contacts/leads to find matching record
    // on Id
    for(Task loopTask : Trigger.New) {
        if (loopTask.WhoId != null) {
          Contact foundContact = foundContacts.get(loopTask.WhoId);
          if (foundContact != null) {
             loopTask.Account_Number__c = foundContact.Account_Number__c;
          }else{
              Lead foundLead = foundLeads.get(loopTask.WhoId);
                if(foundLead != null){
                    loopTask.Account_Number__c = foundLead.Demo_Account__c;
                }
          }
        }
    }    
}

 Do you know what I am doing wrong? There is no another way? I have a few triggers on Contacts already and I know that the number of Queries is limited..

Thanks for your help!

werewolfwerewolf
Actually you can know a priori if it's associated to a Lead or a Contact – just convert the ID to a string and look at the first 3 chars (which are known as the "entity prefix").  00Q is a lead, 003 is a contact.

The Too Many SOQL queries might be a red herring – it seems like you're only doing 2 in there, but your other triggers may be doing the other 19.
arnt72arnt72
can you combine you triggers into one and do a more general query (even without a WHERE), maybe using the SOQL for Loop?
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm

It would involve more logic in your if statements instead of the WHERE clause but might help you avoid running into the limit.