Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Stop the entire transformation if condition is satisified

MaharshiC
Tera Contributor

Hi,

 

I want to stop the entire transformation if the conditions mentioned in the script is followed. So if there are 10 records and even for 1 record if the condition matches then no record should get added in the target table and the whole transformation should stop and alert should show. I have written the following script and i am getting the info message and going to the match=true   but it not stopping the transformation and records are getting created. This is onBefore transformation script

 

 

 

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

	// Add your code here
	gs.addInfoMessage("11Entire transformation will be ignored+"+source.sys_import_set);
	var match = false;
	var gr = new GlideRecord("x_iem_tqa_tqa_staging_request_items");
	gr.addQuery("sys_import_set", source.sys_import_set);
	
	gr.query();
	while (gr.next()){
		gs.addInfoMessage("22Entire transformation will be ignored");
		if(source.request_item_type == 'Other' ){
			match = true;
			break;
		}
	}
	
	if(match){
		ignore = true;
		gs.addInfoMessage("43Entire transformation will be ignored");
	}


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

 

 

 

 

1 ACCEPTED SOLUTION

untested, but the logic I would apply to this scenario 

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

    //Check if the imported data has a request_type of Other
	//and abort if so
    var hasOtherTypeGR = getBaseQuery(false);
    hasOtherTypeGR.addQuery('request_item_type', 'Other');
    hasOtherTypeGR.setLimit(1);
    hasOtherTypeGR.query();
    if (hasOtherTypeGR.hasNext()) {
        log.warn('Import Set contains a request item type of Other, aborting')
        ignore = true;
        return;
    }

	//Check if there is more than one request type
	//of the first row
    var importSetFirstRowValue = getBaseQuery(false);
    importSetFirstRowValue.orderBy('sys_import_row');
    importSetFirstRowValue.setLimit(1);
    importSetFirstRowValue.query();
    if (!importSetFirstRowValue.next()) {
        //this shouldn't happen
        log.error('No Rows to process');
        ignore = true;
    }

    var firstRowRequestTypeValue = importSetFirstRowValue.getValue('request_item_type');
    var result = getAggregateByItemType(firstRowRequestTypeValue);

    if (result > 1) {
        log.warn("More than one row found with type of " + firstRowRequestTypeValue);
        ignore = true;
        return count;
    }

	//helper functions

	
    function getBaseQuery(isAgg) {
        var queryMethod = isAgg ? GlideAggregate : GlideRecord;

        var importSetQuery = new queryMethod(import_set.getValue('table_name'));
        importSetRows.addQuery('sys_import_set', import_set.getUniqueValue());
        return importSetQuery;

    }

    function getAggregateByItemType(requestType) {

        var rowAgg = new getBaseQuery(true);
        rowAgg.addQuery('request_item_type', requestType);
        rowAgg.addAggregate('COUNT', 'request_item_type');
        rowAgg.query();
        if (!rowAgg.next())
            return 0;

        return rowAgg.getAggregate('COUNT', 'request_item_type');
    }

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

View solution in original post

17 REPLIES 17

SakshamSahu
Tera Contributor

Hi MaharshiC

 

You can use "map.ignore=true;" instead of just "ignore=true;" and then test.

 

map.ignore = true; → Stops the transformation for all records.

Kieran Anson
Kilo Patron

Hi,

Move to an onStart script if you want to evaluate before any row reads.

Hi @Kieran Anson ,

 

Thanks for your reply. I had tried the onStart script but then source.sys_import_set is giving undefined value.

Is x_iem_tqa_tqa_staging_request_items your import set table that extends import set row? The following can get you the current import set row identifer

//import_set is a GR of the current sys_import_set
import_set.getUniqueValue()