You need to sign in to do that
Don't have an account?

Apex trigger realtime1 caused an unexpected exception, contact your administrator: realtime1: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.realtime1: line 19, column 1
trigger realtime1 on Contact (after insert)
{
set<id> set1=new set<id>();
list<account> aclist=new list<account>();
list<account> acnewlist=new list<account>();
list<Account> listac=new list<account>();
for(contact con:trigger.new)
{
set1.add(con.accountid);
}
acnewlist=[select id,name,billingaddress
from account where id in:set1];
for(contact con:trigger.new)
{
for(integer i=0;i<acnewlist.size();i++)
{
if(con.accountid==acnewlist[i].id)
{
con.account.sales_rep__c=con.department;
aclist.add(con.account);
}
}
}
update aclist;
}
{
set<id> set1=new set<id>();
list<account> aclist=new list<account>();
list<account> acnewlist=new list<account>();
list<Account> listac=new list<account>();
for(contact con:trigger.new)
{
set1.add(con.accountid);
}
acnewlist=[select id,name,billingaddress
from account where id in:set1];
for(contact con:trigger.new)
{
for(integer i=0;i<acnewlist.size();i++)
{
if(con.accountid==acnewlist[i].id)
{
con.account.sales_rep__c=con.department;
aclist.add(con.account);
}
}
}
update aclist;
}
If you are trying to copy values from Contact to the Account record then below code will work
trigger realtime1 on Contact (after insert)
{
//set<id> set1=new set<id>();
map<id,contact> id=new map<id,contact>();
list<account> aclist=new list<account>();
list<account> acnewlist=new list<account>();
list<Account> listac=new list<account>();
for(contact con:trigger.new)
{
id.put(con.accountid,con);
}
acnewlist=[select id,name,sales_rep__c
from account where id in:id.keyset()];
list<account> acclist=new list<account>();
for(Account sap:acnewlist)
{
contact c=id.get(sap.id);
sap.sales_rep__c=c.department;
Acclist.add(sap);
}
update acclist;
}
All Answers
You're getting messed up where you're trying to put con.department in the Contact's Account's sales_rep__c field. con.account is null, so you can't access its sales_rep__c field.
If what I described is what you're really trying to accomplish, the following should do it for you.
If you are trying to copy values from Contact to the Account record then below code will work
trigger realtime1 on Contact (after insert)
{
//set<id> set1=new set<id>();
map<id,contact> id=new map<id,contact>();
list<account> aclist=new list<account>();
list<account> acnewlist=new list<account>();
list<Account> listac=new list<account>();
for(contact con:trigger.new)
{
id.put(con.accountid,con);
}
acnewlist=[select id,name,sales_rep__c
from account where id in:id.keyset()];
list<account> acclist=new list<account>();
for(Account sap:acnewlist)
{
contact c=id.get(sap.id);
sap.sales_rep__c=c.department;
Acclist.add(sap);
}
update acclist;
}
[{"message":"trgInsertUpdateCommunication: execution of AfterInsert\n\ncaused by: System.NullPointerException: Attempt to de-reference a null object\n\nTrigger.trgInsertUpdateCommunication: line 20, column 1","errorCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","fields":[]}]
[{"message":"trgInsertUpdateCommunication: execution of AfterInsert\n\ncaused by: System.NullPointerException: Attempt to de-reference a null object\n\nTrigger.trgInsertUpdateCommunication: line 20, column 1","errorCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","fields":[]}]
Can any one help me for this