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
Naveen KvpNaveen Kvp 

trigger on task

Hi Everyone,

   Here i wrote a trigger on task where new task record is inserted or updated ,in account i have custom field called Area__c
   Whenever trigger fires the Area__c filed should outopopulate on task.
   I need your help on without selecting Relatedto(whatid) i am try to save a record it will cause an Attempt to de-reference a null error  how to resove
   this what kind of changes i need to do in trigger.   
   
     Thanks in advance.

trigger Update_Area on Task (before insert, before update)
{    
    set<id> Accids= new set<id>();
    map<id, Account> mapleads = new map<id, Account>();
    Map<Id, List<Task>> whatIds = new Map<Id, List<Task>>{};
    
        for(Task t : trigger.new)
        {
            String objName = t.WhatId.getSObjectType().getDescribe().getName();
            if(t.WhatId!=null )
            {
                if(objName=='Account')
                {
                    Accids.add(t.WhatId);
                }  
            }
        }
    
    list<Account> ldlist = [select id, name,Area__c
                            from Account where id in :Accids];
    
    if(!ldlist.isEmpty())
    {
        for(Account l : ldlist)
        {
            mapleads.put(l.Id, l);
        }
    }        
    for(Task tt: trigger.new)
    {        
        String objName = tt.WhatId.getSObjectType().getDescribe().getName();
        if(tt.WhatId!=null)
        {
            if(objName == 'Account')
            {
                tt.Area__c= mapleads.get(tt.WhatId).Area__c;  
            }
        }  
    }
}
Best Answer chosen by Naveen Kvp
sandeep sankhlasandeep sankhla
Hi Naveen,

You are first getting the value from whatId and then you are checking if it is not null

see below code:

   String objName = t.WhatId.getSObjectType().getDescribe().getName();

            if(t.WhatId!=null )
{

}

There is no use of this t.whatId != null, because if this will null then it will throw error on previous line where you are doing t.whatId.getSobjectType()...du eto null it will be null.getSobjecttype()..

this is the reason of your error..

you can first check if it is null or not then inside the same if condition you can get like below

 if(t.WhatId!=null )
{
       String objName = t.WhatId.getSObjectType().getDescribe().getName();
}


This will solve your issue..

Please check and let me know if it helps

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Naveen,

You are first getting the value from whatId and then you are checking if it is not null

see below code:

   String objName = t.WhatId.getSObjectType().getDescribe().getName();

            if(t.WhatId!=null )
{

}

There is no use of this t.whatId != null, because if this will null then it will throw error on previous line where you are doing t.whatId.getSobjectType()...du eto null it will be null.getSobjecttype()..

this is the reason of your error..

you can first check if it is null or not then inside the same if condition you can get like below

 if(t.WhatId!=null )
{
       String objName = t.WhatId.getSObjectType().getDescribe().getName();
}


This will solve your issue..

Please check and let me know if it helps

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Naveen KvpNaveen Kvp
Thanks Sandeep for your help! your answer is really help me alot :)