Auto populate field

HemaV
Tera Contributor

Hello, 

I am currently working on an "issue management" I need "Scripting" for the business rule for the below User story:

 

When the "Assigned to" field is filled, the "issue manager" field should be auto-populated.

 

I've included for you below the Screenshot for reference. 

HemaV_0-1729014015949.png

 

4 REPLIES 4

Astik Thombare
Tera Sage

Hi @HemaV ,

 

We can achieve mentioned scenario via a onchange client script instead of Business rule . Please Create Onchange Client Script on Assigned to Field . Please check below Screenshot and Script

 

I hope you want to Populate Issue Manager with Selected Assigned to Manager

 

AstikThombare_0-1729015728050.png

 

Script - 

 

 

/**
 * Client Script: onChange for "Assigned to" field
 * This script populates the "Issue manager" field based on the selected "Assigned to" user.
 *
 * @param {GlideControl} control - The control that triggered the onChange event.
 * @param {String} oldValue - The previous value of the "Assigned to" field.
 * @param {String} newValue - The new value of the "Assigned to" field.
 * @param {Boolean} isLoading - Indicates if the form is loading.
 * @param {Boolean} isTemplate - Indicates if the form is a template.
 */
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Exit if the form is still loading or if the new value is empty
    if (isLoading || newValue === '') {
        return;
    }

    // If a new user is selected in the "Assigned to" field
    if (newValue) {
        // Retrieve the user record for the selected "Assigned to" user
        g_form.getReference('assigned_to', populateIssueManager);
		
    }
}

/**
 * Callback function to populate the "Issue manager" field.
 *
 * @param {GlideRecord} assignedUser - The user record of the selected "Assigned to" user.
 */
function populateIssueManager(assignedUser) {

    
 // Set the "Issue manager" field to the sys_id of the assigned user's manager
        g_form.setValue('u_issue_manager', assignedUser.manager);
    
}

 

 

Result - Whenever some selects assigned to . issue manager will automatically populate with assigned to manager

 

AstikThombare_1-1729015824649.png

 


If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.

 

By doing so you help other community members find resolved questions which may relate to an issue they're having

 

Thanks,

Astik

 

 

HemaV
Tera Contributor

Thank you @Astik Thombare for your response. but I am expected to do this through business rule scripting. Is that something can you help me with?

Hi @HemaV ,

 

In this case, we can use a 'before' business rule to achieve the desired outcome. However, if the assigned_to field is changed, the rule will not trigger immediately. It will only execute when the form is saved.

Please refer to the screenshot and the script for the business rule below

 

AstikThombare_0-1729042874524.png

 

Script - 

 

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

     current.setValue('u_issue_manager', current.assigned_to.manager);
   

})(current, previous);

 

 

If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.

 

By doing so you help other community members find resolved questions which may relate to an issue they're having

 

Thanks,

Astik

 

 

This scripting is not working When I try to insert and save the form after the "Assigned to" field is filled. "Issue manager" field is not auto-populating. it's showing blank. 

HemaV_0-1729070873705.png

Below are my changes evidence

HemaV_1-1729071044421.png

HemaV_2-1729071081048.png