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
donalmdonalm 

Using a trigger to record information entered on a record field.

I have a client who wants to enter information into a Text (?) field on a Salesforce record.  Different users could update this field at various times, but when the user hits the Save button on the record, the information gets saved in that field, and the user name, and date/time should be stamped on the entry, as well.
Ideally, the most recent entry will always go to the top.  Thus, the next user entering information will see who made the last entry, when it was done, and determine what next steps might be.
Can I create a trigger to make that happen? 
Best Answer chosen by donalm
GovindarajGovindaraj
Hi,
Create a seperate 'Text Area' field and log the history of 'Text' field you want.

Let's take the object is Opportunity and i want to store the history of 'NextStep' field in 'Next_Step_History__c'.
trigger oppNextstepHistory on Opportunity (Before Insert, Before Update) {
    DateTime dateinst = System.now();    
    if(trigger.isInsert)
        {
            for(Opportunity oppIns:trigger.new)
            {
                if(oppIns.NextStep != null)
                {
                    oppIns.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+oppIns.NextStep; 
                }
            }        
        }        
        if(trigger.isUpdate)
        {
            for(Opportunity oppUpd:trigger.new)
            {
               if(oppUpd.NextStep != null)
                {
                   if((trigger.oldMap.get(oppUpd.id).NextStep != trigger.newMap.get(oppUpd.id).NextStep) && (oppUpd.Next_Step_History__c != null))
                                  {
                                      oppUpd.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+trigger.newMap.get(oppUpd.id).NextStep+'\n'+oppUpd.Next_Step_History__c;
                                  }
                                  else if(oppUpd.Next_Step_History__c == null) 
                                  {
                                      oppUpd.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+trigger.newMap.get(oppUpd.id).NextStep;                                        
                                  }
                }
            }            
        }    
}

Please let us know, if this helps.

Thanks,
Govindaraj.S

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
What does "username & date/time stamped on same entry" mean? you want them to be updated automatically on the record or within the same field?

Assuming this is some sort of history tracking you are trying to chieve.....Salesforce has standard fields which automatically capture user who has updated a field value. 
GovindarajGovindaraj
Hi,
Create a seperate 'Text Area' field and log the history of 'Text' field you want.

Let's take the object is Opportunity and i want to store the history of 'NextStep' field in 'Next_Step_History__c'.
trigger oppNextstepHistory on Opportunity (Before Insert, Before Update) {
    DateTime dateinst = System.now();    
    if(trigger.isInsert)
        {
            for(Opportunity oppIns:trigger.new)
            {
                if(oppIns.NextStep != null)
                {
                    oppIns.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+oppIns.NextStep; 
                }
            }        
        }        
        if(trigger.isUpdate)
        {
            for(Opportunity oppUpd:trigger.new)
            {
               if(oppUpd.NextStep != null)
                {
                   if((trigger.oldMap.get(oppUpd.id).NextStep != trigger.newMap.get(oppUpd.id).NextStep) && (oppUpd.Next_Step_History__c != null))
                                  {
                                      oppUpd.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+trigger.newMap.get(oppUpd.id).NextStep+'\n'+oppUpd.Next_Step_History__c;
                                  }
                                  else if(oppUpd.Next_Step_History__c == null) 
                                  {
                                      oppUpd.Next_Step_History__c = dateinst.format('dd-MMM-yyyy','America/Los_Angeles')+'\t-\t'+trigger.newMap.get(oppUpd.id).NextStep;                                        
                                  }
                }
            }            
        }    
}

Please let us know, if this helps.

Thanks,
Govindaraj.S
This was selected as the best answer