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
Sarah ChoeSarah Choe 

Auto populate a Standard contact name lookup field with current user name

Hi all,

I am looking for a way to auto(pre) populate a Contact name as the curent user name when he/she create a new case.
This can be internal user creating a case and community user creating a case.
below is my take but when i test with clicking 'new' case, I still see 'Contact Name' blank and need to be searched. 

can anyone please help...?

trigger CaseContactName on Case (before insert) {

   // List <Case> caseContactName = new List <Case>();
    for (Case c : trigger.new){
    if(c.ContactId == null){
      c.ContactId = UserInfo.getUserId(); 
    }
    }
}
Best Answer chosen by Sarah Choe
Gaurish Gopal GoelGaurish Gopal Goel
Hi Sarah,

Your trigger code is fine but for this requirement it won't work because trigger works in the backend. You need a Visualforce page and override the NEW case button. Or you can use create a custom button and put some onclick javascript and set the value of contact.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

JS CODE:
var uname = '{!$User.FirstName} {!$User.LastName}';
window.open('/500/e?cas3='+uname, target="_self");

See this screenshot, it will help you to create the custom button:
User-added image

Thanks  & Regards,
Gaurish

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Hi Sarah,

Your trigger code is fine but for this requirement it won't work because trigger works in the backend. You need a Visualforce page and override the NEW case button. Or you can use create a custom button and put some onclick javascript and set the value of contact.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

JS CODE:
var uname = '{!$User.FirstName} {!$User.LastName}';
window.open('/500/e?cas3='+uname, target="_self");

See this screenshot, it will help you to create the custom button:
User-added image

Thanks  & Regards,
Gaurish
This was selected as the best answer
Sarah ChoeSarah Choe
Hi Gaurish,thanks a lot for the help.  can you pleae also give some guidance how to override the 'NEW' case button?
many thanks!
Sarah
Gaurish Gopal GoelGaurish Gopal Goel

Hi Sarah, We can't use JavaScript on the existing standard NEW button. We will have to create a custom button if we want to use JavaScript. We can override the existing NEW button with Visualforce page.

Steps to override the existing the NEW button with a VF Page:
Go to Setup --> Type "Case" in Quick find in the left sidebar --> Click "Buttons, Links, and Actions" --> Click "Edit" in front of the New button --> Select Visualforce radio button for Salesforce Classic and Lightning Component for Lightning experience.

See screenshots for reference:
User-added image


User-added image


User-added image

Sarah ChoeSarah Choe
Thanks Gaurish let me try out!