Create Multiple Records using UI action based on Field Value

Still Learning
Kilo Sage

Hello SN Community,

I am working on a requirement to create multiple records using a UI action, based on a field value.  The field value is a location field (u_locations) which can contain any number of entries listed.  What should happen is if there are 7 locations listed in u_locations, then 7 incidents should be created - one for each location. What I am getting is one duplicate record that has the same number (the number should be different)  rather than 7 records.  Can someone please tell me what I'm doing wrong? Here is my script: 

doNewRecords ();
answer = current.insert();
gs.include('ActionUtils');
var au = new ActionUtils();

function doNewRecords() {
	var saveMe = current;
	var count = 0;
	var grChildRecordTable = new GlideRecord('u_operational_mechanical_incident');
//	grChildRecordTable.addEncodedQuery('sys_idIN' + current.u_releases.toString());
	grChildRecordTable.addEncodedQuery('sys_idIN' + current.u_locations.toString());
	grChildRecordTable.query();
	while (grChildRecordTable.next()) {
		count++;
		var grNewTaskTable = new GlideRecord('u_operational_mechanical_incident');
		grNewTaskTable.initialize();
		grNewTaskTable.release = grChildRecordTable.sys_id;
		grNewTaskTable.dataHalls = grChildRecordTable.u_locations;
		grNewTaskTable.incidentType = grChildRecordTable.u_incident_type;
		grNewTaskTable.start = grChildRecordTable.u_start_date;
		grNewTaskTable.report = grChildRecordTable.u_report_date;
		grNewTaskTable.caller = grChildRecordTable.caller_id;
		grNewTaskTable.description = grChildRecordTable.description;
		grNewTaskTable.insert();
		
	}
	au.postInsert(current);
}

 

find_real_file.png

5 REPLIES 5

Aman Kumar S
Kilo Patron

You are getting error due to 

answer = current.insert();// remove this line

 

What is the utility of this?

au.postInsert(current);

Best Regards
Aman Kumar

Hello Aman, 

Thank you so much for your reply.  When I comment out the answer = current.insert(); line then I don't actually get any duplicate records. I think I actually need to figure out a way to count the number of entries within the u_release field, and then create the records using that number. But I am unsure of how to do that.

I am incorporating an OOTB script include called: 'ActionUtils' and within that script include is the The 'au.postInsert(current);  function. 

Here is a copy of just the function within that script include: 

var ActionUtils = Class.create();

ActionUtils.prototype = {
   initialize : function() {
   },   
   postInsert : function(/* GlideRecord */ gr) {
      if (gr.isActionAborted())
         return;      
      var linkedM2MTable = action.get('sysparm_link_collection');
      if (linkedM2MTable)
         this._insertM2M(linkedM2MTable, gr.sys_id);
      
      var relationship = action.get('sysparm_collection_relationship');
      if (relationship) {
         var parentKey = action.get('sysparm_collectionID');
         var parentTable = action.get('sysparm_collection');
         var r = GlideRelationship.get(relationship);
         r.postInsert(parentTable, parentKey, current);
      }
   },

Got it, but what exactly is your objective here for using postInsert, that's what I was asking.

Feel free to mark correct, if your issue has been resolved, so it ends up in solved queue.

Will be helpful for others looking for the similar query.

 

Best Regards
Aman Kumar

Hello Aman,

Using the post insert function, the objective is to link the records to the entries in the u_locations field.  In other words, if there are 7 locations, then each record should be linked to one of those locations (i.e. Houston would have a record created for it, as would Dallas, etc. etc. etc.).  Currently, the script I've written is only creating one duplicate record regardless of the number of entries in the locations field.