You need to sign in to do that
Don't have an account?
Nasir
trigger which will update an email field from contact to customer emailfield in account
Hi
i am trying to write a trigger which will update an email field from contact to customer emailfield in account.But this is not working.
trigger updatecontact on Contact (before insert, before update) {
Account acc = [SELECT Customer_Email__c FROM Account Limit 1];
Contact con = [SELECT Email FROM Contact limit 1 ];
for(Contact co : Trigger.new){
if(acc.Customer_Email__c == null || acc.Customer_Email__c =='')
{ acc.Customer_Email__c = co.Email;
}
}
}
Account acc = [SELECT Customer_Email__c FROM Account Limit 1];
Contact con = [SELECT Email FROM Contact limit 1 ];
for(Contact co : Trigger.new){
if(acc.Customer_Email__c == null || acc.Customer_Email__c =='')
{ acc.Customer_Email__c = co.Email;
}
}
}
Please help me on this
Thanks
Nasir
OK, just as an initial outline you want something more like this, but note this is not bulkified and will break governor limits if you import more than 20 rows at a time. It's straightforward to bulkify, but one step at a time:
Get that working first, understand it and you can then re-write the bulkified version.
All Answers
try the following code:
trigger updatecontact on Contact (before insert, before update) {
public Id aId;
public Id cId;
for(Contact c : Trigger.New){
cId = c.Id;
if(c.Email==NULL && c.AccountId != NULL){
aId = c.AccountId;
}
}
if(aId!=NULL){
Account acc = [select Customer_Email__c from Account where Id = :aId];
for(Contact con : Trigger.New){
con.Email = acc.Customer_Email__c;
}
}
}
Let me know if you face any difficulty.................
Hope this helps you.
OK, just as an initial outline you want something more like this, but note this is not bulkified and will break governor limits if you import more than 20 rows at a time. It's straightforward to bulkify, but one step at a time:
Get that working first, understand it and you can then re-write the bulkified version.
Are you trying to update the value on the Contact or the value on the Account ?
As per my understanding, The contact Email field has to be updated with the Customer_Email__c field on Account. It is not good to use SOQL query inside the for loop.
Hi Enth,
Thanks for your help.This worked:)
Enth can you please tell me how "id" field is choosen .Actuall i don't have concept of "id".How should i use this in SOQL query??
Can you please elaborate this so i can get an overall concept.
Can i have your email id please.i want to be in touch with u so i can excel.
Thanks a lot
Nasir
sravu - yes indeed, it's not good to use SOQL instead the loop, but when someone's new to Apex throwing them in the deep end isn't the best way to learn to swim! Shorter the code easier for someone new to understand and if you check my posting I did say this was NOT optimised.
Hi Nasir,
You really should spend the time to read the Apex Developer Guide, it's fundamental reading for developing in Apex and you need to learn how to write good Apex and good tests if you want to publish anything to a production Org.
I don't email directly as I try to limit my email traffic via daily digests, but I do keep an eye on the forum's and there's plenty of other great contributors around.
All I suggest is that when you post:
Thanks a lot Enth.As i keep reading apex guide!!!But when it comes to ID which we use in SOQL query.I never find examples which has clearly explained about using ID.
Is there any way so i can contact u instantly or u having a Facebook account?
Thanks For your help .
Nasir