variable sets - how to capture new variable values to a new table?

woodyfairley
Tera Guru

A former developer who is highly skilled created a service portal app which uses a variable set to collect accounting data and populate table fields somewhere, I am still looking. Now the accounting fields have changed and I need to incorporate new variables to collect and preserve new accounting data. I created a new table in scope with the new accounting fields, and added new variables to the variable set, but I am not sure how to use the new variables to populate new records in the new table.

I want to follow best practices, do I create a business rule and somehow send the variables' values to a new record in the new accounting table with an Insert trigger? Do I create a Flow to do this?

His application is very advanced and while I am learning a lot by examining his work, my deadline is fast approaching and I need to deliver the changes quickly.

1 ACCEPTED SOLUTION

@woodyfairley 

try this

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

	var req = new GlideRecord('sc_req_item');
	req.get(current.sys_id);

	//gs.info("Org Code: " + current.org_code + "\nProject Number: " + current.project_number + "\nTask Code: " + current.task_code + "\nFiscal Period: " + current.fiscal_period);


	if (!gs.nil('' + req.variables.cao_payment_and_billing)) {
		var segs = '' + req.variables.cao_payment_and_billing;
		segs = JSON.parse(segs);

		for (var x = 0; x < segs.length; x++) {
			//this should create the new record in the BAS Accounting Segments table;

			var segmentsBAS = new GlideRecord(x_g_cao_print_serv_bas_accounting_segments);
			segmentsBAS.addQuery('bas_task', segs[x].bas_task);
			segmentsBAS.query();
			if (!segmentsBAS.next()) {
				segmentsBAS.initialize();
				//segmentsBAS.agency = current.variables.agency;
				//segmentsBAS.print_request = segs[x].sys_id;
				segmentsBAS.bas_project = segs[x].bas_project;
				segmentsBAS.bas_task_code = segs[x].bas_task_code;
				segmentsBAS.bas_organization = segs[x].bas_org_code;
				segmentsBAS.bas_object_class = segs[x].bas_object_class;
				segmentsBAS.bas_budget_fiscal_year = segs[x].bas_budget_fiscal_year;
				//var newSegmentsBAS = segmentsBAS.insert();
				segmentsBAS.insert();
			}
		}
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@woodyfairley 

Is that developer using record producer to populate the variable data to the target table field?

Please share some details around this

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

Thank you for your consideration, I spent the day and evening trying to follow his logic. He created a business rule to capture the variables from a request item form (not a record producer) which contains several variable sets, I found the script too complex to understand but duplicated it as closely as possible with this script. I created a new table to contain the new accounting values (simple text fields) and added the relationship, and also added the accounting table to the bottom of the form in the backed as a related list, but the script does not populate a new record in the accounting table. Any insights you may offer will be appreciated.

This is an "after" business rule with on insert checked and it is applied to the table he created to extend the request item.

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

    var req = new GlideRecord('sc_req_item');
    req.get(current.sys_id);

    //gs.info("Org Code: " + current.org_code + "\nProject Number: " + current.project_number + "\nTask Code: " + current.task_code + "\nFiscal Period: " + current.fiscal_period);


    if (!gs.nil('' + req.variables.cao_payment_and_billing)) {
        var segs = '' + req.variables.cao_payment_and_billing;
        segs = JSON.parse(segs);

        for (var x = 0; x < segs.length; x++) {
            //this should create the new record in the BAS Accounting Segments table;

            var segmentsBAS = new GlideRecord(x_g_cao_print_serv_bas_accounting_segments);
            segmentsBAS.addQuery('bas_task', segs[x].bas_task);
            segmentsBAS.query();
            if (!segmentsBAS.next()) {
                //segmentsBAS.agency = current.variables.agency;
                //segmentsBAS.print_request = segs[x].sys_id;
                segmentsBAS.bas_project = segs[x].bas_project;
                segmentsBAS.bas_task_code = segs[x].bas_task_code;
                segmentsBAS.bas_organization = segs[x].bas_org_code;
                segmentsBAS.bas_object_class = segs[x].bas_object_class;
                segmentsBAS.bas_budget_fiscal_year = segs[x].bas_budget_fiscal_year;
                //var newSegmentsBAS = segmentsBAS.insert();
            }
            segmentsBAS.insert();
        }
    }

})(current, previous);

 

@woodyfairley 

try this

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

	var req = new GlideRecord('sc_req_item');
	req.get(current.sys_id);

	//gs.info("Org Code: " + current.org_code + "\nProject Number: " + current.project_number + "\nTask Code: " + current.task_code + "\nFiscal Period: " + current.fiscal_period);


	if (!gs.nil('' + req.variables.cao_payment_and_billing)) {
		var segs = '' + req.variables.cao_payment_and_billing;
		segs = JSON.parse(segs);

		for (var x = 0; x < segs.length; x++) {
			//this should create the new record in the BAS Accounting Segments table;

			var segmentsBAS = new GlideRecord(x_g_cao_print_serv_bas_accounting_segments);
			segmentsBAS.addQuery('bas_task', segs[x].bas_task);
			segmentsBAS.query();
			if (!segmentsBAS.next()) {
				segmentsBAS.initialize();
				//segmentsBAS.agency = current.variables.agency;
				//segmentsBAS.print_request = segs[x].sys_id;
				segmentsBAS.bas_project = segs[x].bas_project;
				segmentsBAS.bas_task_code = segs[x].bas_task_code;
				segmentsBAS.bas_organization = segs[x].bas_org_code;
				segmentsBAS.bas_object_class = segs[x].bas_object_class;
				segmentsBAS.bas_budget_fiscal_year = segs[x].bas_budget_fiscal_year;
				//var newSegmentsBAS = segmentsBAS.insert();
				segmentsBAS.insert();
			}
		}
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thank you for this script, I took what you provided and built on it to make it work, see below. I learned that the original variable was a multirow variable set, and while it was originally incorporated into every catalog item in the scope, a different developer was trying to fix a problem and ended up deleting some of the occurrences so the original MRVS was not visible is all of the catalog items, plus some anomalous variable sets, variables and other artifacts remained from the failed attempts, including some remnants of code from a fix I tried last year.

Thank you Ankur for your help!

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

    var req = new GlideRecord('sc_req_item');
    req.get(current.sys_id);
    //gs.info("Woody here is the current sys_id= " + current.sys_id);
	
    //if (!gs.nil('' + req.variables.cao_bas_accounting_segments)) {
	// this if checks to see if the variable is visible, or empty?? not sure;
	
    var segs = '' + req.variables.cao_bas_accounting_segments;
	// this variable has to be a multirow variable set, not a single row;
	
	gs.info('about to parse the json data');
	
    segs = JSON.parse(segs);

    gs.info("Woody please note segs= " + segs); // this returns Woody please note segs= [object Object]

    for (var x = 0; x < segs.length; x++) {
        //target table name is 'x_g_cao_print_serv_bas_accounting_segments'- must add quotes;
        var segmentsBAS = new GlideRecord('x_g_cao_print_serv_bas_accounting_segments');
        //segmentsBAS.addQuery('bas_task', segs[x].bas_task);
        segmentsBAS.query();
		//use while and do not use (!segmentsBAS.next()) remove the !
        while (segmentsBAS.next()) {
            segmentsBAS.initialize();
            segmentsBAS.agency = 13;
            segmentsBAS.print_request = current.sys_id;
            segmentsBAS.bas_project = segs[x].cao_bas_project;
            segmentsBAS.bas_task_code = segs[x].cao_bas_task_code;
            segmentsBAS.bas_organization = segs[x].cao_bas_org_code;
            segmentsBAS.bas_object_class = segs[x].cao_bas_object_class;
            segmentsBAS.bas_budget_fiscal_year = segs[x].cao_bas_budget_fiscal_year;
            //var newSegmentsBAS = segmentsBAS.insert();
            segmentsBAS.insert();
        }
    }
    //}

})(current, previous);