You need to sign in to do that
Don't have an account?
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;
}
}
}
}
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;
}
}
}
}
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
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