Client script containing jquery working in nativeUI but not in workspace

aanaveni
Tera Contributor

I trying to set default date time picker value while form loading through client script, it is working in native but not in workspace, I have tried all suggestion which were suggested online like setting isolate script to true or using noconflict but none worked. 

this is the code:

function onLoad() {
    var fields = ['table_name.field_name']; //on purpose
 
    try {
        jQuery('input[data-type="glide_element_date_time"]')
            .each(function (index, value) {
                var ref = jQuery(this).attr('data-ref');
                if (ref.length) {
                    jslog('JM: found element at index: ' + index + ' value: ' + ref);
                    var val = g_form.getValue(ref);
                    if (val == '' && fields.indexOf(ref) > -1) {
                        jslog('JM: registering event for element at index: ' + index + ' value: ' + ref);
                        jQuery(this).next().on('click', function () {
                            setTimeout(controlDateTimeSelector, 500);
                        });
                    }
                }
            });
 
    } catch (e) {
        console.error('JM: Error occurred: ' + e.toString() + ' on line:' + e.lineNumber);
    }
 
    function controlDateTimeSelector(iterator) {
        jslog('JM: Starting controlDateTimeSelector.');
        var prefixId = '#GwtDateTimePicker';
        try {
            if (iterator === undefined) {
                iterator = 0;
 
            } else if (iterator > 5) {
                jslog('JM: Stopping; hit max iteration count.');
                return
            }
 
            if (!jQuery(prefixId + '_header').length) {
                jslog("JM: Didn't find DatePicker, trying again in 500ms. Iterator was: " + iterator);
                iterator++;
                setTimeout(controlDateTimeSelector.bind(null, iterator), 500);
 
            } else {
                jslog('JM: Found and Set DatePicker.');
g_form.setValue('_hh', '12);')
                /*Query(prefixId + '_hh').val('12');
                jQuery(prefixId + '_mm').val('00');
                jQuery(prefixId + '_ss').val('00');
                jQuery(prefixId + '_ampm').val('am');*/
            }
        } catch (e) {
            console.error('JM: Error occurred: ' + e.toString() + ' on line:' + e.lineNumber);
        }
    }
 
}
 
4 REPLIES 4

Shubham Singh
Mega Guru

Hi @aanaveni 

 

JellyScript will not work in Workspace.

Native UI supports JellyScript but Workspace doesn’t. 

Find some alternate method/way to get your requirement fulfilled on Workspace.

 

Thanks!

 

Please mark this response as correct and helpful ✔️👍

I have searched almost every source, only way to make this happen sadly is using jelly script.

Amit Gujarathi
Giga Sage
Giga Sage

HI @aanaveni ,
I trust you are doing great.
The issue you're encountering with setting the default date and time picker value in the ServiceNow workspace, despite it working in the native interface, is likely due to the differences in how the workspace and the native interface handle scripts. As Shubham Singh pointed out, JellyScript, which is typically used in the native UI of ServiceNow, doesn't work in the workspace environment.

Instead of relying on direct DOM manipulation with jQuery, consider using ServiceNow's built-in client script functionalities. You can use the g_form API to set field values and control form behaviors.


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello Amit,

 

I tried using following code using g_form,

function setDatePickerTime(field) {
//Attach an event listener to set default time value for date picker
Event.observe($(g_form.getControl(field).id).next('a'), 'click', function() {
setTimeout(\"$('GwtDateTimePicker_hh').value = '00';$('GwtDateTimePicker_mm').value = '00';$('GwtDateTimePicker_ss').value = '00';\",0);
});
}, but it is not accepting $.