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 

 

I just checked you have marked number field as coalesce ? 

 

In this case it will always insert if it's not found it doesn't matters whether you have action insert ignored or not. 

 

I think you should keep things simple. Keep one transform map for update and one transform map for insert. 

 

If you making a field coalesce, it will always insert even if you restrict it, if the record is not found. So a part of the script becomes redundant here. 

 

Either you remove the coalesce things and mark it as false. 

 

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

@SAS21 can you tell your whole requirement once ?

 

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  need to insert the records with the data given. There are chances that BU might comeup with modifying the data for the existing records. Hence made the number field as coalesce and checking if the number is empty in the source file then insert else update. So wrote the script in the onBefore and using same  function for both insert and update. But there are few fields we have to auto populate certain fields based on other fields in the form .Hence running the Business rules. Because of this status and life cycle are setting as per Workflow/Br but need to update the status and lifecycle as provided in Source. Hence using the onafter script.