Auto populate the user field with Logged in user

TA33
Tera Contributor

Whenever I login in with my user Id the preparer name should be auto populate with my login details.

TA33_0-1687268829516.png

I have wrote the client script onLoad condition but its not working

function onLoad() {
        var currentUser = g_user.userID;
if (g_form.isNewRecord()) {
       g_form.setvalue(currentUser,'preparer');
    }
}

 

2 ACCEPTED SOLUTIONS

Kiranmai_KVL
Kilo Sage

Hello,

Its not best practice to use client scripts as they just display value on the form while loading but value is not saved in the database unless user clicks on save or update.

Instead of client script, right click on the 'Preparer' field & configure dictionary & go to default value tab.

Click on dynamic default value & select 'Me'

OR  if you can also write this line in default value field 'javascript:gs.getUserID()' 

 

Kiranmai_KVL_0-1687329468320.png

 

View solution in original post

Riya Verma
Kilo Sage
Kilo Sage

Hi @TA33 ,

 

Hope you are doing great.

Firstly , if you want to autopopulate the preparer field as logged in user , then ideally best way is to set default value of field by clicking right click on preparer field and clicking on configure dictionary. There in default value section you can define your default value for field. This is best approach for doing your requirement.

 

But still if you want to accomplish it by your way by vreating client script:

function onLoad() {
    var currentUser = g_user.userID;
    if (g_form.isNewRecord()) {
        g_form.setValue('preparer', currentUser);
    }
}

 

But this value will be in display only in form, it wont be save in database. If you save the form the value will be saved else it will be lost.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

3 REPLIES 3

Kiranmai_KVL
Kilo Sage

Hello,

Its not best practice to use client scripts as they just display value on the form while loading but value is not saved in the database unless user clicks on save or update.

Instead of client script, right click on the 'Preparer' field & configure dictionary & go to default value tab.

Click on dynamic default value & select 'Me'

OR  if you can also write this line in default value field 'javascript:gs.getUserID()' 

 

Kiranmai_KVL_0-1687329468320.png

 

I want to get logged user Email and Manager same like Userid

Riya Verma
Kilo Sage
Kilo Sage

Hi @TA33 ,

 

Hope you are doing great.

Firstly , if you want to autopopulate the preparer field as logged in user , then ideally best way is to set default value of field by clicking right click on preparer field and clicking on configure dictionary. There in default value section you can define your default value for field. This is best approach for doing your requirement.

 

But still if you want to accomplish it by your way by vreating client script:

function onLoad() {
    var currentUser = g_user.userID;
    if (g_form.isNewRecord()) {
        g_form.setValue('preparer', currentUser);
    }
}

 

But this value will be in display only in form, it wont be save in database. If you save the form the value will be saved else it will be lost.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma