The CreatorCon Call for Content is officially open! Get started here.

Auto populate a multi line text type variable based on a choice type field selection

Michael94
Tera Contributor

I have a custom choice-type field called "Ref" on the HR table. Additionally, there's a variable editor on the HR form containing several variables. My objective is that when a choice is selected from the "Ref" field and the form is saved or updated, a specific text should be populated in a multiline-type variable called "RefVal" within the variable editor.

 

I've attempted to implement this writing a before-update business rule as outlined below, but I haven't achieved success yet. 

 

(function executeRule(current, previous /*null when async*/) {
	// Store the texts in system properties
	var refvaltext1 = gs.getProperty('sn_hr_core.refval1text');
	var refvaltext2 = gs.getProperty('sn_hr_core.refval2text');
	var refvaltext3 = gs.getProperty('sn_hr_core.refval3text');
// 1st scenario: if user select value 'A' from choice field u_ref, then auto-populate text 'refvaltext1' in the 'refval' variable
        if (u_ref === 'A'){
	var gr_question = new GlideRecord('question_answer');
        gr_question.addQuery('table_sys_id', current.sys_id);
        gr_question.query();
        if(gr_question.next()){
	current.variables.refval = refvaltext1;
	gr_question.update();}
       }
// 2nd scenario: if user select value 'B' from choice field u_ref, then auto-populate text 'refvaltext2' in the 'refval' variable	
        if (u_ref === 'B'){
	var gr_question = new GlideRecord('question_answer');
        gr_question.addQuery('table_sys_id', current.sys_id);
        gr_question.query();
        if(gr_question.next()){
	current.variables.refval = refvaltext2;
	gr_question.update();}
        }
 // 3rd scenario: if user select value 'C' from choice field u_ref, then auto-populate text 'refvaltext3' in the 'refval' variable	
        if (u_ref === 'C'){
	var gr_question = new GlideRecord('question_answer');
        gr_question.addQuery('table_sys_id', current.sys_id);
        gr_question.query();
        if(gr_question.next()){
	current.variables.refval = refvaltext3;
	gr_question.update();}
}	
})(current, previous);

 

Am I overlooking something, or should I consider alternative methods such as using flows, or client scripts, script includes?

 

1 ACCEPTED SOLUTION

Iraj Shaikh
Mega Sage

Hi @Michael94 

 

It looks like there are a few issues with the business rule script you've provided. Here are some points to consider and correct:

1. Reference to the Field: When you reference a field in a business rule, you should use `current` to get the value of the field on the current record. For example, `current.u_ref` instead of `u_ref`.

2. Updating Variable Values: To set the value of a variable in a variable editor, you don't need to perform a GlideRecord query on the `question_answer` table. Instead, you can directly set the value of the variable using the `current.variables` object.

3. Updating the Record: You should not call `update()` on the `current` record inside a before business rule because it can lead to a recursive loop. The `current` record will automatically be saved after the business rule finishes executing.

Here's a revised version of your business rule:

 

(function executeRule(current, previous /*null when async*/) {
    // Store the texts in system properties
    var refvaltext1 = gs.getProperty('sn_hr_core.refval1text');
    var refvaltext2 = gs.getProperty('sn_hr_core.refval2text');
    var refvaltext3 = gs.getProperty('sn_hr_core.refval3text');

    // 1st scenario: if user selects value 'A' from choice field u_ref, then auto-populate text 'refvaltext1' in the 'refval' variable
    if (current.u_ref == 'A') {
        current.variables.refval = refvaltext1;
    }
    // 2nd scenario: if user selects value 'B' from choice field u_ref, then auto-populate text 'refvaltext2' in the 'refval' variable
    else if (current.u_ref == 'B') {
        current.variables.refval = refvaltext2;
    }
    // 3rd scenario: if user selects value 'C' from choice field u_ref, then auto-populate text 'refvaltext3' in the 'refval' variable
    else if (current.u_ref == 'C') {
        current.variables.refval = refvaltext3;
    }
})(current, previous);

 


Please note the following:

- I've used `current.u_ref` to reference the custom choice-type field.
- I've used `current.variables.refval` to directly set the value of the variable.
- I've removed the unnecessary GlideRecord queries and updates.
- I've used `else if` to ensure that only one block of code runs for a given value of `u_ref`.

Before implementing this script, make sure that:

- The field names (`u_ref` and `refval`) are correct and match the actual field names in your instance.
- The system properties (`sn_hr_core.refval1text`, `sn_hr_core.refval2text`, `sn_hr_core.refval3text`) are correctly set up and contain the desired text values.
- The business rule is set to run "before" the update so that it can modify the `current` record before it is saved.

If you still encounter issues after making these changes, you may want to consider using a client script for client-side changes or a flow if you need more complex logic that involves multiple steps or conditions. However, for the described use case, a business rule should suffice.

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

1 REPLY 1

Iraj Shaikh
Mega Sage

Hi @Michael94 

 

It looks like there are a few issues with the business rule script you've provided. Here are some points to consider and correct:

1. Reference to the Field: When you reference a field in a business rule, you should use `current` to get the value of the field on the current record. For example, `current.u_ref` instead of `u_ref`.

2. Updating Variable Values: To set the value of a variable in a variable editor, you don't need to perform a GlideRecord query on the `question_answer` table. Instead, you can directly set the value of the variable using the `current.variables` object.

3. Updating the Record: You should not call `update()` on the `current` record inside a before business rule because it can lead to a recursive loop. The `current` record will automatically be saved after the business rule finishes executing.

Here's a revised version of your business rule:

 

(function executeRule(current, previous /*null when async*/) {
    // Store the texts in system properties
    var refvaltext1 = gs.getProperty('sn_hr_core.refval1text');
    var refvaltext2 = gs.getProperty('sn_hr_core.refval2text');
    var refvaltext3 = gs.getProperty('sn_hr_core.refval3text');

    // 1st scenario: if user selects value 'A' from choice field u_ref, then auto-populate text 'refvaltext1' in the 'refval' variable
    if (current.u_ref == 'A') {
        current.variables.refval = refvaltext1;
    }
    // 2nd scenario: if user selects value 'B' from choice field u_ref, then auto-populate text 'refvaltext2' in the 'refval' variable
    else if (current.u_ref == 'B') {
        current.variables.refval = refvaltext2;
    }
    // 3rd scenario: if user selects value 'C' from choice field u_ref, then auto-populate text 'refvaltext3' in the 'refval' variable
    else if (current.u_ref == 'C') {
        current.variables.refval = refvaltext3;
    }
})(current, previous);

 


Please note the following:

- I've used `current.u_ref` to reference the custom choice-type field.
- I've used `current.variables.refval` to directly set the value of the variable.
- I've removed the unnecessary GlideRecord queries and updates.
- I've used `else if` to ensure that only one block of code runs for a given value of `u_ref`.

Before implementing this script, make sure that:

- The field names (`u_ref` and `refval`) are correct and match the actual field names in your instance.
- The system properties (`sn_hr_core.refval1text`, `sn_hr_core.refval2text`, `sn_hr_core.refval3text`) are correctly set up and contain the desired text values.
- The business rule is set to run "before" the update so that it can modify the `current` record before it is saved.

If you still encounter issues after making these changes, you may want to consider using a client script for client-side changes or a flow if you need more complex logic that involves multiple steps or conditions. However, for the described use case, a business rule should suffice.

Please mark this response as correct or helpful if it assisted you with your question.