Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

One time Fix Script to create relationship

Bijay Kumar Sha
Giga Guru

Hi,

I wanted to write a one time fix script to create relationship between cmdb_ci_ec2_instance table and corresponding cmdb_ci_aws_account ci (this is a reference field in the form of ec2 instance record) if the install status of ec2 instance record is 'Installed''. I've prepared a fix script as below -

 

var ec2 = new GlideRecord('cmdb_ci_ec2_instance');
ec2.addEncodedQuery('install_status=1');
ec2.query();
while (ec2.next()){
    var ciRelationship_add = new GlideRecord('cmdb_rel_ci');
    ciRelationship_add.initialize();
    ciRelationship_add.child = ec2.sys_id;
    ciRelationship_add.parent = ec2.u_aws_account_id;   //reference field for cmdb_ci_aws_account
    ciRelationship_add.type = 'bf83653c0ab30150761028c73a4de0f4';  //Relationship Type = Managed by::Manages
    ciRelationship_add.insert();
}
 
While running this script, it's keep running and running. I don't know if there is any impact in performance for this.
 
Can anyone please let me know what might be the issue or what modification do i need to do?
5 REPLIES 5

Hi @Community Alums ,

Total I've 2.5 lac of records are there in cmdb_ci_ec2_instance table and out of which around 7k are having install status = installed.