Servicely Administration
Scripting
Other API / Javascript Scripts
12 min
overview to support other areas and features in the platform, there are other features in the platform that you can make use of which have a very particular purpose as these are too small to have their own page, this page groups these items all together in one page markdown to html convertor to better support integrations with other systems, we have provided access to a in platform markdown to html convertor (and visa versa) although various platforms that use markdown use their own flavour of it, we wanted to ensure that this convertor catered for a large number of caters markdown to html let newmarkdownstring = markupconverter htmltomarkdown("\<h1>test\</h1>\<p>hello!\</p>") //this will return as the following //# test # // //hello! html to markdown let newhtmlstring = markupconverter markdowntohtml("# test # \n hello!"); //this will be return as the following //\<h1>test\</h1> //\<p>hello!\</p> extracting text out of html you can strip all html tags out of html to get a text newlines will be replaced by \n characters let extractedtext = htmlutils extracttext("\<h1>test\</h1>\<p>hello!\<br/>world!!\</p>"); // this will return // test // hello! // world!! creating attachment using base 64 string this server side scripting example can be used for inbound api purposes (on a system controllers docid pfjtclem zcu8ktp8zjw ) example inbound payload { "servicely inc number" "inc12345678", "data" { "mimetype" "image/png", "filename" "testimage png", "base64string" "string" // full attachment string } } example servicely controller script to create an attachment // below is if the attachment is meant to be linked to an incident record + attachment field name is "attachments" if (servicely inc number && data) { let increc = table("incident", servicely inc number); if (increc) { table("attachment") newrecord() mimetype(data mimetype) filename(data filename) data(data base64string) relatedfield("attachments") parentrecord(increc getid() + "\ incident") create(); } } creating attachment from an external url this server side scripting example can be used if servicely is to retrieve a file off a url and then add that file as an attachment to an incident let imageurl = "https //servicelyai wpenginepowered com/wp content/uploads/ai changing work jpg"; let servicelyfilename = "servicelyrobot png"; let servicelyincrec = table("incident", "75e8611c3cc211efbfec0621814b0941"); // replace with actual servicely record let attachmentfieldname = "attachments"; // replace with attachment field name you are using on servicely let responseobj = http get(imageurl) executebinary(); // creates the attachment off the downloaded file and set the values for the other fields on the attachment table at the same time responseobj toattachment({ relatedfield attachmentfieldname, parentrecord servicelyincrec, externalkey imageurl, filename servicelyfilename }); sending attachment out of servicely in base64 format // define the attachment record to synch outbound // let attachmentrec = let tokenname = "sometokenname"; // replace with servicely outbound token name that you have configured, to use to authenticate with the target system let targeturl = "https //someurl"; // replace with api endpoint of your choosing http post(targeturl) accept("application/json") contenttype(attachmentrec mimetype()) body(base64util encodebase64string(attachmentrec data())) apitokenauth(tokenname) execute(); sending attachment out of servicely in byte array format when an integration target system/environment needs attachments sent from servicely in byte array format // define the attachment record to synch outbound // let attachmentrec = let tokenname = "sometokenname"; // replace with servicely outbound token name that you have configured, to use to authenticate with the target system let targeturl = "https //someurl"; // replace with api endpoint of your choosing http post(targeturl) accept("application/json") contenttype(attachmentrec mimetype()) bodybytes(attachmentrec data()) apitokenauth(tokenname) execute(); checking a condition on a record an example is if you want to define a custom rule depending on if an incident record meets certain criteria one way to do that, you’ll need a custom table that have fields that include a table type and a condition type, similar to like in the report table screenshot below shows a portion of what the form layout for the custom table could look like then, to programmatically check if an incident record matches the condition above, you will need to run a server side script that makes use of a function called “matchescriterion” (note that it is applicable to a tablerecord variable) the following is a snippet of a sample script and expected result if it is run against the example above / say you have an incident record in a variable called increc defined before this line, and a record on the custom table that has fields like screenshot above, placed into a variable called customrec, defined before this line and the condition field on the custom table is named c condition / let incidentmatchescondition = increc matchescriterion(customrec c condition()) == true; log info(incidentmatchescondition); // this will output true if the incident record we are checking, has classification of "other" otherwise, the output will be false moving attachments from one field to another to help in moving attachments from one field to another (without querying the attachment table), there is a function that can be run on the server side to achieve this / move attachments from one field to another on a record where the from field is a multiple reference @param record the table record @param fromfield the from field, which may be a multiple reference @param tofield the to field, which may be a multiple reference @param attachmentids a list of ids of attachments in the from field, if it is a multiple reference / attachmentmanager moveattachments(record tablerecord, fromfield string, tofield string, attachmentids string\[])