Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto populate field with past info.

asd22
Tera Contributor

Hello,

I have a contract form where users enter their information. Currently, the form automatically populates details such as the address, but the bank account number still has to be entered manually. Is there a way to have the bank account number auto-populate if the user has already provided it once? Note that the bank account number is not stored in the sys_user table but in a separate CONTRACT table.

10 REPLIES 10

ChallaR
Giga Guru

@asd22 Hi ,

 

Use a Script to Fetch Previous Bank Account Info

Option 1: Client Script (onLoad or onChange)

You can write a Client Script that runs when the form loads or when the user is selected, and fetches the bank account number from the most recent contract record.

 

Exmpl-

(function executeRule(current, gForm, gUser, gSNC) {
var userId = gForm.getValue('user'); // Assuming 'user' is the reference field

if (userId) {
var ga = new GlideAjax('GetBankAccount');
ga.addParam('sysparm_name', 'getBankAccount');
ga.addParam('sysparm_user_id', userId);
ga.getXMLAnswer(function(response) {
if (response) {
gForm.setValue('bank_account_number', response);
}
});
}
})(current, gForm, gUser, gSNC);

 

Else there is on more way -

Option 2: GlideAjax Script Include

Create a Script Include to query the CONTRACT table and return the bank account number.

 Script Include (Client Callable) 

 

var GetBankAccount = Class.create();
GetBankAccount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBankAccount: function() {
var userId = this.getParameter('sysparm_user_id');
var contractGR = new GlideRecord('contract');
contractGR.addQuery('user', userId);
contractGR.orderByDesc('sys_created_on'); // Get latest contract
contractGR.query();
if (contractGR.next()) {
return contractGR.getValue('bank_account_number');
}
return '';
}
});

 

NOTE -Make sure the Script Include is Client Callable.

 

  • You can add a condition to only populate the field if it’s empty.
  • Consider adding a confirmation message or tooltip to let users know the value was auto-filled.
  • If multiple contracts exist, you can choose to pull from the latest or most relevant one.

 

asd22
Tera Contributor

the bankaccount number is not in any sys_user table. its in a completley different table u_contract. And is shown on the contract form once sent in. The other autopopulated info is taken from the sys_user table

@asd22 ,

var GetBankAccount = Class.create();
GetBankAccount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBankAccount: function() {
var userId = this.getParameter('sysparm_user_id');
if (!userId) return '';

var contractGR = new GlideRecord('u_contract');
contractGR.addQuery('u_user', userId); // Replace 'u_user' with your actual reference field
contractGR.addNotNullQuery('u_bank_account_number');
contractGR.orderByDesc('sys_created_on'); // Get the latest contract
contractGR.query();

if (contractGR.next()) {
return contractGR.getValue('u_bank_account_number');
}
return '';
}
});

 

(function executeRule(current, gForm, gUser, gSNC) {
var userId = gForm.getValue('u_user'); // Replace with your actual user reference field

// Only fetch if bank account is empty
if (userId && !gForm.getValue('u_bank_account_number')) {
var ga = new GlideAjax('GetBankAccount');
ga.addParam('sysparm_name', 'getBankAccount');
ga.addParam('sysparm_user_id', userId);
ga.getXMLAnswer(function(response) {
if (response) {
gForm.setValue('u_bank_account_number', response);
}
});
}
})(current, gForm, gUser, gSNC);

@asd22 HI ,

If there is no link how the data is populating ? there have be a link / connection else its not possible to fetch the data.

 

Thanks,

Rithika.ch