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
Kr ramKr ram 

before insert on TASK object -to get account/con/lead ids into task custom field1

before insert on task object -to get account/con/lead ids into task custom fieldhello gurus:
Goal : To populate the Account ID ,Contact ID,Lead ID into a custom field in the task object (i.e)

If task is linked to an account get the Account id and write it to a custom field in task object
If task is linked to an contact get the Contact id and write it to a custom field in task object
If task is linked to an lead get the lead id and write it to a custom field in task object.

this is the code ,but i get the "attempt to deference a null pointer exception" any suggestions appreciated!



trigger updateAccountFromTask on Task (before insert)
{
    String account_prefix = Schema.SObjectType.Account.getKeyPrefix();
    String contact_prefix = Schema.SObjectType.Contact.getKeyPrefix();

    Map<Id,Account> accountMap = new Map<Id,Account>();
    Map<Id,Contact> contactMap = new Map<Id,Contact>();
    Set<id> Ids = new Set<id>();
        for (Task tk: Trigger.new) 
        {
        Ids.add(tk.WhatId);
       Ids.add(tk.WhoId);
        }
    Map<id,Account> accountMap2 = new Map<id,Account>([Select Id,Full_Account_ID__c from Account Where Id in :Ids]);
    Map<id,Contact> accountMap3 = new Map<id,Contact>([Select Id,Name from Contact Where Id in :Ids]);
        for (Task t: Trigger.new)
        if(t.Subject != null)//) || t.WhoId == null )//|| t.WhoId !=null*/) 
        
        //&& t.Status == 'Completed - Move to Demo'&& ((String)t.WhatId).startsWith(account_prefix))
        {
        Account a = accountMap2.get(t.WhatId);
        Contact b = accountMap3.get(t.WhoId);
         t.Account_id__c= a.Full_Account_ID__c ;
        // t.Contact_Id__c= b.Name;
        //a.Task_Source__c = t.Task_Source__c;
       // a.Demo_Status__c = t.status;// this is the line its throwing exception
        accountMap.put(a.id,a);
       // contactMap.put(b.id,b);
        }
            try
            {
                update accountMap.values();
              //  update ContactMap.values();
            }
                catch (system.Dmlexception e) 
                {
                system.debug (e);
                }
                }