How can I ignore a record in an import set after using a Service Catalog cart?

danieljgates
Tera Contributor

I am reading in an import set, for each record in an OnBefore script, I need to create a Service Catalog cart request and request item. Then, the given catalog service item will generate tasks through a workflow. Once that is done, I need to update the tasks to close them. Once the task is closed, it creates a record into the target table I am running the import script on. Next, I want to take the request item number and put it into the record that was created from the workflow. Since the record would already have been created in the target table with all the information, I want to ignore the import record at that point. I have tried to use the ignore = true; statement, but once I place the order from the cart, the ignore = true; statement is not effective.

Any ideas?

2 REPLIES 2

edwin_munoz
Mega Guru

Hello Daniel,



Are you always generating the records in the target table after the task is closed? Is it really needed to import any data at all?


Maybe you can ignore all record in the beginning?



Could you please share onBefore script?



Thank you.


Code is below.



We are loading multiple tables around our own version of access control defining the "meta data" for control. There are no issues there. We also need to load the initial set of users already granted access to existing systems when the "meta data" goes live. This will be placed into the User Role table. But, the audit and information security departments need to track why a given user was granted and subsequently revoked a role. Typical role request, grant or revoke, will be done through our Service Catalog item. The request would be entered, with the request item being the main piece holding the user and the role. Then, once all approved, then catalog tasks would be generated to our help desk. When those tasks are completed, the workflow then enters a record into the User Role table to keep track.



So, for our initial load of the "meta data" and user roles, we need to create the same request, request item and tasks so that they can be tracked in the future. Therefore, I was reading in the list of users and roles and generating a cart to follow the normal workflow process. The problem is the normal process does not have the request item in it and I need to update that. So, I do, after I create the cart and place the order. I then move forward and complete the task. Then, since the workflow added the User Role record and I update the request item, I just wanted to skip the incoming record.



I tried just ignore = true statement, just by itself and it works. Once I do a cart.placeOrder(), it does not. I have tried moving the ignore = true statement around, but still no avail.





log.info('Input - User Id: ' + source.u_user_id);


log.info('Input - Role: ' + source.u_role);


log.info('Input - Configuration Item Name: ' + source.u_configuration_item_name);


log.info('Input - Start Date: (' + source.u_start_date + ')');


log.info('Input - End Date: ' + source.u_end_date);






ignore = true;




var userRequestedFor;


var users = new GlideRecord('sys_user');


users.addQuery('user_name',source.u_user_id);


users.query();




if (users.next()) {


  userRequestedFor=users.sys_id;


}




var configurationItem;


var configurationItems = new GlideRecord('cmdb_ci_service');


configurationItems.addQuery('name',source.u_configuration_item_name);


configurationItems.query();




if (configurationItems.next()) {


  configurationItem=configurationItems.sys_id;


}




var role;


var roles = new GlideRecord('x_eslfc_acc_ctrl_role');


roles.addQuery('name',source.u_role);


roles.addQuery('configuration_item',configurationItem);


roles.query();




if (roles.next()) {


  role=roles.sys_id;


}




var neededFrom;




neededFrom = new GlideDateTime();




var justification = 'Access Control Import - ' + source.u_configuration_item_name + ': Initial load of already granted roles.';






//Create Request


var cart = new global.Cart(null,userRequestedFor);


cart.requested_for = userRequestedFor;


var item = cart.addItem('6b83f33637fb3100a5fdd5c543990e3e');




//Set Variables in your Cart Item


log.info('Cart Variable - Requested For: ' + userRequestedFor);


log.info('Cart Variable - Configuration Item: ' + configurationItem);


log.info('Cart Variable - Role: ' + role);


log.info('Cart Variable - Needed From: ' + neededFrom.toString());


log.info('Cart Variable - Justification: ' + justification);




cart.setVariable(item, 'requested_for', userRequestedFor);


cart.setVariable(item, 'cmdb_ci',configurationItem);


cart.setVariable(item, 'role', role);


cart.setVariable(item, 'needed_from', neededFrom);


cart.setVariable(item, 'justification', justification);




var request = cart.placeOrder();




request.requested_for = userRequestedFor;


request.short_description = justification;


request.description = justification + ' This is for User Id - ' + source.u_user_id + ' and Role ' + source.u_role + '.';


request.update();




log.info('Cart Request Number: ' + request.number);




var requestItem;


var requestItemNumber;


var requestItems=new GlideRecord('sc_req_item');


requestItems.addQuery('request.number',request.number);


requestItems.query();




if (requestItems.next()) {


  requestItem=requestItems.sys_id;


  requestItemNumber=requestItems.number;


  requestItems.configuration_item = configurationItem;


  requestItems.update();


}




log.info('Cart Request Item: ' + requestItem);


log.info('Cart Request Item Number: ' + requestItemNumber);




// Now find the task that was generated, assigned it to System Administrator and close it.


var systemAdministrator;


var users = new GlideRecord('sys_user');


users.addQuery('user_name','admin');


users.query();




if (users.next()) {


  systemAdministrator=users.sys_id;


}




var tasks = new GlideRecord('sc_task');


tasks.addQuery('request_item', requestItem);


tasks.query();




while (tasks.next()) {


  log.info('Cart Task Number: ' + tasks.number);


  tasks.state=3;


  tasks.assigned_to = systemAdministrator;


  tasks.close_notes = 'Closed due to Access Control Import - ' + source.u_configuration_item_name + ': Initial load of already granted roles.';


  tasks.update();


}




// Since the closing of the task will create the user_role record, we do not need to load one, just update it with the request item.


var userRoles = new GlideRecord('x_eslfc_acc_ctrl_user_role');


userRoles.addQuery('user',userRequestedFor);


userRoles.addQuery('role',role);


userRoles.query();




while (userRoles.next()) {


  log.info('Updating user role ' + userRoles.sys_id);


  userRoles.request_item_for_grant = requestItem;


  userRoles.update();


}




ignore = true;