Skip to main content

I need to add a button to a form that will populate a Approved By field with the Current User's Preferred Name, and a Approved On field with the Current Date.  For simplicity I made both Approved By and Approved On fields single line of text fields.

So far I've tried to add a Custom Javascript function, but two problems occur for me.  One is I can't figure out how to return the current users name, I can return a text value successfully like in the example below is about all.  Two is for the date, the current date returns a long line of numbers instead of the formatted date for some reason?  So this function:

function ApprovedFunction()

{

NWF$('#' + idApprovedBy).val("Current User Name here");

NWF$('#' + idApprovedOn).val(fn-FormatDate(Current Date, 'MM/dd/yyyy'));

}

**Returns these values:
Approved-By-Example-16-1.jpg

Does anyone have any ideas on how to make this work?  Thanks. 

You could use the default value setting within the SLT box and use the values Current User, and Current Date. Just note that the Current User value will be the claim, or login.


My problem is not really just displaying the current users preferredName or a formatted date, I could use calculated values to do that. 

Instead my problem is I only want these fields to be filled in when someone clicks on the Approve this request button.  This is for a multi-step form where we want one of the steps to be a manager going to the form and then clicking on the Approve this request button if everything looks right.

The problem is we have to use javascript as a work around because rules cannot populate values in fields.  And when using javascript, we can't use Named Control references nor can we use the userProfileLookup runtime function.  And even though we can use the FormatDate() runtime function, for some reason the Current Date reference displays as a decimal value?

I would even be fine with using a dropdown control instead of a button, it's just I can't figure out anyway to populate fields with the current users preferred name and the current date from any control inside of the form?


if you could replace button with Yes/No check box, you could easily make a formula giving you current user conditionally and do not need any javascript like

If(ManagerApproved,Current User, " ")

ManagerApproved is name of Yes/No checkbox


I can do the same thing with the button but I need the current user's preferred name, not their full claims based login name.

This will return the current person's claims based login name:

NWF$('#' + idApprovedBy).val(Current User);

But without the userProfileLookup runtime function I can't return the preferred name.  I also still can't figure out how to return a formatted date.  Any ideas on why the current date returns as a decimal value (example: .0000005734)?

At the end of the day, I think the problem is that we cannot reference Named Controls or Form variables when using JavaScript.  That really limits what we can do in the forms.


I can do the same thing with the button but I need the current user's preferred name,

I wanted to give you just hint, I thought you can work on this out ...

so, you can still use:    If(ManagerApproved,userProfileLookup(Current User,property_of_interest), " ")

I also still can't figure out how to return a formatted date

use the same formula as above, just replace profile lookup with:   formatDate(Current date, format string)

I think the problem is that we cannot reference Named Controls or Form variables when using JavaScript

of course you can not reference control name in java script.

for that purposes you have to create a variable for a control


I didn't get from your hint that you meant to put the provided formula on a calculated field, but that is a really good workaround which I will use on this form.  Thank you. 

On your last point, I created variable names for my controls.  Using the Custom JavaScript section I can populate field values using their variable name, but I can't seem to read values from fields on my form even when using their variable name.  But it's not really important now as I have a workable solution.


Reply