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
Richa_LearningRicha_Learning 

Trigger to Copy value from one field to another

Hi,
I am trying to copy a Standard Field value to Custom field in the same object.
I need to copy Name field  (standard) from Event to Check_name__C (custom field) to Event only using trigger.

Actualy am not sure how to write a trigger but I tried ..an anyone please help with this.

trigger insertname on Event (after insert) {
Set<Id> Ids= new Set<Id>();
    for (Event E1 : Trigger.new)
    {
        Ids.add(E1.Id);       
    }
List<Event> EventList = new List<Event>([Select Id,whoId from event where Id in :Ids]);

    for(Event temp : EventList )
    {
        Event e2 = new e2();
       
                insert e2;

    }


}
Ramu_SFDCRamu_SFDC
Hi,

Since the name field of Event object is a lookup field related either to Contact or Lead we first need to identify what object it is related. Refer to this post to know how to find the object name based on id https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xxqIAA .
Once you get the object name, query for the name of the record on this object based on who id and update the custom field.
Richa_LearningRicha_Learning

Thanks.
Yes I got the ID.
For contacts its "001" and for  Lead its "00U"

Now can you help how to update the custom field?
Ramu_SFDCRamu_SFDC
Here is the sample code after you get the id prefix for Contacts and Leads

**custom_name__c is the custom field of Text datatype on Event object 

trigger neweventcopyname on Event (before insert,before update) {  
    for(Event evnt:trigger.new){
        string name;
        if(evnt.WhoId!=null){          
            string objid = evnt.WhoId;
        string code=objid.substring(0,3);
        if(code=='003'){          
           name=[select firstname from contact where id=:objid limit 1].firstname;          
        }
        else{
            name=[select firstname from lead where id=:objid limit 1].firstname;          
        }
        evnt.custom_name__c=name;
        }   
    }
}

This is just a sample code that works fine for the single record insert or update scenarios. You need to consider bulkification of this code for masss insert/update scenarios.

Mark this post as Best Answer if this resolved your requirement so that it would help others as well. 
sohail najmisohail najmi
My Working:

Here i am showing data from lookup field into table. when querying only id's are fetch for lookup fields. 
obj1: Service__c 
obj2: Service_Type__c 

Get the id of NAME field (lookup with Service_type) from Service__c and then querying back in service_type to get the name and show in service obj. 


    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
        List<Service__c > lstService = [select id, Code__c,Service_Group__c from Service__c ];
        if(lstService.size() > 0 ) {
             for (Service__c  obj: Trigger.new){
             string name1;
                name1 = [select id, Name from Service_Type__c where id=:obj.Service_Type__c ].Name;
               //  system.debug('name1  ->' + name1 );
                  obj.Name = name1;   [updating the actual field name with Id]
                  break;
            }

Hope this answer helps anyone. .

Thanks