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
k sfdck sfdc 

URGENT:how to solve System.ListException: List index out of bounds:below a trigger?

Hi,
       trigger Trg_onContact on Contact (before insert, before update) {

List<ID> accountIds=new List<ID>();

for(Contact con:Trigger.New)
{
  if(con.Email != null && con.AccountId !=null)
  {
   accountIds.add(con.AccountId);
  }
}
List<Contact> contactList=[SELECT Email FROM Contact WHERE Id IN : accountIds];
List<Account> accountList=[SELECT Name,Email__C FROM Account WHERE Id IN :accountIds];

for(Integer i=0; i < accountList.size(); i++)
{
  String Email = ''+ contactList[0].get('Email');
  accountList[i].Email__C=Email;
 
}

update accountlist;

System.debug('***accountlist****'+accountlist);

}

please help me...........
Best Answer chosen by k sfdc
kevin lamkevin lam
Try this:

trigger Trg_onContact on Contact (before insert, before update) {

// List<ID> accountIds=new List<ID>();
Map<ID, String) accountEmailMap = new Map<ID, String>();
List<Account> accountlist = new List<Account>();

for(Contact con:Trigger.New)
{
  if(con.Email != null && con.AccountId !=null)
  {
// accountIds.add(con.AccountId);
accountEmailMap.put(con.AccountId, con.Email);
  }
}
// List<Contact> contactList=[SELECT Email FROM Contact WHERE Id IN : accountIds];
// List<Account> accountList=[SELECT Name,Email__C FROM Account WHERE Id IN :accountIds];

// for(Integer i=0; i < accountList.size(); i++)
// {
//  String Email = ''+ contactList[0].get('Email');
//  accountList[i].Email__C=Email;

// }

For (Account accountRecord : [SELECT Id, Email__c FROM Account WHERE Id IN :accountEmailMap.keySet()]) {
     accountRecord.Email__c = accountEmailMap.get(accountRecord.Id);
     accountlist.add(accountRecord);
}

update accountlist;

System.debug('***accountlist****'+accountlist);

}

All Answers

kevin lamkevin lam
For this line:

List<Contact> contactList=[SELECT Email FROM Contact WHERE Id IN : accountIds];

The contactList is always empty because the Id in the query is a Contact Id.
k sfdck sfdc
Hi,
      I want
             How to update account Email field when i create or update contact email based?

please help me..........
kevin lamkevin lam
Try this:

trigger Trg_onContact on Contact (before insert, before update) {

// List<ID> accountIds=new List<ID>();
Map<ID, String) accountEmailMap = new Map<ID, String>();
List<Account> accountlist = new List<Account>();

for(Contact con:Trigger.New)
{
  if(con.Email != null && con.AccountId !=null)
  {
// accountIds.add(con.AccountId);
accountEmailMap.put(con.AccountId, con.Email);
  }
}
// List<Contact> contactList=[SELECT Email FROM Contact WHERE Id IN : accountIds];
// List<Account> accountList=[SELECT Name,Email__C FROM Account WHERE Id IN :accountIds];

// for(Integer i=0; i < accountList.size(); i++)
// {
//  String Email = ''+ contactList[0].get('Email');
//  accountList[i].Email__C=Email;

// }

For (Account accountRecord : [SELECT Id, Email__c FROM Account WHERE Id IN :accountEmailMap.keySet()]) {
     accountRecord.Email__c = accountEmailMap.get(accountRecord.Id);
     accountlist.add(accountRecord);
}

update accountlist;

System.debug('***accountlist****'+accountlist);

}
This was selected as the best answer
k sfdck sfdc

Hi,
    I have one small requirement How to display Below Objects records hierarchy

The hierarcy should be
     Account
     ---- Opportunity
            ----- Contact

please help me............