Question on scripted rest api order guide mandatory variables validation

Mani60
Tera Contributor

Hi All,

I want to make order guide mandatory variables validation in my scripted rest API,
Scenario: Whenever anyone tried to hit the api at that time if that is an mandatory variable and it is coming without any value then it should throw an error message and it should not allow create a request throw my scripted api.
Please find the below code which i am using :

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

       
    var msg = request.body.data;
var reqFor=  new ceredianUtility().getRequestorSysid(request.body.data.requestor);
var officergroup = request.body.data.officer_group_approval.split("*");

        var officergroupsysid = [];
        officergroupsysid = new ceredianUtility().getGroupSysid(officergroup);
        var chathamgroup = request.body.data.chatham.split("*");

        var chathamsysid = [];
        chathamsysid = new ceredianUtility().getChathamSysid(chathamgroup);
        var listgroup = request.body.data.group.split("*");

        var listgroupsysid = [];
        listgroupsysid = new ceredianUtility().getGrouSysid(listgroup);
        var profilegroup = request.body.data.profile.split("*");
        var profilegroupsysid = [];
        profilegroupsysid = new ceredianUtility().getProfileSysid(profilegroup);
        var regiongroup = request.body.data.region.split("*");
        var regiongroupsysid = [];
        regiongroupsysid = new ceredianUtility().getRegionSysid(regiongroup);
        var salesforcegroup = request.body.data.salesforce.split("*");
        var salesforcegroupsysid = [];
        salesforcegroupsysid = new ceredianUtility().getSalesforceSysid(salesforcegroup);

       

       
       
    var json= {"opened_by": "d6e6c818870f351058f3202c8bbb3578","requested_for" : reqFor,'officer_group_approval': officergroupsysid,'chatham': chathamsysid,'group':listgroupsysid,'profile':profilegroupsysid,'region':regiongroupsysid,'salesforce':salesforcegroupsysid};
for (var property in msg) {
    if(property!='officer_group_approval'&& property!='chatham'&& property!='group'&& property!='profile'&& property!='region'&& property!='salesforce'){
   
                json[property] = msg[property];
    }
   
}
var cat = new GlideRecord('sc_cat_item_guide');
if (cat.get('name', 'New Hire Request(WIP)')) {
  var sog = new SNC.ScriptableOrderGuide(cat.getValue("sys_id"));
  var result = sog.process(new JSON().encode(json));
  if(!result)
    gs.log("Processing the scriptable order guide failed with message: " + sog.getMessage());
  else {
    var request = sog.getRequest();
    gs.log("Request created - " + request.sys_id); } }

    response.setBody({
            result: 'Catalog Item request' + request.number + 'created.'

        });

})(request, response);


Could you please help me on this to get correct solution.


Thanks,

0 REPLIES 0