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
ch ranjithch ranjith 

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;
}
Best Answer chosen by ch ranjith
nitin sharmanitin sharma
Hi ,


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

MJ Kahn / OpFocusMJ Kahn / OpFocus
A description of what you'd like to accomplish would be helpful.... Currently, it looks like you're saying, "Whenever a Contact is created, set the Contact's Account's Sales Rep to the value stored in the Contact's Department field." That doesn't make much sense to me, but it's what I think the code is trying to do.

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.

trigger realtime1 on Contact (after insert) {
    Map<Id, Account> mapAccounts = new Map<Id, Account>();
    for (Contact con:trigger.new)  {
        if (con.AccountId != null) {
            Account a = new Account(Id=con.AccountId);
            a.sales_rep__c=con.department;
            mapAccounts.put(con.AccountId, a);
        }
    }
    
    if (!mapAccounts.isEmpty()) update mpaAccounts.values();
}


nitin sharmanitin sharma
Hi ,


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;

}
This was selected as the best answer
Anand LaturkarAnand Laturkar
Hi All Same As it is i get following Error
[{"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":[]}]
Anand LaturkarAnand Laturkar
Hi All Same As it is i get following Error
[{"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