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

Hello @SAS21 

 

It's recommended to put direct values to avoid any confusion. 

 

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

Hello @SAS21 

 

If you do the direct assignment as I told in the message above, make sure to use gr.update(); 

 

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  and i noticed two records are inserting may be due to var newSysId = gr.insert();?

Hello @SAS21 

 

Okeh so now insert is happening. Yes, that's because of gr.insert.

 

But how will you get the sys_id in On Before. in that case, in OnAfter write another line if(action=='insert) ignore =true; because you have already inserted the record. Write this in another OnAfter script not the same one. But make the order of it to execute before this one. For example make this order as 50. And your CURRENT ON AFTER (the one discussed) as 100. 

 

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  after this total i see 3 records like below. two with the same number and one wih a new one. when i printed newSysID  - its empty

CGCSEM0066995
CGCSEM0066995
CGCSEM0066996