Set field value if the related list has the current record

ss123
Tera Contributor

Hi team,

 

Would like to know how do we script/check whether the related list of the current record is not empty. See below screen shots for reference.

 

In the Private Task [vtb_task] form, the related list is "Visual Task Board Cards"

SabrinaSalazar_0-1675397477085.png

 

When we check the list of the  "Visual Task Board Cards" [vtb_card] , you can see the PTASK record is in the "Task" field of "Visual Task Board Cards" [vtb_card]

 

SabrinaSalazar_1-1675397696613.png

 

The requirement is when the current PTASK is in the "Visual Task Board Cards" [vtb_card], the "Service Task" field in PTASK form will be set to "No"

 

SabrinaSalazar_2-1675397817064.png

 

Tried creating a business rule and client script but it seems to be NOT working.

 

Business Rule:

Table: vtb_card

 

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

// Add your code here
var vtask = new GlideRecord('vtb_task');
vtask.addQuery('sys_id', current.getUniqueValue());
vtask.query();
if (vtask.hasNext())
g_scratchpad.isParent = true;
else
g_scratchpad.isParent = false;
})(current, previous);

 

Client script:

Table: vtb_task

 

function onLoad() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.isParent) {
g_form.setValue('x_bmk_technology_s_u_service_task_1', 'no');

} else {
g_form.setValue('x_bmk_technology_s_u_service_task_1', 'yes');
}
}

 

14 REPLIES 14

Brad Bowman
Kilo Patron
Kilo Patron

I re-created this in my PDI by adding a field to my vtb_task table.  This table is in the Global scope, so when I added the field, the name was just u_service_task_1, not prefixed with my custom scope ID.  I made my field a Choice type with the choices yes and no.  Your Business Rule / Client Script combination doesn't make sense in that the (display, I assume) Business Rule is running on the card table, but the Client Script is onLoad of the task table - so which record are you displaying in hopes of updating the custom field?  I'll assume you want this to happen when the Private Task record is displayed, so the script for the display Business Rule on the vtb_task table would look like this:

 

(function executeRule(current, previous /*null when async*/ ) {
	var vcard = new GlideRecord('vtb_card');
	vcard.addQuery('task', current.getUniqueValue());
	vcard.query();
	if (vcard.next()) {
		g_scratchpad.isParent = true;
	} else {
		g_scratchpad.isParent = false;
	}
})(current, previous);

 

then your onLoad Client Script is fine, depending on the custom field name - my setValue lines are just u_service_task_1.

  

Hi @Brad Bowman  thanks for this. The logic to make the Service Task field = Yes is working, but it doesn't make the field change to No.

 

There are a couple of things you can do to trouble-shoot this.  The first is to temporarily add some logging lines to the Business Rule to confirm if/when it is running and the result of the GlideRecord:

(function executeRule(current, previous /*null when async*/ ) {
	var vcard = new GlideRecord('vtb_card');
	vcard.addQuery('task', current.getUniqueValue());
	vcard.query();
	if (vcard.next()) {
		g_scratchpad.isParent = true;
		gs.addInfoMessage('true')
	} else {
		g_scratchpad.isParent = false;
		gs.addInfoMessage('false')
	}
})(current, previous);

Once you confirm that viewing a Private Task Record in each test case gives the correct result from the Business Rule, check the value in the Client Script vs. the field value.  In my case I created a Choice type field with the values 'yes' and 'no', but maybe yours is 'No' or 'no ' with an errant space at the end?

@Brad Bowman  

 

Thanks but Not sure where I went wrong. Here's my updated BR and CS. The "No" still not working tho. Hope you can guide me on this? Thank you!!

 

SabrinaSalazar_0-1675733640262.png

 

 

SabrinaSalazar_1-1675733674039.pngSabrinaSalazar_2-1675733695079.png

 

Ah, so the Business Rule When to run needs to be 'display', not 'before'.