- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
HI,
We are loading User data via transform maps to sys_user table. However the department field also is mapped to department field in user table. (source_department)
SInce the choice is set Create=TRUE for Department, it is creating Departments in cmn_department table.
However if that happens we want to update the ID field in cmn_department table with some static value to identify what came via import . (since we also manually upload departments )
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
in that case don't use field map for department. please use onBefore transform script
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
var sourceDepartment = source.u_department_name;
var gr = new GlideRecord("cmn_department");
gr.addQuery("name", sourceDepartment); // query with name if department name is coming in staging table
gr.query();
if (gr.next()) {
target.department = gr.getUniqueValue();
} else {
// not found then create
gr.initialize();
gr.name = sourceDepartment;
// populate other fields if you wish
gr.id = 'Some Unique ID';
gr.insert();
target.department = gr.getUniqueValue();
}
})(source, map, log, target);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar the same script what you suggested as onBefore
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) { // Add your code here var sourceDepartment = source.u_department; log.info('<<<target dept onbefore 26'+ sourceDepartment ); var gr = new GlideRecord("cmn_department"); gr.addQuery("name", sourceDepartment); // query with name if department name is coming in staging table gr.query(); if (gr.next()) { target.department = gr.getUniqueValue(); } else { // not found then create gr.initialize(); gr.name = sourceDepartment; // populate other fields if you wish gr.id = 'beforescript'; gr.insert(); target.department = gr.getUniqueValue(); } })(source, map, log, target); |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
so gr.id line is not working?
Did you try creating new department from background script and populate some random value in that id field?
I was able to insert with some random value, so it should work fine for you.
I believe I answered your question and you can take it further from here based on your experience and developer skills.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
yes the ID field is not populating in cmn_department rest all okay.
The IF else condition is not executing . Tried putting logs but no logs in either IF loop /ELSE loop
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
did you try running background script and see?
I was able to set id field via script.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi Nisha,
You need to create a onChoiceCreate Transform script for this :
https://www.servicenow.com/docs/bundle/zurich-integrate-applications/page/script/server-scripting/re...
then you can use something like if( name == 'your deparment field name ') then target.department.id = 'yourvalue '. this is a pseudo code which you need to adapt. I have added alink to docs that is more detailed on how onchoice create works and objects available for use.
//Shivambi