+ Start a Discussion
Deepu BDeepu B 

Simple Trigger, Not working with my Code.

Hi all,

My Scenario, i inserted some contacts with Dataloader in to Contacts Obj while inserting i left Account Name field empty in Contacts Obj. Now i want to update that field(Account Name field in Contact Obj).

Ex: For Contacts, starts with 'Al%' Account Name will be "Infosys".
      For Contacts, starts with 'Na%' Account Name will be "IBM".
      For Contacts,  starts with 'Ma%' Account Name will be "TCS".

for this i wrote a trigger as below it didn't showing error but not updating Account Name field in Contact.


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

list<contact> lst = new list<contact>([select id,name from contact where name like 'al%']);
for(contact c: trigger.new)


      c.account.name = 'Infosys';
    insert lst;
Hi, you would need to lookup the accounts and then update the AccountID in Contact. You can use this code as a guide, but debug and fix if required. Am assuming that the three accounts with name Infosys, IBM and TCS exist. And, since there is no specific logic, I will just use these 3 names and directly map them. Also, this code is case-sensitive, you can tweak it to be case insensitive as well.
tgigger updateAccount on Contact(before insert, before update){
	Map<String, Account> accMap = new Map<String, Account>(); 
	for(Account acc: [select id, name from Account where name in ('Infosys','IBM','TCS')]){
		accMap.put(acc.name, acc); 
	for(Contact con: trigger.new){
		if(con.name.startsWith('AI') && accMap.containsKey('Infosys')){
			con.AccountId = accMap.get('Infosys').id;
		else if(con.name.startsWith('Na') && accMap.containsKey('IBM')){
			con.AccountId = accMap.get('IBM').id;
		else if(con.name.startsWith('Ma') && accMap.containsKey('TCS')){
			con.AccountId = accMap.get('TCS').id;

Deepu BDeepu B
It is also Not working, Hargobind_Singh!  did my approach is wrong.?
pankaj kabra 8pankaj kabra 8
I think issue may be of the hardocded values(Infosys, TCS, AI etc) and their case. like AI is in uppercase here and in data it might be in lowecase.
Just match the values and check again. Even if this doesn't work, let me know.
Deepu BDeepu B
This can't be solving by many people,

Let me clear some more on this scenario,

1. I loaded data from data loader in to contact Obj (Say: 50)
2. while loading i left Account field empty in contac Object.
3. Now i want to update that Account field in Contact Obj, which contact names starts with 'Al%' update the account 'Infosys'

for above how could write the trigger.?