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
Sarma DuvvuriSarma Duvvuri 

Insert Case Owner Name in text field using apex

Hi All,

I have one text field. I wnat to insert that field with owner name using Apex or Trigger. Can any one suggest me.

Thanks,
Sarma
Meghna Vijay 7Meghna Vijay 7
Hi Sarma,
 
trigger on case(before Insert) {
  if(Trigger.isBefore && Trigger.isInsert) {
    TriggerHandler.handleBeforeInsert(Trigger.new);
}
}

public class TriggerHandler {
    public static void handleBeforeInsert(List<Case> newCaseList) {
       Set<String> caseIdSet = new Set<String>();
        for(Case newCase : newCaseList) {
           caseIdSet.add(newCase.OwnerId);
}
// Create a map of String with Owner
// Loop over the newCaseList
and assign newCase.TextField = map.get(key).Name;

} 
}

But let me tell you it can be done via config using formula field of text type.

Hope it helps. If it does, close it by marking it as solved.

Thanks
manasa udupimanasa udupi

Hi Sarma,

You can do this using configuration itself without the need of code, below are some of the options:

1. Convert that text field to a formula field of return type text . and the formula should be-  Owner.FirstName +  Owner.LastName
2. If you don't want to change the field type then, create a workflow that runs when the record is created, and add action as field update on that field and populate formula as Owner.FirstName +  Owner.LastName
3. you can also create process builder to do the same

If all 3 doesn't meet the requirement then please provide more information on the object and fields, will help you create a trigger on that object
Hope this helps:)

Thanks,
Manasa