Populate GlideDateTime field with UI Action

Anderson_R
Tera Contributor

I have a UI Action that runs Onclick openCloseNotesPopup(). 
All aspects of the UI Action function, except for the work_end field being populated with the current date and time. How can I adjust the script so it populates that field?  Any help is greatly appreciated! 

function openCloseNotesPopup() {
// Check if the 'work_notes' field is not empty
var workNotes = g_form.getValue('work_notes');

if (workNotes && workNotes.trim() !== "") {
// Display a confirmation dialog
if (confirm('Are you sure you want to Close Complete Task?')) {
// User confirmed, proceed with Close Complete
var msg = workNotes;

g_form.setValue('state', '3');
g_form.setValue('work_notes', msg);

// Add current date and time to 'work_end' field
var currentDate = new GlideDateTime();
currentDate.setDisplayValue(gs.nowDateTime()); // Set the current date and time

g_form.setValue('work_end', currentDate);
// Save the record
g_form.submit();
} else {
// User canceled the confirmation, do nothing
return false;
}
} else {
// 'work_notes' is empty, proceed with cancellation
var reason = prompt('Enter a summary of the work completed:');

if (reason) {
var msgTwo = reason;

g_form.setValue('state', '3');
g_form.setValue('work_notes', msgTwo);

// Save the record
g_form.submit();
} else {
return false;
}
}
}

1 ACCEPTED SOLUTION

Hi Anderson, I'm glad that you were able to solve this!

 

As per your question, simply click at this code sample button:

 

MarcosKassak_0-1701892693999.png

 

Then select the language and you're good to go.

 

Cheers!

View solution in original post

3 REPLIES 3

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Anderson_R,

 

I had a great look at your code and would like to try another approach instead of your piece of red code.

 

function openCloseNotesPopup() {
    // Check if the 'work_notes' field is not empty
    var workNotes = g_form.getValue('work_notes');

    if (workNotes && workNotes.trim() !== "") {
        // Display a confirmation dialog
        if (confirm('Are you sure you want to Close Complete Task?')) {
            // User confirmed, proceed with Close Complete
            var msg = workNotes;

            g_form.setValue('state', '3');
            g_form.setValue('work_notes', msg);

            // Add current date and time to 'work_end' field
            var currentDate = new GlideDateTime();
            currentDate.setDisplayValue(gs.nowDateTime()); // Set the current date and time

            // Convert currentDate to a string representation
            var dateString = currentDate.getDisplayValue();

            g_form.setValue('work_end', dateString); // Set the 'work_end' field with the string date
            // Save the record
            g_form.submit();
        } else {
            // User canceled the confirmation, do nothing
            return false;
        }
    } else {
        // 'work_notes' is empty, proceed with cancellation
        var reason = prompt('Enter a summary of the work completed:');

        if (reason) {
            var msgTwo = reason;

            g_form.setValue('state', '3');
            g_form.setValue('work_notes', msgTwo);

            // Save the record
            g_form.submit();
        } else {
            return false;
        }
    }
}

 

This modification converts the currentDate variable to a string representation using getDisplayValue() and sets this string value to the 'work_end' field using setValue().

 

Now, the 'work_end' field should be populated with the current date and time when the function runs.

 

Let me know about the results!

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯

Hi @Marcos Kassak 


I greatly appreciate your efforts! Unfortunately, that edited script didn't seem to work. However, I've decided to create a BR to populate the work_end field instead of trying to cake it into the UI Action. 

This simple script works for the BR:

(function executeRule(current, previous /*null when async*/) {
var currentDate = new GlideDateTime();
current.work_end = currentDate;
})(current);

If you don't mind sharing, how do you get your script in that light brown square? It's much more readable that way. 

Hi Anderson, I'm glad that you were able to solve this!

 

As per your question, simply click at this code sample button:

 

MarcosKassak_0-1701892693999.png

 

Then select the language and you're good to go.

 

Cheers!