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.

Business Rule (Inserting Record from table to another table) - Best Practice .

win b
Mega Guru

Hi! Can someone guide me on this. I know that it seems to be correct but i want to know if theres another way of inserting data records to another table. Here below what i've done in my Business Rule (BR).

-----------------------------------------------------------------------------------------------

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

// Insert a new gcash wallet
var Arr = [current.variables.wallet_name,current.variables.project_name];
var gWRecord = new GlideRecord('x_582528_secure_ap_secure_acl_matrix');
gWRecord.initialize();
// gWRecord.wallet_name = current.variables.wallet_name;
for(var ctr = 0; ctr < Arr.length; ctr++){
gWRecord.wallet_name = Arr[ctr];
}
gWRecord.insert();

})(current, previous);

----------------------------------------------------------------------------

The scenario is that i got two tables which created from custom application. What i need to do is from my main table is i want to insert the populated field wallet_name to my other table 'x_582528_secure_ap_secure_acl_matrix'. But the problem is i got many fields to insert in the 'x_582528_secure_ap_secure_acl_matrix' table and using loop may cause problem in the future. Is there any possible solutions were i can insert the populated values from my main table to my other table? 

Thank you in advance for helping.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are on right track for optimizing the script

but ensure you are also using a corresponding array containing field names of the table where you want to insert so that the data is filled correctly

like this

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

    // Insert a new gcash wallet
    var Arr = [current.variables.wallet_name,current.variables.project_name];
    var targetTableFields = ['waller_name','project_name'];
    var gWRecord = new GlideRecord('x_582528_secure_ap_secure_acl_matrix');
    gWRecord.initialize();
    for(var ctr = 0; ctr < Arr.length; ctr++){
        gWRecord[targetTableFields[ctr]] = Arr[ctr];
    }
    gWRecord.insert();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are on right track for optimizing the script

but ensure you are also using a corresponding array containing field names of the table where you want to insert so that the data is filled correctly

like this

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

    // Insert a new gcash wallet
    var Arr = [current.variables.wallet_name,current.variables.project_name];
    var targetTableFields = ['waller_name','project_name'];
    var gWRecord = new GlideRecord('x_582528_secure_ap_secure_acl_matrix');
    gWRecord.initialize();
    for(var ctr = 0; ctr < Arr.length; ctr++){
        gWRecord[targetTableFields[ctr]] = Arr[ctr];
    }
    gWRecord.insert();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

win b
Mega Guru

Is there will be an issue on looping if i got let say 50 fields to be inserted? and the var Arr is currently in my variable set were my fields at. i tried using the  var targetTableFields = ['waller_name','project_name'];  but it doesnt work from my end. 

Hi,

you should set the array elements corresponding to variable

Example 1st element of field array is variable wallet_name so 1st element of the other array should be the field where you want wallet name to be populated

try this

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

	// Insert a new gcash wallet
	var Arr = ['wallet_name','project_name']; // give only variable names
	var targetTableFields = ['waller_name','project_name'];
	var gWRecord = new GlideRecord('x_582528_secure_ap_secure_acl_matrix');
	gWRecord.initialize();
	for(var ctr = 0; ctr < Arr.length; ctr++){
		gWRecord[targetTableFields[ctr]] = current.variables[Arr[ctr]];
	}
	gWRecord.insert();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ohh i see thanks for this.