transform map action delete update create

Jeff W NZAO B
Mega Guru

Hi,

I have a transform map to the core_company table, i modified my csv file to add a column i called action_type, i want to update, create or delete through the action defined. I created a script in the transform, but it doesn't work, can someone help me with the code please

(function transformEntry(source, target, map, log, isUpdate) {
  
  var action = source.u_action_type + '';

  if (action == 'ignore') {
    ignore = true;
    return;
  }

  if (action == 'delete') {
    var existing = new GlideRecord('core_company');
    
    
    if (target.u_name) {
      if (existing.get('u_name', target.u_name)) {
        existing.deleteRecord();
        log.info('Record deleted: ' + target.u_name);
      } else {
        log.warn('Record not found for deletion: ' + target.u_name);
      }
    } else {
      log.warn('u_name is not defined in target; cannot delete.');
    }
    
    ignore = true;
    return;
  }


})(source, target, map, log, isUpdate);
2 ACCEPTED SOLUTIONS

Bhavya11
Kilo Patron

Hi @Jeff W NZAO B ,

 

you can try below code 

create onBefore type transform script 

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	var action= source.u_action_type.toString().toLowerCase(); // u_action_type column name of csv

	var existingCompany = new GlideRecord("core_company");
	existingCompany.addQuery('name',source.u_name); // u_name column name 
	existingCompany.query();

	if(action =='insert'){
		if(existingCompany.next()){
			target.sys_id= existingCompany.sys_id;
		}
	}
	if(action =='delete'){
		if(existingCompany.next()){
			 gs.log('Record deleted: ' + target.name); // target.name is core_company table name field 
			existingCompany.deleteRecord();
			
		}
		 gs.log('Record not found for deletion: ' + target.name);// target.name is core_company table name field 
		igonre= true;
	}

})(source, map, log, target);

 

Please mark my answer correct & helpful, if it helps you

Thanks,
BK

View solution in original post

shantanu_patel8
Mega Guru

Hi @Jeff W NZAO B ,

 

From your description I can understand that you have 3 type of action they are insert, update or delete.

 

In the transform map you will need to create do the field mapping and making one of the field as unique. For example you can consider name as unique field. This will sort out your insert and update actions.

 

For Delete action, you can create a new "onbefore" transform map script and use the following code:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

// Add your code here
var action= source.u_action_type.toString().toLowerCase(); // u_action_type column name of csv
if(action =='delete'){
var existingCompany = new GlideRecord("core_company");
existingCompany.addQuery('name',source.u_name); // u_name column name
existingCompany.query();


    if(existingCompany.next()){
        gs.log('Record deleted: ' + target.name); // target.name is core_company table name field
        existingCompany.deleteRecord();
    }
    gs.log('Record not found for deletion: ' + target.name);// target.name is core_company table name field
    igonre= true;
}

})(source, map, log, target);

 

Please mark the answer helpful and correct if it helps the issue. Happy scripting 🙂

-Shantanu

View solution in original post

4 REPLIES 4

Bhavya11
Kilo Patron

Hi @Jeff W NZAO B ,

 

you can try below code 

create onBefore type transform script 

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	var action= source.u_action_type.toString().toLowerCase(); // u_action_type column name of csv

	var existingCompany = new GlideRecord("core_company");
	existingCompany.addQuery('name',source.u_name); // u_name column name 
	existingCompany.query();

	if(action =='insert'){
		if(existingCompany.next()){
			target.sys_id= existingCompany.sys_id;
		}
	}
	if(action =='delete'){
		if(existingCompany.next()){
			 gs.log('Record deleted: ' + target.name); // target.name is core_company table name field 
			existingCompany.deleteRecord();
			
		}
		 gs.log('Record not found for deletion: ' + target.name);// target.name is core_company table name field 
		igonre= true;
	}

})(source, map, log, target);

 

Please mark my answer correct & helpful, if it helps you

Thanks,
BK

Thanks, instead of deleting, i did a turn to false the state of the data. So i renamed the action_type to active_status, so i keep the data but it's said if it's active true or false

shantanu_patel8
Mega Guru

Hi @Jeff W NZAO B ,

 

From your description I can understand that you have 3 type of action they are insert, update or delete.

 

In the transform map you will need to create do the field mapping and making one of the field as unique. For example you can consider name as unique field. This will sort out your insert and update actions.

 

For Delete action, you can create a new "onbefore" transform map script and use the following code:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

// Add your code here
var action= source.u_action_type.toString().toLowerCase(); // u_action_type column name of csv
if(action =='delete'){
var existingCompany = new GlideRecord("core_company");
existingCompany.addQuery('name',source.u_name); // u_name column name
existingCompany.query();


    if(existingCompany.next()){
        gs.log('Record deleted: ' + target.name); // target.name is core_company table name field
        existingCompany.deleteRecord();
    }
    gs.log('Record not found for deletion: ' + target.name);// target.name is core_company table name field
    igonre= true;
}

})(source, map, log, target);

 

Please mark the answer helpful and correct if it helps the issue. Happy scripting 🙂

-Shantanu

Thanks, instead of deleting, i did a turn to false the state of the data. So i renamed the action type to active status, so i keep the data but it's said if it's active true or false