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.

Onbefore Transform map not updating the record, only inserting even though matches with theCondition

Community Alums
Not applicable

 

Following is my field map

PradeepPatel1_1-1719862773395.png

following is onbefore script

here I am checking if entity and current_ram combination exits, if yes then update the record.

I am mapping ram with the expected ram

PradeepPatel1_2-1719862981532.png

if combination does not exits then create new record

PradeepPatel1_3-1719863117694.png

for reference this is the table and following is the form

PradeepPatel1_4-1719863179173.png

 

Following is the script

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    var up = new GlideRecord("sn_risk_advanced_risk_assessment_scope");
    up.addEncodedQuery("entity.nameSTARTSWITH"+ source.u_entity + "^risk_assessment_methodology.nameSTARTSWITH" + source.u_current_ram);
    up.query();
    if(up.next())
    {
        action = 'update';
    }
    else{
        ignore = true;
    }
})(source, map, log, target);

 

 

6 REPLIES 6

try using this script"

var entityName = source.u_entity;
var currentRAM = source.u_current_ram;

// Create a new GlideRecord instance for the target table
var up = new GlideRecord("sn_risk_advanced_risk_assessment_scope");

// Add query to find records where entity name and risk assessment methodology match
up.addEncodedQuery("entity.nameSTARTSWITH" + entityName + "^risk_assessment_methodology.nameSTARTSWITH" + currentRAM);
up.query();

// If the record exists, update it
if (up.next()) {
    // Map the existing record fields to the target object to perform the update
    target.initialize();
    for (var field in source) {
        if (source.hasOwnProperty(field) && target.isValidField(field)) {
            target[field] = source[field];
        }
    }
    target.setWorkflow(false); // Disable business rules and workflows
    target.autoSysFields(false); // Prevent automatic updating of sys fields like sys_updated_by and sys_updated_on
    target.update(); // Save the changes

    action == 'update';
}
// Else, ignore the record creation
else {
    ignore = true;
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 


Community Alums
Not applicable

Hi @Deepak Shaerma ,

 

target.update I can not use, they are having their best practice set while saving the code, throws error message target.update can not be used in onbefore