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
NasirNasir 

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;
    }
  }
}
Please help me on this
Thanks
Nasir

 

Best Answer chosen by Admin (Salesforce Developers) 
EnthEnth

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:

 

 

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

  List<Account> updateList = new List<Account>();

for(Contact co : Trigger.new){
   // 
   Account acc = [SELECT Customer_Email__c FROM Account WHERE Id = :co.AccountId];
if(acc.Customer_Email__c == null || acc.Customer_Email__c =='')
   { 
    acc.Customer_Email__c = co.Email;
    updateList.add(acc);
   }
  }

  if (updateList.size() > 0) update updateList;

}

 

Get that working first, understand it and you can then re-write the bulkified version.

 

All Answers

sravusravu

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.

EnthEnth

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:

 

 

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

  List<Account> updateList = new List<Account>();

for(Contact co : Trigger.new){
   // 
   Account acc = [SELECT Customer_Email__c FROM Account WHERE Id = :co.AccountId];
if(acc.Customer_Email__c == null || acc.Customer_Email__c =='')
   { 
    acc.Customer_Email__c = co.Email;
    updateList.add(acc);
   }
  }

  if (updateList.size() > 0) update updateList;

}

 

Get that working first, understand it and you can then re-write the bulkified version.

 

This was selected as the best answer
EnthEnth

Are you trying to update the value on the Contact or the value on the Account ?

sravusravu

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.

NasirNasir

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

EnthEnth

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.

 

 

EnthEnth

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:

 

  1. Make sure you're posting to the right board
  2. Search first to check if the problems already listed. Remember to check out the DeveloperForce Recipes/Cookbook.
  3. Describe what you're trying to do
  4. Always include the code snippet of what you've done and the errors/problems with it
That should normally guarantee you get a good response from someone.
Good luck,
Enth

 

NasirNasir

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