Throw Exceptions for Field Mapping in Transform Map

Mrman
Tera Guru

Hi All,

I have critical requirement about Transforming the data to the target table (cmdb_ci) . We have already created field maps for the corresponding target field and also few Transform scripts .

Now the requirement is to throw exceptions by logging it using a script . For example we have the following field maps .I have highlighted   some fields for which exception has to be done.

Now I want to know how do I write script for already created direct field mappings like cinum , hostname . Where can I write the script by using the below mentioned script include. Any Response will be really helpful.

Please throw exceptions for the following conditions (and do no create the record):

1. Blank CINUM

2. Blank or space in HOSTNAME

3. Invalid HOSTNAME (not a valid pipeline or operational value if App/Web/DB Inst)

4. Invalid STATUS (not 'Not Ready', 'Operating', 'Decommissioned', 'Return_Owner', or 'Withdrawn')

5. Invalid SERVICETYPE (not Gold, Silver, Bronze, or blank)

6. Invalid ENVIRONMENT (not Production, Pre-production, UAT, Development, or blank)

7. Blank or invalid PRIMARY_AFFECTED_COUNTRY (based on Location table)

8. Blank SERIALNUM (if CI_Server_Type=Physical)

9. Blank MANUFACTURER (if CI_Server_Type=Physical)

10. Blank MODEL (if CI_Server_Type=Physical)

find_real_file.png

We have a common script include where we call to log the warnings and messages. Like below

// code for logging error messages
gs.include('data_load_logs_asset');
var scinc = new data_load_logs_asset();
var logmessage ='';
var topic ='';
var messagetype = '';

topic = serial;

After calling this we use if and else to log .

else {

    logmessage =logmessage + "*-*" + "Warning: UNSPSC "+itemId+" is not present for Hardware feed in the Data.";

    messagetype = messagetype + "*-*" + "Warning";

  }

1 ACCEPTED SOLUTION

Hi Ravi,



You need to use existing field map but also add transform scripts for for the validations. First transform script will validate the data and then the data will be transferred to target table with the help of field mappings.


Script seems to be okay... add below statement to add logs.


..


..


var num = source.u_cinum;


if(num ==""){



  ignore = true;


  logmessage =logmessage + "*-*" + "Warning: The cinum is blank.";


  messagetype = messagetype + "*-*" + "Warning";


log.info(logmessage );


}


View solution in original post

8 REPLIES 8

Gurpreet07
Mega Sage

Hi Ravishankar,



This should be possible using onBefore transform script.


http://wiki.servicenow.com/index.php?title=Transform_Map_Scripts#gsc.tab=0


Hi Gurpreet,



Please let me know whether I can write Transform script for already created direct field mapping .



Like for below CINUM , if it is Blank then do not create else we have to log it . Please suggest .


Hi Gurpreet,



Please suggest   , like for example can we use transform script for below ..



1. Blank CINUM


2. Blank or space in HOSTNAME



we already have field mappings created.


Hi Ravi,



You can write script for already existing transform map. From you screenshot below you need to switch tab to transform scripts and create new script. Change type to onBefore.


find_real_file.png



All fields from source table accessible using source.field name. Validate field value from source table one by one and logs as per validations.


Example from above link


Example:


var name = source.u_name.toString();
var info = "Before the row is transformed, " + name; log.info( info );  
// Make sure a company name has been provided
var company = source.u_company.toString();
if(company.length == 0 ){
ignore = true; info ="No company name, row ignored! " + name; log.info( info );
}