To set a field value in UI pages

Poorva Bhawsar
Mega Sage

Hi Community,

 

i have a ui page, using that i want to map the fields which i have on the dialog box and fields which i have in the time worked table. I am able to map all except the time worked field.

 

Here is my code.

function closeWindow() {

    GlideDialogWindow.get().destroy();
}

function action_changed() {
    var action = gel('actions').value;
    if (action == "incall" || action == "outcall") {
        document.getElementById("element.sys_choice.label").style["display"] = "inherit";

    } else {
        document.getElementById("element.sys_choice.label").style["display"] = "none";
        document.getElementById("element.sys_choice.label").value = "";
    }
}

function update_ticket() {

    var usr = g_user.userID;
    var action = gel('actions').value;
    var response = gel('call_response').value;
    var days = gel('ni.task_time_worked.time_workeddur_day').value;
    var hours = gel('ni.task_time_worked.time_workeddur_hour').value;
    var minutes = gel('ni.task_time_worked.time_workeddur_min').value;
    var seconds = gel('ni.task_time_worked.time_workeddur_sec').value;
    var comments = gel('task_time_worked.comments').value;
    var time_worked = calculateTimeWorked(days, hours, minutes, seconds);
    var tck = g_form.getUniqueValue();


    var gr = new GlideRecord('task_time_worked');
    gr.initialize();
    gr.setValue('user', usr);
    gr.setValue('action', action);
    gr.setValue('u_call_response', response);
    gr.setValue('time_worked', time_worked);
    gr.setValue('comments', comments);
    gr.setValue('task', tck);
    gr.insert();
    GlideDialogWindow.get().destroy();
}
function calculateTimeWorked(days, hours, minutes, seconds) {
    // Convert everything to seconds and sum up
    var totalSeconds = parseInt(days) * 86400 + parseInt(hours) * 3600 + parseInt(minutes) * 60 + parseInt(seconds);

    var ajax = new GlideAjax('OptusUtils');
    ajax.addParam('sysparm_name', 'getTimeWorked');
    ajax.addParam('sysparm_addtime', totalSeconds);
    ajax.getXML(doSomething);
    //ajax.getXMLAnswer(function(doSomething) {
   }
 function doSomething(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    time_worked = answer;
    //g_form.setValue('time_worked', answer);
    //totalSeconds=answer;
}
 
Where exactly i need to set the field value. I am getting the correct output in the alert but its not setting value in the time worked field on time worked table.
 
Thanks,
Poorva
4 REPLIES 4

Community Alums
Not applicable

Hi @Poorva Bhawsar ,

If you want to set it to some default value, map the sys id as follows:

ui_reference name="ref" table="sys_user" value="<give_sys_id_of_default_value_here>"/>

I dont want to set it to a default value. Whatever i will enter in the popup for time worked field, it should create a record in a table and auto fill time worked field with the value i entered on the popup.

Naveen Kumar4
ServiceNow Employee
ServiceNow Employee

Hi @Poorva Bhawsar ,

 

Can you share the output of alert?

 

Did you pull time_worked field on to the form?

 

Can you try executing g_form.setValue('time_worked','100000000'); in the developer tools or may be client script?

 

Thanks,

Naveen

Here is the screenshot of the alert. 

PoorvaBhawsar_0-1701247113886.png

I am getting the date, hours, minutes and seconds what i have entered on the popup.

g_form.setValue('time_worked','100000000'); i tried this but its not working.