Home  >   FAQs   >  How to write a server script to set case SLA by created day?
FAQs in this section

How to write a server script to set case SLA by created day?

Table of Contents
Consider CompanyX provides marketing services to multiple organizations. If any organization has any issue with the service provided, they reach out to the support. For support also CompanyX uses CRM.

CompanyX is very concerned about their client and wants to make sure timely resolution is provided to their client. For that they use SLAs. But their support team works 24 x 5. If any client reports an issue over the weekend, SLAs are being violated.

To avoid that, they want to set the SLA for tickets created over the weekend to a different one having higher targets.

To achieve this, you must create a process for ticket creation and Server Script action. Check if createdtime is a weekend, then set SLA as Weekend SLA.

async function main(record, user) {
    //Create Date object from case created datetime
    var dateObj = new Date(record.createdtime);
    //Check if day number is 0 (Sunday) or 6 (Saturday)
    if(dateObj.getDay() === 0 || dateObj.getDay() === 6 || 1) {
        try {
            //Fetch weekend SLA id using rest api query operation
            //Note: SLA id can be hard coded as well instead of fetching
            //from api since id will not change once created. That will
            //avoid additional api request and reduce execution time
            var slaResult = await vtap.macro.ws.query("SELECT id FROM SLA WHERE policy_name = 'Weekend SLA' LIMIT 1;");
            if(slaResult && slaResult.success && slaResult.result && slaResult.result.length) {
                var wsSlaId = slaResult.result[0].id;
                var wsSlaIdParts = wsSlaId.split('x');
                var slaId = wsSlaIdParts[1];
                //Set slaid to record
                record.slaid = slaId;
        } catch(error) {
    return record;
Was this FAQ helpful?
0  out of  0  found this helpful.
Comments 0
Be the first to comment
© Copyright 2023 Vtiger. All rights reserved.