Santhana Rajhan
Mega Sage

Time Worked in Agent Workspace!!

Use this code below to add Time Worked to any of the UI Action in Agent Workspace. Any existing UI action can be modified to include this and run on click. Please note that this works on Client Side and if a Server Side script is being utilized, it has to be modified to work on Client Side to make this work. 

If a similar one was already exists in the community, kindly excuse.

Reference: There is an amazing article on Agent Workspace which could be used in multiple aspects. 

https://www.ashleysn.com/post/workspace-ui-actions

There are three aspects,

  1. UI Page - To enter the data
  2. Script Include - To Glide and add the Time Worked
  3. UI Action - To trigger the UI Page

1. UI Page:

Name: Provide a valid name Eg: ws_timer

Application: Global

HTML:

<link href="sn_customerservice.agentworkspace.css.jsdbx" rel="stylesheet" type="text/css" />
<g:ui_form onsubmit="invokePromptCallBack('ok')">
<table width="100%">
    <tr>
     	<td nowrap="true"><g:no_escape>${title}</g:no_escape></td>	
	</tr>
        <tr><td>
			<g:ui_input_field label="Hours"  name="hr" value="00" type="number" min="0" max="24"/> <g:ui_input_field label="Mins" name="min" value="00" type="number" min="0" max="60"/>
<g:ui_input_field label="Seconds" name="sec" value="00" type="number" min="0" max="60"/>		</td>
			<td/></tr>
        <tr>
            <td colspan="2" align="right">
                <g:dialog_buttons_ok_cancel 
                   ok="invokePromptCallBack('ok');" 
                   ok_type="button" />
            </td>
        </tr>	
</table>
</g:ui_form>

Client script:

function invokePromptCallBack() {

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return '';
    }

    var case_sysid = getQueryVariable('case');
    var user = getQueryVariable('user');

    if (gel('min').value < 60 && gel('sec').value < 60 && user && case_sysid && gel('min').value >= 0 && gel('sec').value >= 0 && gel('hr').value >= 0 && (gel('min').value > 0 || gel('hr').value > 0)) {
        var ga = new GlideAjax('Workspace_Ajax'); //Script Include
        ga.addParam('sysparm_name', 'updateTimer');
        ga.addParam('sysparm_u_case', case_sysid);
        ga.addParam('sysparm_u_user', user);
        ga.addParam('sysparm_u_hr', gel('hr').value);
        ga.addParam('sysparm_u_min', gel('min').value);
        ga.addParam('sysparm_u_sec', gel('sec').value);
        ga.getXML(Parse);

    }

    function Parse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        window.location.href = '';
    }
}

2. Script Include:

Name: Workspace_Ajax (As mentioned in the Client Script of the UI Page)

Application: Global

Accessible from: All Application Scopes

Client Callable: True

Script:

var Workspace_Ajax = Class.create();
Workspace_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    updateTimer: function() {
        var timer = new GlideRecord('task_time_worked');
        timer.initialize();
        timer.setValue('task', this.getParameter('sysparm_u_case'));
        timer.setValue('user', this.getParameter('sysparm_u_user'));
        timer.setValue('time_in_seconds', this.getParameter('sysparm_u_sec'));
        timer.setValue('time_worked', new GlideDuration(this.getParameter('sysparm_u_hr') + ':' + this.getParameter('sysparm_u_min')));
        timer.insert();
    },

    type: 'Workspace_Ajax'
});

3. UI Action:

Name: Save

Application: CSM Workspace (Set based on requirement)

Order: -100

Action name: sysverb_ws_save

Show Insert / Show Update: True (Set based on requirement)

Client: True

List v2 Compatible: True

Condition: !current.isNewRecord() && current.canWrite() (Set based on requirement)

Workspace Form Button / Workspace Form Menu: True (Set based on requirement)

Workspace Client Script:

function onClick(g_form) {
    g_modal.showFrame({
        title: getMessage("Timer"),
        url: 'ui_page.do?sys_id=sys_id&case=' + g_form.getUniqueValue() + '&user=' + g_user.userID, //Mention SYS ID of the UI Page in sys_id=
        size: 'sm',
        autoCloseOn: 'URL_CHANGED',
        callback: function(ret, data) {
            console.log(data);
            console.log(ret);
            g_form.save();
        }
    });
}
Comments
Derek17
Tera Contributor

Hey Santhana,

Thanks for this! I implemented it on my instance and it works as expected!

However, I do have one question. When a user goes to click 'Cancel' or the close dialog button at the top right of the UI Page it still saves the record.

Do you know how to update this to cancel the save functionality when either of those buttons are clicked? I only want the record to save then 'OK' is clicked after entering in time.

Thanks,

alona3
Tera Contributor

Can you show me how it looks like?

Derek17
Tera Contributor

Hey Alona,

This is what the pop-up looks like in our instance.

find_real_file.png

alona3
Tera Contributor

Hi Derek,

 

I followed the instruction above but it doesn't work on my end.

In UI Action, I chose Task Table.

find_real_file.png

It looks like below:

 

find_real_file.png

 

 

In addition, via Workspace Client Script, there's an error.

 

find_real_file.png

 

grzegorzch
Kilo Expert

You need to provide the sys_id of the ui page you created before.

find_real_file.png


grzegorzch
Kilo Expert

Great article! Thanks!

alona3
Tera Contributor

Thank you so much!!!

Hanook
Tera Contributor

can you show me the output

Version history
Last update:
‎03-08-2021 11:28 AM
Updated by: