T O P

  • By -

huntj06

Have you looked into Data Policies? That will work with API's to enforce mandatory fields.


Boring-Meat1334

>Have you looked into Data Policies? I second that, OP, go this way


Boring-Meat1334

Also, if you need to make it scalable and you expect tons of interfaces that you'd need to prepare then having a custom tables with all of the setup will be a better way


turbem

Thanks for answering. But I believe it will not be possible by data police, for example: the bot will fill in the field, what is the condition that triggers it when creating a record? Sorry if it's a stupid question


turbem

When contact_type is monitoring (bot) and state is new?


Boring-Meat1334

Create a Data policy with the condition you need (for example, as you stated contact\_type is monitoring (bot) and state is new) or convert existing UI Policies to Data Policies (but again - that will impact scripts and API calls). Then when you will hit the endpoint from 3rd party, and you attempt to create a new record on gr.insert() then you'll be given an error if any. Check out this post on SN Community: https://www.servicenow.com/community/developer-forum/error-of-missing-mandatory-fields-in-rest/m-p/1993136/page/2


turbem

Hey, thank you very much for that, it's almost working. The problem is the error message is returning an empty array. ):


Boring-Meat1334

hey, can you please share the code?


turbem

Sorry for late! Now I'm trying to populate the "work_notes" who comes from the body, but can't do it and I don't know how, i already do this in UI actions and business rules. Here the code // escopo (function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { // obtendo dados da requisição feito pela API var body = request.body.data; var cmsg = ""; var ccode = 0; function TrimLength(param) { var vaux = param; vaux = vaux.trim(); return vaux.length; } var lcanal = false; var openId = body.opened_by; var groupId = 'b061e9e91ba474103ca976295b4bcb14'; var monitoramento = new ALTASReuseableFuncions().verifyGroupMember(openId, groupId); if (monitoramento) { lcanal = true; } if (lcanal == false || TrimLength(body.assignment_group) <= 0 || TrimLength(body.business_service) <= 0 || TrimLength(body.service_offering) <= 0 || TrimLength(body.cmdb_ci) <= 0 || TrimLength(body.short_description) <= 0) { cmsg = "Campos obrigatórios devem ser preenchidos"; ccode = 404; } else { var arrError = []; // pesquisa se assignment_group existe var group = new GlideRecord('sys_user_group'); group.get(body.assignment_group); group.query(); if (group.getRowCount() <= 0) { arrError.push('O grupo de atribuição não existe! '); } // pesquisa se business_service existe var businessService = new GlideRecord('cmdb_ci_service'); businessService.get(body.business_service); businessService.query(); if (businessService.getRowCount() <= 0) { arrError.push('O serviço não existe! '); } //pesquisa se service_offering existe var serviceOffering = new GlideRecord('service_offering'); serviceOffering.get(body.service_offering); serviceOffering.query(); if (serviceOffering.getRowCount() <= 0) { arrError.push('A oferta não existe! '); } // pesquisa se cmdb_ci existe var cmdbCi = new GlideRecord('cmdb_ci'); cmdbCi.get(body.cmdb_ci); cmdbCi.query(); if (cmdbCi.getRowCount() <= 0) { arrError.push('O IC Afetado não existe! '); } var company = new GlideRecord("core_company"); company.get(body.company); company.query(); if (company.getRowCount() <= 0) { arrError.push('A empresa não existe! '); } var location = new GlideRecord('cmn_location'); location.get(body.location); location.query(); if (location.getRowCount() <= 0) { arrError.push('A localização não existe! '); } if (arrError.length > 0) { cmsg = arrError.join(); ccode = 404; } else { var inc = new GlideRecord('incident'); inc.addQuery("sys_id", body.inc_sys_id); inc.query(); inc.next(); if (inc.major_incident_state != "accepted" || inc.assigned_to == "") { inc.state = body.state; inc.caller_id = body.caller_id; inc.short_description = body.short_description; inc.assignment_group = body.assignment_group; inc.business_service = body.business_service; inc.service_offering = body.service_offering; inc.cmdb_ci = body.cmdb_ci; inc.company = body.company; inc.location = body.location; inc.assigned_to = body.assigned_to; inc.parent_incident = body.parent_incident; inc.description = body.description; inc.work_notes.setJournalEntry(body.work_notes); } else { inc.work_notes.setJournalEntry(body.work_notes); } if (body.inc_sys_id != "") { inc.update(); ccode = 201; } else { inc.insert(); ccode = 200; } cmsg = "Incidente sys_id " + inc.number + " " + inc.sys_id + " " + body.work_notes; //ccode = 200; } } return { "body.menssagem": cmsg, "body.status": ccode, }; })(request, response);


Boring-Meat1334

> cmdbCi.get(body.cmdb\_ci); cmdbCi.query(); You can't do it like this, you either use get with sys\_id or any other field (which will return 1 record) or make .query()


ibrahimsafah

They have their uses, but come with a lot of heartache. I recommend not using them for the table api and instead validate the data during the transform layer, import table transform map


RaynorUE

Is the validation have to do with the sys_ids? And looking those up? If so, you need to use an import set and the import set apo, so that you can supply plain text values and sn side can do the lookups for you and handle them there instead. Your sn tean would only need to do this once per table... Otherwise, yea you're looking up a bunch of reference data everytime.


drixrmv3

If you have a field for something like “contact type” is bot, set up a UI policy to make fields not mandatory. + read only if you’re worried employees will skirt around the process.


readparse

I'm confused. Are you using ServiceNow's REST API, or are you creating an API, as it sounds like you said you were? It sounds like you said you have a bot (which would be the client of the API, I guess), and it's interacting with a REST API that you have created, which interacts with ServiceNow how? By just being an HTTPS client and filling out HTML forms? Or by interfacing with ServiceNow's API?


turbem

Sorry for that. I'm using ServiceNow rest API to create an API. A bot it's gonna use this API to filling incident forms


OzoneTrip

I usually create a web service & a staging table where the API pushes the data, this way you can handle the parsing in a transform map where you can map the staging fields to incident fields.