Integration between 2 instances

suuriyas
Tera Contributor

HI Community,

 

I have a requirement where in dev instances i have created a catalog item and once the request is raised in dev instances then a sla record need to be created in sandbox instances...based on variables (like name, type, target etc) in catalog item then the  record need to be created in sla def table

For this i have setup the integration between dev and sandbox.

created rest message in dev and written a script in BR.

Record is getting created but it is empty not sure what is that i'm missing 

rest message: just for testing i have used only some variables

suuriyas_0-1762874406748.png

suuriyas_1-1762874455443.png

After insert BR:

(function executeRule(current, previous /*null when async*/) {

    try {
gs.sleep(2000);
        var vars ={};
        var gr = new GlideRecord('sc_item_option_mtom');
        gr.addQuery('request_item', current.sys_id);
        gr.query();
        while (gr.next()){
            var opt = new GlideRecord('sc_item_option');
            if (opt.get(gr.sc_item_option)){
                vars [opt.item_option_new.name.toString()] = opt.value.toString();
           
            }
        }
        var slaName = vars['name'];
        var slaType = vars['type'];
        var startCondition = vars['start_condition'];

        gs.info('creating sla in target instances with values' + 'Name=' + slaName);
 var r = new sn_ws.RESTMessageV2('SLA Creation', 'Post');

r.setStringParameter('name',slaName);
r.setStringParameter('type',slaType);
r.setStringParameter('start_condition',startCondition);

gs.info('debug: sla payload values name' + slaName );
gs.info('Debug : request body' + r.getRequestBody());

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}
 /*try {
    gs.info('creating sla in target instances with values' + 'Name=' + name);
 var r = new sn_ws.RESTMessageV2('SLA Creation', 'Post');
 r.setStringParameterNoEscape('start_condition', current.start_condition);
 r.setStringParameterNoEscape('name', current.name);
 r.setStringParameterNoEscape('type', current.type);
 r.setStringParameterNoEscape('target', current.target);


 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}*/

})(current, previous);
 
Please let me know what im missing in it
Thanks in advance

 

 

3 REPLIES 3

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @suuriyas 

Based on your screenshots and script, the problem is most likely that you are not setting the target variable in your Business Rule.

Your REST Message's Content field (from the first image) defines a JSON payload that requires four values:

  • name

  • type

  • target

  • start_condition

Your Business Rule script successfully gets the values for name, type, and start_condition, but it never retrieves or sets the target variable. The target instance is likely receiving a JSON payload with a null value for target, causing it to create an empty or incomplete record.

Update this BR:

(function executeRule(current, previous /*null when async*/) {

    try {
        gs.sleep(2000); // Note: A sleep here is unusual, but I'll leave it as it was in your script.
        
        var vars = {};
        var gr = new GlideRecord('sc_item_option_mtom');
        gr.addQuery('request_item', current.sys_id);
        gr.query();
        
        while (gr.next()) {
            var opt = new GlideRecord('sc_item_option');
            if (opt.get(gr.sc_item_option)) {
                vars[opt.item_option_new.name.toString()] = opt.value.toString();
            }
        }
        
        // Get all your variable values
        var slaName = vars['name'];
        var slaType = vars['type'];
        var slaTarget = vars['target']; // <-- **1. ADDED THIS LINE**
        var startCondition = vars['start_condition'];

        gs.info('creating sla in target instances with values' + 'Name=' + slaName);
        
        var r = new sn_ws.RESTMessageV2('SLA Creation', 'Post');

        // Set ALL required parameters
        r.setStringParameter('name', slaName);
        r.setStringParameter('type', slaType);
        r.setStringParameter('target', slaTarget); // <-- **2. ADDED THIS LINE**
        r.setStringParameter('start_condition', startCondition);

        gs.info('debug: sla payload values name' + slaName);
        
        // This log is your most important debugging tool
        gs.info('Debug : request body' + r.getRequestBody()); 

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        
        gs.info('SLA Creation response status: ' + httpStatus);
        gs.info('SLA Creation response body: ' + responseBody);

    } catch (ex) {
        var message = ex.message;
        gs.error('SLA Creation BR Error: ' + message); // It's good practice to log errors
    }

})(current, previous);



Note: 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

@Deepak Shaerma ,

 

Thanks for replying

I tried your script by adding target but still it create empty record 

this is what i see in logs

In catalog item i have created all the variables similar to the sla def table.

suuriyas_1-1762877026627.png

var slaName = vars['name'];
        var slaType = vars['type'];
        var slaTarget = vars['target'];
        var startCondition = vars['start_condition'];
        gs.info('debug2 all vars found for item' + current.number +'=' + JSON.stringify(vars));
suuriyas_3-1762877152875.png

 

 

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @suuriyas 

 

My video will give you a better idea — even though it’s an interaction between ServiceNow and ServiceNow (SN–SN).

 

 

https://youtu.be/FSvzsprV494

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************