Push List Collector to table

servicetrout
Tera Expert

Warning... javascript newbie here...

I have a List Collector that is finally working and need to push the user selected values   to a string field in my table.

It appears I should be able to do this with an OnSubmit client script.   It is my understanding the List Collector holds the Sys_Id references from the table.

Anyone able to provide an example for this?       Currently, a comma separated list of Sys_ids is showing up in my table string field.   I was trying this for starters.

function onSubmit() {

var divisions = producer.variable_pool.u_mg2divisions.toSting().split(',');

  alert(divisions);

}

1 ACCEPTED SOLUTION

Finally, got this working !.. sheesh way tougher nut to crack than it should have been.



Set up a List Collector in Record Producer.


List Collector uses lookup table of names and IDs.


List Collector loads comma separated list of Sys_IDs to target table.


To convert Sys_ID's back to original names, add this to the Script section of the record producer.





current.u_mg2divisions = convertListCollector();



function convertListCollector() {


                                                                                                                                                                                                                                                      // Get List Collector values (a comma separated list of Sys_IDs)


    var divArray = producer.Your_RecProd_FieldName.toString().split(',');                                     // Split the list into an array


    var outString = '';                                                                                                                       // Initialize/clear the output string


    for(i = 0; i < divArray.length; i++){                                                                   // Loop through List Collector Sys_ID array


                    var recObj = new GlideRecord('YourSourceTableName');   // Create table object


                    recObj.addQuery('sys_id',divArray[i]);                                     // Create query to return record associated to Sys_ID


                    recObj.query();                                                                                                           // Run the query


                  recObj.next();                                                                                                                 // Should not need a while/loop since Sys_ID should have only one record.


                  var divValue = recObj.u_divname;                                                 // create variable with value from source field


            if ( i == 0 ) {


                      outString = divValue;                                                                                       // don't start the string with a comma


              } else {


                        outString = outString + ', ' + divValue;                                 // Append to output string variable


              }


      }



  return outString;


}


View solution in original post

5 REPLIES 5

servicetrout
Tera Expert

Making progress, now have the references in a variable, I can see.



function onSubmit() {


  var value_field = g_form.getValue('u_mg2divisions');


  alert(value_field);



}


Just need to split the string based on the commas, convert the reference back to the value, then concatenate the values back into a string, right?


Interesting, if I set the target field to a reference, it shows just the   values for the last Sys_id value from the items selected in the List Collector.


Got this far, the alert shows the Sys_ID for the field, but it isn't converting the Sys_ID to the value.



function onSubmit() {


  var value_field = g_form.getValue('u_mg2divisions');


// alert(value_field);     (works)



  var divArray = value_field.split(',');


//       alert(divArray);   (works)




for(i = 0; i < divArray.length; i++){


  alert(divArray[i]);     // Works



  var co = new GlideRecord('u_badgmg2div');         // something below here is not working


  if (co.get(divArray[i])) {


    var display = '';


    display = co.getDisplayValue();


    alert(display);                                                                           // does not show... hangs.


   


  }



// Hold for reference


//           app.addQuery('sys_id',divArray[i]);


//app.addQuery('field', 'true');


//app.addNullQuery('field');


//app.query();


//if(app.next()) {





}


Figured out this code will convert the List Collector array of Sys_ID's into a string, but not sure how to replace the table field with this new string.


Anyone have any insight?     Would it be possible to add this as a function in the Record Producer Script field and call the function?



Record Producer Client Script:



function onSubmit() {



  // Get the value in the Record Producer field, which is a comma separated list of Sys_IDs


  var value_field = g_form.getValue('u_mg2divisions');        


  var divArray = value_field.split(',');                                                             // Split the list into an array



var outString = '';                                                                                                                     // Initialize/clear the output string


for(i = 0; i < divArray.length; i++){                                                                 // Loop through List Collector Sys_ID array


// alert('Inside loop I:' + i +   'value: ' + divArray[i]);




  var recObj = new GlideRecord('u_badgmg2div');             // Create table object


  recObj.addQuery('sys_id',divArray[i]);                                             // Create query to return record associated to Sys_ID


  recObj.query();                                                                                                                   // Run the query


      recObj.next();                                                                                                                   // Should not need a while/loop since Sys_ID should have only one record.


  var divValue = recObj.u_divname;                                                       // create variable with value from source field


  //alert('Division ' + i + ' : ' + divValue);



  if ( i == 0 ) {


  outString = divValue;                                                                                                 // don't start the string with a comma


  } else {


  outString = outString + ', ' + divValue;                                           // Append to output string variable


  }



//Debug:     alert('outString: ' + outString);                                       // Debug: check it


}


  g_form.setValue('u_mg2divisions', outstring);               // This sets the form... need to set/replace the table field


//   current.u_mg2divisions = outString;                                         // Not allowed to use current.




}