Queue

I have added a couple of new functions to my “Integration Helper” script include that I wanted to quickly discuss.

The first function below allows you to take the sysID of an output record on the ECC Queue. The method will then poll the ecc queue every second for a resulting input record to be created (up to a maximum of either 25 seconds, or a time you specifically pass to the method). When the result comes back, the payload string will be returned.

The second function, which is used by the first, will grab the correct payload from the ecc queue. One interesting thing to note is that the payload will either be a string in the ecc_queue.payload field, or, if it is large enough, it will be contained in an attachment on the ecc queue record. This function will return the correct string whether it be in the payload field or stored as an attachment.

Please be aware that this function may take up memory temporarily if the payload attachment is exceedingly large.

The first function, getEccResponse, uses the second function, getEccPayload, to return the correct payload string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
       /*
     * getEccResponse
     *
   * This method will take the sysID of an output record on the ECC queue
   * and wait up to a max number of MS for a corresponding "input"
   * response to be generated on the queue.
     *
     * PARAMS
     * outputSysId - The sys_id of the "output" record on the queue
     * waitMS - max number of miliseconds to to keep polling for the response.
     *
     * RETURNS
     * null - no response in within the max wait time;
     * Otherwise, we return the ECC Queue Payload string from the response record
     */

    getEccResponse: function(outputSysId,waitMS){
        if(!waitMS) {
            waitMS = 25000;
        }
        var start = new GlideDateTime;
       
       
        //Loop waiting for a record that is created in response to your query
        var resp = new GlideRecord("ecc_queue");
        resp.addQuery("response_to", outputSysId);
        resp.addQuery("queue", "input");
        do{
            resp.query();
            resp.next();
            gs.sleep(1000);
            if ( GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS ) {
                return null;
            }
        } while(!resp.sys_id);
       
        //Found a response
        return this.getEccPayload(resp);
       
    },
   
   
   
    /*
     * getEccPayload
   *
   * Large payload bodies will create an attachment on the ECC Queue record.
   * This function will automatically determine if the response is an attachment
   * or if it is plain text within the payload field.  It will return the full
   * payload string regardless of whether it is contained within the field or in
   * an attachment.
     *
     * PARAMS
     * ecc - The GlideRecord of the ECC Queue record
     *
     * RETURNS
     * Full payload string
   *
     */

    getEccPayload: function(ecc){
       
        if(ecc.payload != "<see_attachment/>"){
            return ecc.payload;
        }
       
        var SysAttachment = Packages.com.glide.ui.SysAttachment;
        var sa = new SysAttachment();
        var payload = sa.get(ecc, "payload");
       
        return payload;
    },