• brijender singh rathore 16
  • NEWBIE
  • 25 Points
  • Member since 2017

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 8
    Replies
Hi
I have created an apex trigger to update contact address when the account address is changed. The code worked fine for already created accounts, but when I have created a new account the following error was thrown and I was not able to save the account. Please help me.

Error: updateContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateContact: line 33, column 1​
 
trigger updateContact on Account (after insert, after update) {
    List<Contact> contactsForUpsert = New List<Contact>();

    Map<Id, Contact> contactsByAccountId = new Map<Id, Contact>();
    List<Contact> contacts = [SELECT Id,
                                     AccountId,
                                     mailingstreet,
                                     mailingcity,
                                     mailingstate,
                                     mailingpostalcode
                            FROM Contact WHERE AccountId IN: Trigger.newMap.keySet()];
    for (Contact cont: contacts) {
        contactsByAccountId.put(cont.accountId, cont);
    }

    for (Account account: Trigger.new) {

        Account old;
        if (Trigger.isUpdate) {
            old = Trigger.oldMap.get(account.Id);
        }

        Boolean isShippingAddressChangedOrNew = Trigger.isInsert ? true :
                                                (   account.ShippingStreet != old.ShippingStreet
                                                 || account.ShippingCity != old.ShippingCity
                                                 || account.ShippingState != old.ShippingState
                                                 || account.ShippingPostalCode != old.ShippingPostalCode
                                                 || account.ShippingCountry != old.ShippingCountry) ? true : false;

        if (isShippingAddressChangedOrNew) {
            Contact relatedContact = contactsByAccountId.get(account.Id);
           
            relatedContact.mailingstreet     = account.Shippingstreet;
            relatedContact.mailingcity       = account.Shippingcity;
            relatedContact.mailingstate      = account.Shippingstate;
            relatedContact.mailingpostalcode = account.shippingpostalcode;
            contactsForUpsert.add(relatedContact);
        

    }

    try {
        upsert contactsForUpsert;
    } catch(Dmlexception e) {
        System.debug(LoggingLevel.ERROR, 'Contact insert from createopp.trigger has failed with message:' + e.getMessage());
    }
}
}

 
  • January 23, 2018
  • Like
  • 0
PLZ HELP ME HOW GET ConvertedAccountid TO WHICH LEAD IS GEETING CONVERTED
IT IS SHOWING NULL IN SYSTEM DEBUG.
trigger converted on Lead (after insert)
{
set<id> s= new set<id>();
list<account> acc = new list<account>();
list<contact> cs = new list<contact>();
list<task> tsk = new list<task>();
list<opportunity> ops = new list<opportunity>();
for (lead ls:trigger.new)
{

s.add(ls.id);

}
for(lead l:[select id,ConvertedAccountId,company from lead where id in:s])
{
 system.debug('*************ConvertedAccountId************'+l.ConvertedAccountId);
account a = new account();
a.id = l.ConvertedAccountId;
a.name = l.company;

acc.add(a);
system.debug('@@@@@@'+a);
opportunity o = new opportunity();
o.name = l.company;
o.accountid = l.ConvertedAccountId;
o.StageName = 'Prospecting';
o.CloseDate = system.today();

ops.add(o);
system.debug('@@@@@@'+o);
task t = new task();
t.subject = 'converted lead task';
t.whatid = l.ConvertedAccountId;
t.priority = 'Normal';
t.status = 'Not Started';

tsk.add(t);
system.debug('@@@@@@'+t);
contact c = new contact();
c.lastname = l.company;
c.accountid = l.ConvertedAccountId;

cs.add(c);
system.debug('@@@@@@'+c);
}
insert acc;
insert cs;
insert tsk;
insert ops;


}
latest__c is rollup summary field = max(opportunity).This trigger is mapping latest opportunity amount into account field (acc.opty_amount__c).
trigger latest on Account (after update)  {    
  if(checkRecursive.runOnce())  
  {    set<id> acctid =new set<id>();    
  list<account> accnt=new list<account>();  
  for(account acc:trigger.new)  
  {       acctid.add(acc.id);           }          
  for(account acc : [select id,name,opty_amount__c,latest__c(select id,amount,CreatedDate from opportunities) from account where id  in:acctid])    {    list <opportunity> oppo = acc.opportunities;    
for(opportunity op:oppo)   
 {    if(acc.latest__c == op.CreatedDate)    
      {    acc.opty_amount__c=op.amount;  
      }
 
  }          
    accnt.add(acc);    
  }    
 update accnt;  
}
THIS TEST CLASS IS GIVING 80% COVERAGE PLZ HELP ME TO CONVERT IT TO 100%.
@isTest
private class UpdateAccount_Test
{  public static testMethod void TestUpdateAccount() 
  {        Account acc = new Account();           
         acc.Name = 'Test';              
         insert acc;         
         opportunity oppo = new opportunity();          
         oppo.accountid  =acc.id;     
         oppo.amount = 50000.00;                        
        update acc;    
        }  
 }
PLZ HELP ME HOW GET ConvertedAccountid TO WHICH LEAD IS GEETING CONVERTED
IT IS SHOWING NULL IN SYSTEM DEBUG.
trigger converted on Lead (after insert)
{
set<id> s= new set<id>();
list<account> acc = new list<account>();
list<contact> cs = new list<contact>();
list<task> tsk = new list<task>();
list<opportunity> ops = new list<opportunity>();
for (lead ls:trigger.new)
{

s.add(ls.id);

}
for(lead l:[select id,ConvertedAccountId,company from lead where id in:s])
{
 system.debug('*************ConvertedAccountId************'+l.ConvertedAccountId);
account a = new account();
a.id = l.ConvertedAccountId;
a.name = l.company;

acc.add(a);
system.debug('@@@@@@'+a);
opportunity o = new opportunity();
o.name = l.company;
o.accountid = l.ConvertedAccountId;
o.StageName = 'Prospecting';
o.CloseDate = system.today();

ops.add(o);
system.debug('@@@@@@'+o);
task t = new task();
t.subject = 'converted lead task';
t.whatid = l.ConvertedAccountId;
t.priority = 'Normal';
t.status = 'Not Started';

tsk.add(t);
system.debug('@@@@@@'+t);
contact c = new contact();
c.lastname = l.company;
c.accountid = l.ConvertedAccountId;

cs.add(c);
system.debug('@@@@@@'+c);
}
insert acc;
insert cs;
insert tsk;
insert ops;


}
trigger trigger52 on India__c (after insert) {
  list<san_francisco__c> con =new list<san_francisco__c>();  

    for(India__c h : trigger.new){
     san_francisco__c s = new san_francisco__c();
      s.Name=h.Name;
        s.Company__c=h.Company__c;
        s.Mobile__c=h.Mobile__c;
           s.Email__c=h.Email__c;
        con.add(s);
        
        
    }
    insert con;
}
latest__c is rollup summary field = max(opportunity).This trigger is mapping latest opportunity amount into account field (acc.opty_amount__c).
trigger latest on Account (after update)  {    
  if(checkRecursive.runOnce())  
  {    set<id> acctid =new set<id>();    
  list<account> accnt=new list<account>();  
  for(account acc:trigger.new)  
  {       acctid.add(acc.id);           }          
  for(account acc : [select id,name,opty_amount__c,latest__c(select id,amount,CreatedDate from opportunities) from account where id  in:acctid])    {    list <opportunity> oppo = acc.opportunities;    
for(opportunity op:oppo)   
 {    if(acc.latest__c == op.CreatedDate)    
      {    acc.opty_amount__c=op.amount;  
      }
 
  }          
    accnt.add(acc);    
  }    
 update accnt;  
}
THIS TEST CLASS IS GIVING 80% COVERAGE PLZ HELP ME TO CONVERT IT TO 100%.
@isTest
private class UpdateAccount_Test
{  public static testMethod void TestUpdateAccount() 
  {        Account acc = new Account();           
         acc.Name = 'Test';              
         insert acc;         
         opportunity oppo = new opportunity();          
         oppo.accountid  =acc.id;     
         oppo.amount = 50000.00;                        
        update acc;    
        }  
 }
Hi
I have created an apex trigger to update contact address when the account address is changed. The code worked fine for already created accounts, but when I have created a new account the following error was thrown and I was not able to save the account. Please help me.

Error: updateContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateContact: line 33, column 1​
 
trigger updateContact on Account (after insert, after update) {
    List<Contact> contactsForUpsert = New List<Contact>();

    Map<Id, Contact> contactsByAccountId = new Map<Id, Contact>();
    List<Contact> contacts = [SELECT Id,
                                     AccountId,
                                     mailingstreet,
                                     mailingcity,
                                     mailingstate,
                                     mailingpostalcode
                            FROM Contact WHERE AccountId IN: Trigger.newMap.keySet()];
    for (Contact cont: contacts) {
        contactsByAccountId.put(cont.accountId, cont);
    }

    for (Account account: Trigger.new) {

        Account old;
        if (Trigger.isUpdate) {
            old = Trigger.oldMap.get(account.Id);
        }

        Boolean isShippingAddressChangedOrNew = Trigger.isInsert ? true :
                                                (   account.ShippingStreet != old.ShippingStreet
                                                 || account.ShippingCity != old.ShippingCity
                                                 || account.ShippingState != old.ShippingState
                                                 || account.ShippingPostalCode != old.ShippingPostalCode
                                                 || account.ShippingCountry != old.ShippingCountry) ? true : false;

        if (isShippingAddressChangedOrNew) {
            Contact relatedContact = contactsByAccountId.get(account.Id);
           
            relatedContact.mailingstreet     = account.Shippingstreet;
            relatedContact.mailingcity       = account.Shippingcity;
            relatedContact.mailingstate      = account.Shippingstate;
            relatedContact.mailingpostalcode = account.shippingpostalcode;
            contactsForUpsert.add(relatedContact);
        

    }

    try {
        upsert contactsForUpsert;
    } catch(Dmlexception e) {
        System.debug(LoggingLevel.ERROR, 'Contact insert from createopp.trigger has failed with message:' + e.getMessage());
    }
}
}

 
  • January 23, 2018
  • Like
  • 0