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
Prema -Prema - 

Write a trigger on contact to create a contact, if email exists then update the old value of email as well as new value under description field of conatct..pls help..I am a fresher

AvaneeshAvaneesh
Hi Prema 

Can you explain your trigger with an example I am confused 

Thank you
Avaneesh Singh
Prema -Prema -
Sorry, if you have updated your email then it should show the old value as well as the new value +owner name on that contact record.
AvaneeshAvaneesh
Hi Prema 
writing a trigger for you and i am not able to understand your complete Requirement but let me tell you this trigger is an update in which if any email address of contact is changed then new email is stored in the email field and old email id will store in the description field 
trigger ContactTriggerForStatus on Contact (before update) {
    
    for(contact con :trigger.new){
        if(!trigger.oldMap.get(con.id).email.EQUALS(trigger.newMap.get(con.id).email)){
         con.Email=trigger.newMap.get(con.id).email;// new email id is inserted in contact 
         con.Description = trigger.oldMap.get(con.id).email+ ' ' +con.Owner.name;
        }
        
    }
  
   
}

if you like my solution please mark as best else let me know your problem 

Thank you
Avaneesh Singh 
Amit Chaudhary 8Amit Chaudhary 8
Hi Prema ,

I will suggest your to start learning trigger from trailhead.
1) https://trailhead.salesforce.com/modules/apex_triggers
 
trigger ContactTriggerForStatus on Contact (before update) {
    
    for(contact con :trigger.new)
	{
        if( con.email != trigger.oldMap.get(con.id).email )
		{
			// Here con.email is new email and trigger.oldMap.get(con.id).email is old email
			con.Description = trigger.oldMap.get(con.id).email+ ' ' +con.Owner.name;
        }
    }
}
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework

1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org. 

3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers


4) Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately
It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail
Prema -Prema -
Hello Amit Chaudhary 8,
I tried your code but I am getting null for the owner.name..Can you please check,..
Prema -Prema -
Hello 
Avaneesh, Thanks for the response but I am getting the same error on your code as well..
AvaneeshAvaneesh
Hi Prema 
this code is working fine check it
that 
works only for after update record and in the last trigger we didn't get id so we are not able to find out owner name 
trigger ContactTriggerForStatus on Contact (after update) {
    set<id> allidset = new set<id>();
    List<contact> conList = new List<contact>();
     for(contact con:trigger.new){
        if(con.email!=NULL){
            allidset.add(con.Id);
        }
    }
    
    for(contact con :[select id,owner.name,email from contact where id IN : allidset]){
        System.debug('ss'+con.owner.name);
        if(trigger.oldMap.get(con.id).email!=null && !trigger.oldMap.get(con.id).email.EQUALS(trigger.newMap.get(con.id).email)){
         contact c = new contact();
         c.Id=con.Id;
         c.Email=trigger.newMap.get(con.id).email;// new email id is inserted in contact 
         c.Description = trigger.oldMap.get(con.id).email+ ' ' +con.Owner.name;
         conList.add(c);   
        }
        
    }
    if(conList.size()>0){
        update conList;
    }
  
}

if this was helpful mark best answer else let me know issue.
Thank you
Avaneesh Singh

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Prema / Avaneesh ,

No need to After update event and doing the update again. You just need to query the related field owner.name. You can get the ownerId directly but to get the owner.Name you need query.

Please try below code.
trigger ContactTriggerForStatus on Contact (before update) {
 
    Set<ID> setContId = new Set<ID>();
    for(contact con :trigger.new)
    {
        if( con.email != trigger.oldMap.get(con.id).email )
        {
            setContId.add(con.id);
        }
    }
    
    Map<Id,Contact> mapContact = new Map<Id,Contact>([Select id,Owner.name from contact where id in :setContId]);

    for(contact con :trigger.new)
    {
        if( con.email != trigger.oldMap.get(con.id).email && mapContact.containsKey(con.id) )
        {
            con.Description = trigger.oldMap.get(con.id).email+ ' ' +mapContact.get(con.id).Owner.name;
        }
    }
    
    
}

Let us know if this will help you