Client Script to Fill in Seat Location from Spaces Table

jlaps
Kilo Sage

I am having trouble with something that seems like it should be easy. On a user record for sys_user, I created a new reference field for SEAT ASSIGNED. When the user record is opened, I want it to fill in the SEAT ASSIGNED with the value of SPACE assigned to that user in the SPACES table. The related list tab is doing this for us already, but I want it in a field so the preview/popup view will show this field-location when that user is referenced.

 

I had thought that a simple ONLOAD client script would do it, but my searching is showing that I might need a script include and glideajax?

 

What is the best way to do this?

jlaps_0-1713293524238.png

 

Below is code I have revamped a hundred times, so I am positive its not working... I cant even get the alert to pop.

function onLoad() {

		uservalue = g_form.getValue("user_name");
		user = g_form.getReference("user_name");

       var seat = new GlideRecord("sn_wsd_core_space");
	   seat.addQuery('u_assigned_to', user);
	   seat.query();
alert('popup: ' +user);
		while (seat.next()){
			var seatspace = seat.sys_id;
			g_form.setValue("u_space", seatspace);
		}

   }

 

1 ACCEPTED SOLUTION

Hi @jlaps ,

 

First of all, you should make that field a referenced field and it should reference the 'sn_wsd_core-space' table. Because if you are using that field as a string field, you will not be able to preview that record and the reference icon will not be visible for string field . See below examples 

 

String Field With no reference icon

 

AstikThombare_0-1713421142909.png

Reference Field with reference icon 

AstikThombare_1-1713421228106.png

 

Now lets move towards your script . I have made some changes to it please refer below script

 

 

(function calculatedFieldValue(current) {
 
        var spc = new GlideRecord('sn_wsd_core_space');
        spc.addQuery('u_assigned_to', current.sys_id);
        spc.setLimit(1);
        spc.query();
        if spc.next()) {
 
            return spc.sys_id.toString();
 
        } else {
 
            return '';
        }
 
 
})(current);

 

 

One point to remember here is that using a calculated value makes the field read-only. By default, you cannot preview a reference field if it's read-only. To address this, we need to set one property to true: 'glide.ui.reference.readonly.clickthrough'.

AstikThombare_3-1713421717502.png

 

Here's a scenario I tried in my PDI:

 

Use Case:The sys_user table has a related list named "Incident Task." To reflect this, I created a field named "Incident Task" in the sys_user table that references the incident_task table. When a task is assigned to any user, it is displayed in both the "Incident Task" related list and the "Incident Task" field (as shown in the screenshot below)

 

 

AstikThombare_4-1713422204799.png

 

Script for calculate value Which I used -

 

(function calculatedFieldValue(current) {
 
        var incTask = new GlideRecord('incident_task');
        incTask.addQuery('assigned_to', current.sys_id);
        incTask.setLimit(1);
        incTask.query();
        if (incTask.next()) {
 
            return incTask.sys_id.toString();
 
        } else {
 
            return '';
        }
 
 
})(current);

 

 

Outcome of it -

 

AstikThombare_5-1713422330308.png

 

I'm not able to preview record because i have not set property (glide.ui.reference.readonly.clickthrough ) to true .now after setting property to true -

 

AstikThombare_6-1713422515163.png

 

The same can be Achieve with the help of display business rule and onload Client Script 

 

Display Business rule -

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var incTask = new GlideRecord('incident_task');
    incTask.addQuery('assigned_to', current.sys_id);
    incTask.setLimit(1);
    incTask.query();

    if (incTask.next()) {
        g_scratchpad.incidentTaskSysId = incTask.sys_id.toString();
    } else {
        g_scratchpad.incidentTaskSysId = ' ';
    }


})(current, previous);

 

 

Onload Client Script -

 

 

function onLoad() {
    g_form.setValue('u_incident_task', g_scratchpad.incidentTaskSysId);
    g_form.setDisabled('u_incident_task', true);
}

 

 

I spent a lot of time figuring it out. If you could mark it as helpful and accept it as a solution, I'd really appreciate it

 

Thanks,
Astik

 

 

View solution in original post

5 REPLIES 5

This is excellent. The second part on the clickthrough reference is deeper than I need it actually. I dont need to be able to preview the seat/space, but for example when Deskside Support gets a ticket to setup a laptop for suchandsuch person shown as the REQUESTED FOR on their ticket, I want them to be able to click the preview on REQUESTED FOR to see the location and seat... what you have is perfect. Thank you for your efforts!