cannot get change record sys id when created from workflow

Saranya2
Tera Contributor

Hi All,

we have a catalog item, from there we will create change request once sctask is closed complete. I have written below code. It is creating change request, but i couldn't get sys id of the change record. I need to use the change record sys id in other activity. i am not sure what i have done mistakes. can anyone please help me for debugging.

also I have tried to get change number, I am not able to get it, it is returning as undefined.

 

var chg = new GlideRecord('change_request');
chg.newRecord();
chg.parent = current.sys_id;
if (current.variables.risk == "true")
    chg.risk = 2;

var ven_conn_type = current.variables.is_the_vendor_connectivity_type_checkpoint_client_based_vpn;

if (ven_conn_type == 'Yes') {
    var std_temp = new GlideRecord('std_change_record_producer');
    if (std_temp.get(gs.getProperty('fedex.firewall.sys_id.changetemplate1'))) {
        chg.std_change_producer_version = std_temp.current_version;
    }

chg.requested_by = current.request.requested_for;
chg.assigned_to = workflow.scratchpad.change_assignee;
if (chg.insert()) {
    var attachments = new GlideRecord('sys_attachment');
    if (attachments.get('table_sys_id', current.sys_id)) {      
        var attach = new GlideSysAttachment();
        attach.copy('sc_req_item', current.sys_id, 'change_request', chg.getUniqueValue());
              }
      chg.update();
}
workflow.scratchpad.parent_chgreq = chg.sys_id;
current.comments = "Change Request-" + chg.number + " has been created for " + current.number + ".";

 

Thanks,

Saranya

12 REPLIES 12

Chaitanya naram
Kilo Sage

Hi @Saranya2 write like this

workflow.scratchpad.parent_chgreq=chg.insert();

if(JSUtil.notNil(workflow.scratchpad.parent_chgreq)){

.......

......

}

And use initialise instead of new record of possible.

 

 

Thanks & Regards | Chiranjeevi Chaitanya Naram
Kindly mark the answer Correct and Helpful if it helps to resolve your issue.

Vishal Birajdar
Giga Sage

Hello @Saranya2 

 

Can you try like below.....

 

var changeSysId;
var ven_conn_type = current.variables.is_the_vendor_connectivity_type_checkpoint_client_based_vpn;

var chg = new GlideRecord('change_request');
chg.initialize();
chg.parent = current.sys_id;
if (current.variables.risk == "true"){
    chg.risk = 2;
}
if (ven_conn_type == 'Yes') {
    var std_temp = new GlideRecord('std_change_record_producer');
    if (std_temp.get(gs.getProperty('fedex.firewall.sys_id.changetemplate1'))) {
        chg.std_change_producer_version = std_temp.current_version;
    }

chg.requested_by = current.request.requested_for;
chg.assigned_to = workflow.scratchpad.change_assignee;
changeSysId = chg.insert();  // you will get the sysId of newly inserted record

if (changeSysId) {
    var attachments = new GlideRecord('sys_attachment');
    if (attachments.get('table_sys_id', current.sys_id)) {      
        var attach = new GlideSysAttachment();
        attach.copy('sc_req_item', current.sys_id, 'change_request', changeSysId);
              }

workflow.scratchpad.parent_chgreq = changeSysId; 
}

/*Glide record on change table & get number */
var changeNumber;
var grChange= new GlideRecord('change_request');
grChange.addQuery('sys_id', changeSysId);
grChange.query();
if(grChange.next()){
changeNumber= grChange.number;

}
current.comments = "Change Request-" + changeNumber + " has been created for " + current.number + ".";

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Aniket Chavan
Tera Sage
Tera Sage

Hello @Saranya2 ,

Please give a try to the script below and see how it works for you.

var changeSysId;
var changeNumber;

var chg = new GlideRecord('change_request');
chg.initialize();
chg.parent = current.sys_id;

if (current.variables.risk == "true") {
    chg.risk = 2;
}

var ven_conn_type = current.variables.is_the_vendor_connectivity_type_checkpoint_client_based_vpn;

if (ven_conn_type == 'Yes') {
    var std_temp = new GlideRecord('std_change_record_producer');
    if (std_temp.get(gs.getProperty('fedex.firewall.sys_id.changetemplate1'))) {
        chg.std_change_producer_version = std_temp.current_version;
    }
}

chg.requested_by = current.request.requested_for;
chg.assigned_to = workflow.scratchpad.change_assignee;

// Insert the record to get the sys_id
changeSysId = chg.insert();

if (JSUtil.notNil(changeSysId)) {
    var attachments = new GlideRecord('sys_attachment');
    if (attachments.get('table_sys_id', current.sys_id)) {      
        var attach = new GlideSysAttachment();
        attach.copy('sc_req_item', current.sys_id, 'change_request', changeSysId);
    }

    // Update the record after inserting and copying attachments
    chg.update();

    // Get the change number after inserting the record
    var grChange = new GlideRecord('change_request');
    grChange.addQuery('sys_id', changeSysId);
    grChange.query();
    
    if (grChange.next()) {
        changeNumber = grChange.number;
    }

    workflow.scratchpad.parent_chgreq = changeSysId;
    current.comments = "Change Request-" + changeNumber + " has been created for " + current.number + ".";
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket