Record Producer Script is not functioning in Service Portal as it does in Native UI.

sanjay36
Tera Expert

Hi Community,

I've been at this for quite a while to figure out the cause.

I've built a Record Producer which is supposed to create a request IF AND ONLY IF the record submitted is valid. 

This Validation happens by using an external system so i have the REST API script set up in the Record producer script. 

So IF VALID , it posts the record and gets back the status as 201 or Not 201.

if 201 it should create the request , if not simply abort the action and show the error which got back from the NOT 201 response Message. 

The Record producer script works exactly as designed in Native UI, but it doesn't function in service portal. At least it does not even log the errors in service portal. For my requirement it is really necessary for aborting the action if not valid.

Please help me out on this.

Please find the script below : 

var recep_Empl_Id = '';
var recep_Ms_Id = '';
var newEmailRequested = producer.nmb_newInternetEmail;
var req_email = producer.nmb_email_to;
var processing_for = producer.nmb_processing_for;
var req_emp_id = producer.nmb_emp_id;
if(processing_for == 'Yourself'){
recep_Empl_Id = producer.nmb_emp_id;
}if(processing_for == 'Other Person'){
recep_Empl_Id = producer.nmb_other_emp_id;
}

if(producer.nmb_possible_msid !=''){
recep_Ms_Id = producer.nmb_possible_msid;
}

gs.info('EUTS NEW Mailbox Httpstatus' + newEmailRequested +'/n' + req_email + '/n' + recep_Empl_Id +'/n' + recep_Ms_Id + '/n' + req_emp_id);

var unik_guid='';
unik_guid = gs.generateGUID();
var r = new sn_ws.RESTMessageV2('x_opt_euts_intake.APS Requests', 'POST Outlook Exchange');
r.setEccCorrelator(unik_guid);

r.setStringParameterNoEscape('RecipientEmailAddress', newEmailRequested);
r.setStringParameterNoEscape('RequestorEmail', req_email);
r.setStringParameterNoEscape('RequestorEmployeePhone', '');
r.setStringParameterNoEscape('RecipientEmployeeId', recep_Empl_Id);
r.setStringParameterNoEscape('RecipientMSId', recep_Ms_Id);
r.setStringParameterNoEscape('ProductName', 'Exchange/Outlook Mailbox');
r.setStringParameterNoEscape('RequestorEmployeeId', req_emp_id);
r.setStringParameterNoEscape('Approved', '1');


r.setHttpTimeout(480);
var response = r.execute();
response.waitForResponse(480);

var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();
var httpStatus = response.getStatusCode().toString();
gs.info('EUTS NEW Mailbox Httpstatus' + httpStatus + 'unik_guid : '+ unik_guid);
gs.info('EUTS NEW Mailbox Httpstatus responseBody' + responseBody);

if (httpStatus == '201'){

//capture request ID from the response
var getPayload = new integrationHelper();
var payload = getPayload.getEccResponse(unik_guid,'');
var outputJson = payload.toString();

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(outputJson);
var node = xmlDoc.getNode('//output');
var ErrorOutputJson = node.getTextContent();
var Responseparsed = JSON.parse(ErrorOutputJson);
var requestid = Responseparsed.meta.attributes[0].value;

var requested_for = producer.nmb_requested_by.toString();

var gr = new GlideRecord("sc_request");
gr.initialize();
gr.requested_for = requested_for;
gr.short_description = 'New Mailbox Request';
var grid = gr.insert();

var grritm = new GlideRecord("sc_req_item");
grritm.initialize();
grritm.request = grid;
grritm.cat_item = '7824b573dbc15b00a8c9ab92ca961934';
var ritm_sys_id = grritm.insert();

//get all variable sys_ids
var grvariables = new GlideRecord("item_option_new");
grvariables.addEncodedQuery('cat_item=7824b573dbc15b00a8c9ab92ca961934^active=true');
grvariables.query();
while (grvariables.next()) {

var gritemOptionNew = new GlideRecord("sc_item_option");
gritemOptionNew.initialize();
gritemOptionNew.item_option_new = grvariables.sys_id;
gritemOptionNew.value = getItemOptionValue(grvariables.name);
gritemOptionNew.order = grvariables.order;
var itemOptionMtom = gritemOptionNew.insert();

var groptionMtom = new GlideRecord("sc_item_option_mtom");
groptionMtom.initialize();
groptionMtom.sc_item_option = itemOptionMtom;
groptionMtom.request_item = ritm_sys_id;
groptionMtom.insert();
}

//pass all the values to the record
if(requestid !=''){
current.external_request_id = requestid;
}
current.emp_id = recep_Empl_Id;
current.ms_id = recep_Ms_Id;
current.request_type = 'Individual Mailbox';
current.new_desired_email = newEmailRequested;
current.contact_email = producer.nmb_email_to;
current.requested_by = producer.nmb_requested_by;
current.approver = producer.nmb_owner_manager;
//current.requestor_phone = phone;
current.display_name = producer.nmb_new_Display_name;
current.requested_item = ritm_sys_id;

gs.addInfoMessage("Your request for a New Mailbox has been received - once the request is approved, you will be notified by email");
}else if (httpStatus != '201'){

var getPayload = new integrationHelper();
var payload = getPayload.getEccResponse(unik_guid,'');
var outputJson = payload.toString();
gs.info('EUTS NEW Mailbox Httpstatus inside !201' + outputJson);
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(outputJson);
var node = xmlDoc.getNode('//output');
var ErrorOutputJson = node.getTextContent();
var ParseResponse = JSON.parse(ErrorOutputJson);

if(ParseResponse.hasErrors == true){
gs.addErrorMessage(ParseResponse.errors[0].detail);
current.setAbortAction(true);
}

}
producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=7824b573dbc15b00a8c9ab92ca961934";

 

 

Thanks,

Sanjay

1 ACCEPTED SOLUTION

sanjay36
Tera Expert

I made it possible another way by creating a Catalog item instead of RP.

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sanjay,

So did you try to print the log for the API response what is coming. Also any error coming in system logs for this.

Also it would be best practice to push the API code to some script include so that the record producer script doesn't look bulky.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sanjay,

Any update on this?
Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

sanjay36
Tera Expert

I made it possible another way by creating a Catalog item instead of RP.