You need to sign in to do that
Don't have an account?
k 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...........
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...........
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
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.
I want
How to update account Email field when i create or update contact email based?
please help me..........
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);
}
Hi,
I have one small requirement How to display Below Objects records hierarchy
The hierarcy should be
Account
---- Opportunity
----- Contact
please help me............