return array as a consolidated string value in a business rule so i can list all found records together

dave_edgar
Mega Guru

scenario is i need to find all items related to my parent record and list them out in a script to create a file for them

I am able to create the file fine but the lookup and return of the child records is causing me a headache

this script finds them 

 

var allParts = '';
var rmap = new GlideRecord("u_rma_part_information");
rmap.addQuery("u_rma", current.sys_id);
rmap.addEncodedQuery('ORDERBYsys_created_on');
rmap.query();
while(rmap.next()) {
allParts = rmap.u_rma_part_failed_part_serial + " - " + rmap.u_rma_part_pid + " - " + rmap.u_rma_part_chassis_serial;
gs.log("RMA parts 1 is: " + allParts);
}

// so the gs.log out puts each line found but i want them consolidated so I can spit them out as a single entry below, I've tried allParts[i]; but that fails

var arr = number+ u_rma_ship_to_information+ u_rma_address+ u_rma_city+ u_rma_state+ u_rma_zip+
u_rma_country+ u_rma_ship_to_contact_name+ u_rma_ship_to_contact_phone+ u_rma_ship_to_contact_email+
u_rma_customer_reference+ u_rma_shipping_advice+
allParts   ;

 

any ideas my scripting friends

 

I need the found records in a string as i need to push them into a file that is being created along with the parent records fields values, the italic fields.

This email script works for an email notification to bring them all back but i can;t appear to get that working for this need:

var gr = new GlideRecord("u_rma_part_information");
gr.addQuery("u_rma", current.sys_id);
gr.addEncodedQuery('ORDERBYsys_created_on');
gr.query();
while(gr.next()) {
template.print("FPN: "+ gr.u_rma_part_failed_part_serial + "<br />" +
"PID: " + gr.u_rma_part_pid + "<br />" +
"CS/N: " + gr.u_rma_part_chassis_serial + "<br />");
}

1 ACCEPTED SOLUTION

Piggy backing off of Allen, here's an example of what your code would look like using an array: 

var allParts = [];

var gr = new GlideRecord("table");
gr.addQuery('query');
gr.query();

while(gr.next()) {
  allParts.push(gr.my_field); // .push() will add a new item into your array
}

// Finally
gs.log(allParts.toString()); // Comma seperated list of things

Hope this helps,

Dylan

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hello,

Sorry, your post is a bit confusing because you're saying you're using an array and you don't have one declared anywhere. allParts...is not an array, you've declared that as a string variable. Can you clarify what you're trying to do?

You're trying to loop through relevant records and add this line of string for each record found?

allParts = rmap.u_rma_part_failed_part_serial + " - " + rmap.u_rma_part_pid + " - " + rmap.u_rma_part_chassis_serial;

Then you want to want to do what at the bottom of your code with it?

If you're trying to loop through and add that same line over and over for all the different records, then you'd want to do:

allParts += rmap.u_rma_part_failed_part_serial + " - " + rmap.u_rma_part_pid + " - " + rmap.u_rma_part_chassis_serial + '\n';

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Piggy backing off of Allen, here's an example of what your code would look like using an array: 

var allParts = [];

var gr = new GlideRecord("table");
gr.addQuery('query');
gr.query();

while(gr.next()) {
  allParts.push(gr.my_field); // .push() will add a new item into your array
}

// Finally
gs.log(allParts.toString()); // Comma seperated list of things

Hope this helps,

Dylan

P.S. I see you're add an encoded query in order to order your records by created date. There are some functions that come with the GlideRecord() API to handle this. You can use: 

gr.orderBy('field_name'); // For ascending order

- and -

gr.orderByDesc('field_name') // For descending order

Cheers,

Dylan

Thanks. I'm just trying to start easy with them to see what they're trying to do without throwing too much at them.

Appreciate the help.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!