Sandeep Rajput
Tera Patron
Tera Patron

Recently, I came across an interesting question where the questioner wanted to fetch the details of a Subject person on an HR Task of type Submit catalog item.

 

Problem Statement: Fetching the details of a subject person on an HR Task of type Submit Catalog Item.

Solution: Unfortunately, there is no direct way to fetch the sys_id of subject person on HR Task, if we closely look at the page source on my Todos page, there is no way we can get details like parent HR Case or any subject person related information. Even there is no direct way to know about the sys_id of the current HR task.

 

After going through the page source, I spotted an HTML div where the task's sys_id is available in an abstract manner.

Screenshot 2023-10-03 at 7.24.10 PM.png

 

Searched on the backend using this sys_id and found the corresponding HR task.

Screenshot 2023-10-03 at 7.24.45 PM.png

Since HR task has a parent HR Case, hence subject person related details can be fetched easily from it.

 

Now the question of pulling this abstract hr task sys_id in a client script came. Here, I simply applied some javascript to fetch the intended information.

 

Screenshot 2023-10-03 at 7.30.23 PM.png

Using this info: Simply used added a reference variable HR Task on the Record producer.

 

Screenshot 2023-10-03 at 7.33.35 PM.png

Screenshot 2023-10-03 at 7.33.42 PM.png

Used the following Onload script to populate my HR Task reference field.

Screenshot 2023-10-03 at 7.34.37 PM.png

Here is the script for the same.

 

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   //Following line fetches the sys_id of the task shown in To-Dos
   var hrTaskSysID = this.document.getElementsByClassName('task-content')[1].id.split('_')[0];
   g_form.setValue('hr_task',hrTaskSysID);   
}

 

 

Added another variables to get details related to the subject person.

Screenshot 2023-10-03 at 7.40.21 PM.png

Screenshot 2023-10-03 at 7.40.25 PM.png

Added couple of more single line text variables for representing First name and last name

 

Here is the client script I used to populate these variables.

Screenshot 2023-10-03 at 7.42.05 PM.png

Here is the client script.

Screenshot 2023-10-03 at 7.42.05 PM.png

Here is the source of client script.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var myUtils = new GlideAjax('HRUtils');
    myUtils.addParam('sysparm_name', 'getSubjectPersonForHRTask');
    myUtils.addParam('sysparm_hr_sys_id', g_form.getValue('hr_task'));
    myUtils.getXML(subjectPersonResponse);

    function subjectPersonResponse(response) {		
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var subjectPerson = JSON.parse(answer);		
        g_form.setValue('subject_person', subjectPerson['subject_person_sys_id']);
		g_form.setValue('first_name', subjectPerson['subject_person_first_name']);
		g_form.setValue('last_name', subjectPerson['subject_person_last_name']);		
    }

}

 

 

 

Now comes the script include.

 

 

var HRUtils = Class.create();
HRUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getSubjectPersonForHRTask: function() {

        var hrTaskSys_id = this.getParameter('sysparm_hr_sys_id');
        var glideHRTask = new GlideRecord('sn_hr_core_task');
        var subjectPerson = {};
        if (glideHRTask.get(hrTaskSys_id)) {

            subjectPerson['subject_person_sys_id'] = glideHRTask.parent.subject_person.getValue('sys_id');
            var glideUser = new GlideRecord('sys_user');
            if (glideUser.get(glideHRTask.parent.subject_person.getValue('sys_id'))) {
                subjectPerson['subject_person_first_name'] = glideUser.getValue('first_name');
                subjectPerson['subject_person_last_name'] = glideUser.getValue('last_name');
            }

        }
        return JSON.stringify(subjectPerson);
    },

    type: 'HRUtils'
});

 

 

 

Here is the end result. Here all the details have been fetched dynamically using the solution shared above.

Screenshot 2023-10-03 at 7.44.58 PM.png

 

Many would argue that we should not rely on the HTML task-content class as it may change in the future versions but this is the risk I am willing to take as long as it gets me the desired results.

 

Hope this helps.

Comments
G24
Kilo Sage

Would this apply only if the Fulfillment type of your HR Service is set to "Service Activity", as opposed to "Flow"?

Sandeep Rajput
Tera Patron
Tera Patron

@G24 This will apply for Fulfilment type Service Activity as well.

Version history
Last update:
‎10-03-2023 07:36 AM
Updated by:
Contributors