Adding a list of CIs from a form/record producer to the CI field and affected CIs related list

david_f
Kilo Explorer

Hello all,

I help out by creating forms for our ServiceNow instance, I'm no Javascript developer but I can muddle my way through with fairly basic commands. Right now I'm updating one of our Change forms and I wanted to automatically update the Change with the CI/Affected CIs that we capture in the form. I did a search and found a post (https://community.servicenow.com/thread/278982?q=Adding%20a%20list%20of%20CIs%20from%20a%20form/reco... ) but it seems to be an update to 2 fields on the same table.

In our instance, I'd want to update the Configuration item field (cmdb_ci) on the Change with the first CI captured from the record producer/form and any additional CIs in to the affected CIs related list table (ci_task) - we wouldnt always have more then 1 CI in the list.

One of our developers did take a quick shot at putting this functionality in but it doesnt work. He had put in the below. Any chance one of you wonderful people could help with a code snippet to complete what I need?

//get the affected CIs and put into glide list

var CIs = new GlideRecord('u_cmdb_ci_interim_node');    

CIs.addQuery("sys_id","IN",producer.node);  

CIs.query();

// add each CI from glide list to affected CIs related list

var counter = 0;

while (CIs.next()) {

var affectedCI = new GlideRecord('task_ci');

affectedCI.initialize();

affectedCI.ci_item = CIs.sys_id;

affectedCI.task = current.sys_id;

affectedCI.insert();

counter++;

// add first CI to "Configuration Item" field in Change Request

if (counter == 1){

current.cmdb_ci = CIs.sys_id;

}

}

Thanks for your time,

David

1 ACCEPTED SOLUTION

erik_brostrom
Mega Guru

Is it doing anything? Inserting blank records, same record over and over, or just not doing anything at all?


If nothing at all, whomever that is submitting the record producer may not have the appropriate rights on the u_cmdb_ci_interim_node to create, I'd check the ACL's on that table to confirm vs whom is submitting.



if it is inserting blank records or multiple of the same, when in a your while, I'd add "CIs.sys_id.toString()".



Hopefully that helps...


View solution in original post

3 REPLIES 3

erik_brostrom
Mega Guru

Is it doing anything? Inserting blank records, same record over and over, or just not doing anything at all?


If nothing at all, whomever that is submitting the record producer may not have the appropriate rights on the u_cmdb_ci_interim_node to create, I'd check the ACL's on that table to confirm vs whom is submitting.



if it is inserting blank records or multiple of the same, when in a your while, I'd add "CIs.sys_id.toString()".



Hopefully that helps...


Hello,



It's not doing anything at all, you're absolutely right that users dont have access to create on that table.



Thanks for that!



Regards,



David


JC S_
Mega Guru

Hi David,



Where did you put this code in the record producer? You can't use current on client scripts right? We also have the similar requirement to be able to allow users to enter multiple CIs for a change record so we created a Service Portal record producer, we already have the list collector for 1 of the fields in the form and we just need to transfer the content of that list collector to the Affected CI related list.