The CreatorCon Call for Content is officially open! Get started here.

Need to get the Sys ID of the inserted record from onBefore transform Script

SAS21
Tera Guru

Before inserting,  the source fields are populating in target record but since the need of the running business rules the fields on the form (Status and lifecycle) are auto setting. Hence trying to get the SysID of the inserted record in Onbefore transform script and need to use that in OnAfter script to overwrite the  Status and lifecycle with the Source file fields. But no luck.

Here is the script

Inserting the record if number(coalesce ) field is empty 

OnBefore

 

var number = source.u_number;
    var gr = new GlideRecord('x_cg_service_request_request');
    if (gs.nil(number)) {
        action == 'insert';
        ignore = false;
        mapSRFields(source, gr);
    } else {
        if (gr.get('srnumber', number)) {
            action = 'update';
            ignore = false;
            mapSRFields(source, gr);
        } else {
            ignore = true;
        }
    }
    if (!isUpdate) {
        map.put('newRecordSysId', target.sys_id);
    }
 
function mapSRFields(source, gr) {
        if (source.u_requestor != '') {
            gr.caller.setDisplayValue(source.u_requestor);
        }
        if (source.u_manager != '') {
            gr.x_cg_bu_manager_name.setDisplayValue(source.u_third_party__onship_manager);
        }
target = gr;
 
 
On After:
 
var newRecordSysId = map.get('newRecordSysId');
    gs.info('newRecordSysId' + newRecordSysId);
    if (newRecordSysId) {
        var gr = new GlideRecord(map.getTargetTable());
        if (gr.get(newRecordSysId)) {
            if (source.u_life_cycle != '') {
                gr.x_cg_life_cycle_stage.setDisplayValue(source.u_life_cycle_stage);
            }
            if (source.u_status != '') {
                gr.state.setDisplayValue(source.u_status);
            }
            gr.update();
        } else {
            log.warn("Could not find record with newRecordSysId " + newRecordSysId);
        }
    } else {
        log.warn("newRecordSysId is empty");
    }

 

 

12 REPLIES 12

Shivalika
Mega Sage
Mega Sage

Hello @SAS21 

 

You cannot get the sys_id in onBefore by default because no insertion has happened. It just means before transformation. It is still sitting in import set table. 

 

You need to explicitly use gr.insert to insert and get sys_id. 

 

Also in On Before script you have written 'action == insert.It should be action = insert ; 

 

Try below script --- 

 

On Before ----- 

 

var number = source.u_number;

var gr = new GlideRecord('x_cg_service_request_request');

 

if (gs.nil(number)) {

    action = 'insert'; // Corrected assignment

    ignore = false;

    mapSRFields(source, gr);

    var newSysId = gr.insert(); // Insert record and get sys_id

    map.put('newRecordSysId', newSysId); // Store sys_id in map

} else {

    if (gr.get('srnumber', number)) {

        action = 'update';

        ignore = false;

        mapSRFields(source, gr);

        gr.update();

    } else {

        ignore = true;

    }

}

 

function mapSRFields(source, gr) {

    if (!gs.nil(source.u_requestor)) {

        gr.caller.setDisplayValue(source.u_requestor);

    }

    if (!gs.nil(source.u_manager)) {

        gr.x_cg_bu_manager_name.setDisplayValue(source.u_third_party__onship_manager);

    }

}

 

ON After 

 

var newRecordSysId = map.get('newRecordSysId');

gs.info('newRecordSysId: ' + newRecordSysId);

 

if (!gs.nil(newRecordSysId)) {

    var gr = new GlideRecord(map.getTargetTable());

    if (gr.get(newRecordSysId)) {

        if (!gs.nil(source.u_life_cycle)) {

            gr.x_cg_life_cycle_stage.setDisplayValue(source.u_life_cycle_stage);

        }

        if (!gs.nil(source.u_status)) {

            gr.state.setDisplayValue(source.u_status);

        }

        gr.update();

    } else {

        gs.warn("Could not find record with newRecordSysId: " + newRecordSysId);

    }

} else {

    gs.warn("newRecordSysId is empty");

}

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

@Shivalika Thank you for replying. Tried with the one you have given but still not overwriting the values it is intended to 

Hello @SAS21 

 

Another problem is why are you using set displayValue everywhere ? 

 

Even if it is a reference field as long as you pass the correct front end values it will take it. 

 

Please replace it everywhere with 

setValue(); 

 

Or best way is direct assignment, gr.(Fieldname) = Source.YOURIMPORTFIELD. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

@Shivalika  for requester , requesteronbehalf etc email ids were provided and have choice type fields as well. Need to fetch the names of the user instead of mapping email ids. Hence using setDisplayValue