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
sfdc_apex_learnersfdc_apex_learner 

Need help with a Case trigger to populate a Case email field

Hello All,

 

I need some help with a case trigger. Here's my setup and what I am trying to do:

 

I have a Case email field called Designated Email Address (from Account) - it's API Name is Designated_Email_Address_from_Account__c. I also have an Account email field called Designated Email Address - API Name is Designated_Email_Address__c. On the Case detail page, I have the standard Account Name field which is a lookup on the Account object.

 

What I want done via a trigger is when I update the Account Name field on the Case by the lookup, that it will also populate the Designated_Email_Address_from_Account__c on the Case based on the Designated_Email_Address__c from the Account.

 

Please post the code to make this happen along with code comments as I would like to learn how you came up with the code.

 

Thanks!

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

Hi

   will you check this code..

trigger UpdateAccountEmailInCase on Case (before insert, before update)
{


    for(Case c : Trigger.new){

        Account account = [SELECT ID, Name,  Designated_Email_Address__c FROM ACCOUNT WHERE ID = :c.AccountId];
 
        if(account != null){
            c.Designated_Email_Address_from_Account__c = account.Designated_Email_Address__c;
   
       }
    }
}

 Did this post answers your question if so please mark it solved so that others get benifited.

 

 

       Thanks

All Answers

asish1989asish1989

Hi

   will you check this code..

trigger UpdateAccountEmailInCase on Case (before insert, before update)
{


    for(Case c : Trigger.new){

        Account account = [SELECT ID, Name,  Designated_Email_Address__c FROM ACCOUNT WHERE ID = :c.AccountId];
 
        if(account != null){
            c.Designated_Email_Address_from_Account__c = account.Designated_Email_Address__c;
   
       }
    }
}

 Did this post answers your question if so please mark it solved so that others get benifited.

 

 

       Thanks

This was selected as the best answer
sfdc_apex_learnersfdc_apex_learner

Thanks asish1991 that worked.

sfdc_apex_learnersfdc_apex_learner

Hey asish,

 

So I got this to work in my dev org.

 

However, now I am trying deploy this to my production using Eclipse and I am getting:

 

1 Error - test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

2 Warnings - File only saved locally, not to server.

 

Could you provide the code to fix this?

asish1989asish1989

Hi

   you need to write a test class for code coverage of that trigger...

  Here is some reference you can refer 

 

 

 

@isTest
private class mytest {

  @isTest static  void CodecoverageUpdateAccountEmailInCase() { 
     Account account = new Account(Name='test' ,  Account_Email__c = 'test@gmail.com');
     insert account;
     Case testcase = new Case(Status='New', Origin = 'Phone', AccountId = account.id);
     insert testcase;
     case.Designated_Email_Address_from_Account__c = account.Account_Email__c;
     update testcase;
    
   }
}

 

Thanks

 

sfdc_apex_learnersfdc_apex_learner

Asish,

 

Thanks very much for the test class. I am getting it to save locally in the Force.com IDE however I can't deploy to the server.

 

Here is a screenshot of error message and you can see the test class behind it at https://www.box.com/s/w15re8q9evoimv6baipa

 

Please advise.

 

Thanks!