Nintex form button setting field value


Badge +2

I am very new to Nintex forms, looking to obtain the simple 'step by step' Button JS properties for setting username (userprofile) info to a text field.  Any and all info is appreciated. Thanks


10 replies

Badge +16

Can you explain a bit more about what you want to do?  User fills in some details then presses a button and some userprofile data populates?

Any screenshots or further explanation would be a help.

Badge +2

Hi Cassy, simply put, a user fills information on the form, then I would like the user / approver to hit a button to populate his username (as e-signature) in a text field and a date in an additional text field, then hide or disable the button.  Can you help...thanks

Userlevel 4
Badge +7

Hi Tim Roy

Add a single text field to your form and give it a client id JavaScript variable name of varUser.

Add a button to your form and give the Client click a value of GetUser()

Now in the form settings under custom JavaScript, paste the following code

function GetUser(){
    //get current user id
    var userid= _spPageContextInfo.userId;

    //query the user by id to get all properties
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
  var requestHeaders = { "accept" : "application/json;odata=verbose" };
  $.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
  });

  function onSuccess(data, request){
      //get the user title property
    var loginName = data.d.Title;

    //set the field value
    NWF$(varUser).val(loginName);
  }

  function onError(error) {
    alert("error");
  }

}

This will give the following result:

If you get any problems let me know

Badge +2

Hi Paul, thanks for the quick response.  I tried the settings as specified but the username / ID is not showing up in the text box.  I am wondering if there are other settings on the text box / button or JS code that need to be edited / changed for my specific form...for example, does the JS code need to reference the varUser / loginName applicable to my form.  I copied to JS code as is with no editing.

//set the field value
    NWF$(varUser).val(loginName);

I am new to Nintex and any help is appreciated.

Thanks

Userlevel 4
Badge +7

Hi Tim Roy

Did you set the client id JavaScript variable name on the text box and the client click setting on the button as the screenshots above?

varUser is set as the single line of text field client id JavaScript variable name.

Let me know how you get on

Badge +2

Hi Paul, here are screenshots of my text box and button properties.

text box and button settings

Here is my Nintex form (using sharepoint list)...I am using the 'Title' text box for adding username.

Thanks

Userlevel 4
Badge +7

Hi

Very odd, that is exactly how I set my form up. Can you post a screen shot of the custom javascript section of form settings? just to double check its right

Thanks

Badge +2

Hi Paul, thanks...here is a screen shot of the form settings...I copied and pasted exactly as submitted.  Would you be able to export / send the form to me?

Thanks

Userlevel 4
Badge +7

Hi Tim Roy

I am so sorry!! I'm an idiot, I sent you an old version I used before updating it to make it work!!

Use this version of the code and you should be fine:

function GetUser(){
    //get current user id
    var userid= _spPageContextInfo.userId;

    //query the user by id to get all properties
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
  var requestHeaders = { "accept" : "application/json;odata=verbose" };
  NWF$.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
  });

  function onSuccess(data, request){
      //get the user title property
    var loginName = data.d.Title;

    //set the field value
    NWF$('#'+varUser).val(loginName);
  }

  function onError(error) {
    alert("error");
  }

}

Note this bit was wrong:

    //set the field value
    NWF$(varUser).val(loginName);

    //should be

    //set the field value
    NWF$('#'+varUser).val(loginName);

Again apologies and I hope it works this time!!! 

Badge +2

Hi Paul, it works now...thanks very much.

Reply