- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 04:28 AM
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();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:01 AM - edited 12-11-2024 05:27 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 04:51 AM
Hello @roomawakar,
I think in your fix script code, the first thing you need to do is add a filter using 'addQuery'. The second thing is to use 'gr1.initialize()' for creating the records.
For Example:
var gr = new GlideRecord('u_m2m_kb_ci');
gr.addQuery(); // Apply a filter condition if you want to add
gr.query();
while(gr.next()){
var gr1 = new GlideRecord('m2m_kb_ci');{
gr1.initialize();
gr1.cmdb_ci=gr.cmdb_ci;
gr1.kb_knowledge=gr.kb_knowledge;
gr1.insert();
}
}
Please mark my solution as Helpful and Accepted, if it works for you in any way!
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:06 AM
The problem isn't that he's missing an addQuery (because he wants ALL of them).
The heart of the problem is that he's got a custom table: u_m2m_kb_ci.
Custom tables will have u_ prefixes on their custom columns. So the values they're trying to set don't exist, because the fields literally don't exist either.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:01 AM - edited 12-11-2024 05:27 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:55 AM
@Uncle Rob Thank you so much. The issue has been resolved now. I was using the wrong field name of the custom table. I didn't notice that at all.
Also, I tried doing it through flow designer, The issue I am facing is I am select any pill in 'For each' . There is nothing selectable. Attaching the screenshot.