Copy records from custom table to OOB table

roomawakar
Tera Contributor

Hi All,

I am trying to copy records from a custom table - 'u_m2m_kb_ci' to the OOB table -'m2m_kb_ci' through the fix script.

 When I ran the script, it just inserted the blank rows of the same count as of custom table.

Can someone please tell, where I am making mistake.

 

var gr = new GlideRecord('u_m2m_kb_ci');

gr.query();

while(gr.next()){

    var gr1 = new GlideRecord('m2m_kb_ci');{

    gr1.cmdb_ci=gr.cmdb_ci;

    gr1.kb_knowledge=gr.kb_knowledge;

    gr1.insert();

    }

}

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

I think you'd be way better off building this in Flow Designer, even if you're going to run it once, if this level of coding is challenging (which is TOTALLY ok!).  That way you operate only in a world of things that actually exist.

 

First, use GOOD VARIABLE names so you don't get yourself confused between gr and gr1

lets go with oldKBCI and newKBCI.

 

Second, you don't have to keep declaring the gr1/newKBCI variable.  Define it once outside the while loop and you can STILL push inserts to it.  Also, you have to initialize it.

Third, I'm betting cmdb_ci and kb_knowledge don't actually exist on u_m2m_kb_ci.  I'll bet they're actually u_cmdb_ci and u_kb_knowledge.   Custom tables prefix with u_ as do custom fields in those tables.  Scopes are an exception to this but you'd then have a scope prefix... at least on the table.

 

 

 

var oldKBCI = new GlideRecord('u_m2m_kb_ci');
oldKBCI .query();
var newKBCI = new GlideRecord('m2m_kb_ci');
newKBCI.initialize();
while(oldKBCI .next()){
    newKBCI.cmdb_ci = oldKBCI.u_cmdb_ci;
    newKBCI.kb_knowledge = oldKBCI.u_kb_knowledge;
    newKBCI.insert();
}

 

 

DOING IT IN FLOW INSTEAD

Trigger:  Scheduled
1)  Look Up Records (u_m2m_kb_ci)
2)  For each of (1)
3)  Create Record (m2m_kb_ci)

--- cmdb_ci = (drag cmdb_ci value from node 2 on data panel)
--- kb_knowledge = (drag kb_knowledge value from node 2 on data panel)

 

 

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@roomawakar 

your script is incomplete.

Please apply correct query to fetch right number of records and then insert

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader