Help with transforming fields

shane_davis
Tera Expert

I have a data source and transform map which automatically creates assets from an email with an XLS attachment.   The asset creation is working by adding assets to the alm_hardware table.   The columns on the spreadsheet are filling in the serial_number, po_number, u_order_number and invoice_number.

My issue is that I need to fill in the following Target fields and I cannot figure out how.

                                                                                                                   

SourceTarget
u_chassis_descriptionfind_real_file.png
manufacturer
find_real_file.png
u_order_date (mm/dd/yyyy format)purchase_date (yyyy-mm-dd format and Dictionary info is alm_hardware.purchase_date)
1 ACCEPTED SOLUTION

This could be not as bad as I thought. It looks like each one is always the same, just not exactly what you have in ServiceNow. Try something like this:



// Set the model (must be a String or will not match the cases)


switch (source.u_chassis_description.toString()) {


case 'Opti 7010 (USFF)':


      answer = 'Dell Inc. OptiPlex 7010';


      break;


case 'Opti 9020 (USFF)':


      answer = 'Dell Inc. OptiPlex 9020';


      break;


case 'Latitude E5540':


      answer = 'Dell Inc. Latitude E5540';


      break;


case 'Latitude E7440':


      answer = 'Dell Inc. Latitude E7440';


      break;


default:


      answer = '';


}



Let me know if that worked and if you find it working for some but not all of your data. If you have more things that need to match up, you can add more case, answer, break statements above the default section.


View solution in original post

15 REPLIES 15

Shawn Dowler
Tera Guru

Are you having difficulty with the reference fields? It depends a lot on how you want to do it, but the reference fields require you to provide some unique identifier. That can be a sys_id or a serial number. Here is some code I used to import users' department information. If the department already exists, then it just sets the department ID and moves on. If the department doesn't exist, then a new one is created. You need to become familiar with GlideRecord and how to build a GlideRecord query.



This script had the reference value field name set to "id"



// Associate Department with User. Create new department or update department name if necessary


// Check if the Department already exists


if (!source.u_department_id.nil() && !source.u_department_name.nil()) {


      var dept_gr = new GlideRecord("cmn_department");


      var dept_id = source.u_department_id;


      dept_gr.addQuery('id', dept_id);


      dept_gr.query();


      if (!dept_gr.next()) {   // if department does not exists create a new Department and associate it with the user


              dept_gr = new GlideRecord("cmn_department");


              dept_gr.name = source.u_department_name;


              dept_gr.id = source.u_department_id;


              dept_gr.update();   // Create the department


      } else {


              if (dept_gr.name != source.u_department_name) {   // if the department name has changed


                      dept_gr.name = source.u_department_name;   // change the name


                      dept_gr.update();   // Update the department


              }


      }



      answer = dept_id;   // associate the department with the user


}



Does this get you any closer to what you are looking for? Do you have any more specific questions? I tried to guess where you might be running into some trouble. You will need to do something similar for setting the vendor to IBM. You can set the value using a script, but it's even simpler. You just need answer = "IBM" assuming IBM is already in the table and IBM is the display value.



In the import set for your date, there is a field called Date format that you need to set to match your date format on your source.


Shawn - Thank you for your reply.   I was able to get the Acquisition method and Vendor to populate as these are the easiest ones.   Yes, I am having trouble with the reference fields as I am very new to scripting!    


shane_davis
Tera Expert

I was able to get the source "u_order_date" from the XLS file of format mm/dd/yyyy to the ServiceNow "purchase_order" format of mm-dd-yyyy by using the script below.



var month = source.u_order_date.split('/')[0];  


var day = source.u_order_date.split('/')[1];  


var year = source.u_order_date.split('/')[2];  


 


var newDate = month + '-' + day + '-' + year;  


answer = newDate;


Chandan23
Tera Expert

If your source is having simple string value:


then in the field map please use



Referenced value field name   and then by source scriptset the excat value you want to map.