Page 1 of 1

Fill in current user

PostPosted: Wed Jun 05, 2019 6:48 pm
by mabe
Hi all,

I am trying to fill in the currently logged in username into a field when editing a form.
This is my JavaScript code:
Code: Select all
if(nuFormType() == 'edit') {
    var refA = #zzsys_user_id#;
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");


It works if i replace #zzsys_user_id# with a String, but I can't get the current username.

Do you have a solution to this?

Thanks very much!

Re: Fill in current user

PostPosted: Wed Jun 05, 2019 7:02 pm
by kev1n
Hi,

You can use nuUserName()
https://wiki.nubuilder.net/nubuilderfor ... nuUserName


Code: Select all
if(nuFormType() == 'edit') {
    var refA = nuUserName();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");



But since nuUserName() returns null if logged in as globeadmin, I use my custom function:

Code: Select all
function nuUserNameEx() {
    var u = nuUserName();
    if (u == null || u == '') {
        u = "globeadmin";
    }
    return nuUser;
}

if(nuFormType() == 'edit') {
    var refA = nuUserNameEx();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");

Re: Fill in current user

PostPosted: Wed Jun 05, 2019 7:20 pm
by mabe
kev1n wrote:Hi,

You can use nuUserName()
https://wiki.nubuilder.net/nubuilderfor ... nuUserName


Code: Select all
if(nuFormType() == 'edit') {
    var refA = nuUserName();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");



But since nuUserName() returns null if logged in as globeadmin, I use my custom function:

Code: Select all
function nuUserNameEx() {
    var u = nuUserName();
    if (u == null || u == '') {
        u = "globeadmin";
    }
    return nuUser;
}

if(nuFormType() == 'edit') {
    var refA = nuUserNameEx();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");


Nice!!
Thank you very much!
I had to use === for the comparison, but otherwise this works! :)

Re: Fill in current user

PostPosted: Fri Jun 07, 2019 5:33 pm
by admin
.