how to access the object(current) from eventqueue

amala2
Mega Guru

I have an eventqueue : gs.eventQueue("test",current,current); which is triggered via Business Rules.After which in a script action I am getting the parameters of event .So if I try to print the event.parm1 in script action ,I get [object GlideRecord].But after that If I try to get any field from the object ,I am getting "undefined".

My exact requirement is after passing the event.parm1 to script action ,I will call a script include in which I am accessing this object.

eg: gs.eventQueue("test",current,current);--->Business Rule

Script action         :

        var si= new testingnow(event.parm1);

        si.myfun();

Script include:

initialize: function(record) {

  this.record = record;

}

     

7 REPLIES 7

Tony's right.



The eventQueue() method signature is misleading, because even though you're passing current (or whatever other Glide Record you're messing around with) in as that second argument, it doesn't seem to be available past that. (If it is, I can't nail it down.)



So, you've got to do what Tony suggests: take the sys_id of the record, send it in as parm1, then on processing query the record with that sys_id.


Actually!   Scratch that.   I'm wrong.



Assuming that, in your business rule you say:


        gs.eventQueue("test",current,current);



Your second 'current' is, as others have said, a mistake (and should be a string), that first current (the second argument) is a GlideRecord or an argument that will produce a GlideRecord. When processing the Script Action that handles the event, it'll take or get that GlideRecord and put it into a variable called current.



In other words: we're overthinking this.   If you want the Incident that was sent into eventQueue in your Script Action, just current is already there.


James Bengel
Giga Expert

As others have alluded to, event.parm1 is defined as a string argument, so if you want to pass an object to it you would need to "stringify" the object when you queue the event, and parse it in your script to reconstitute it as an object.  One common use case is events that trigger notifications, and since I just had to do one of these, I'll use it to illustrate.

In this story I had to clone the Incident Create Knowledge business rule to set the author to the Resolved by field, and then send a notification to that user to complete the edit/publish workflow when the incident was closed. So in the business rile I have this function:

function submitDirect() {
    var kb = new GlideRecord("kb_knowledge");
    kb.source = current.sys_id;
    kb.short_description = current.short_description;
    kb.sys_domain = current.sys_domain;
    kb.text = current.comments.getHTMLValue();
    kb.workflow_state = 'draft';
	kb.kb_knowledge_base = gs.getProperty("glide.knowman.task_kb", "dfc19531bf2021003f07e2c1ac0739ab");
	kb.author = current.resolved_by; // STRY0012102
	kbSysId = kb.insert();
	if(kbSysId) {
        gs.addInfoMessage(gs.getMessage('Knowledge Article created: {0} based on closure of Incident: {1}', [kb.number, current.number]));
	// STRY0012102
        var eventParm1Obj = {};
        eventParm1Obj.kbsys_id = kbSysId;   // article_ID
        eventParm1Obj.kbNumber = gs.getMessage(kb.number); // display value of article
        event1Str = JSON.stringify(eventParm1Obj);
        gs.eventQueue('incident.nc_incident_kb', current, event1Str);
    }
}

Everything in the if() below the comment //STRY0012102 is what I added to do the job of passing the items I needed. (I'm not sure why I needed the gs.getMessage to get the display value to resolve, but it didn't work without it, so one of the Jedi will have to answer that one)

So now I have queue an event that is passing the following example as a string to the mail script that will eventually use it.

{"kbsys_id":"0d32c1bfdbaab3008f75c191159619ce","kbNumber":"KB0010753"}

Note: If you look at System Policy -> Events -> Event Log you can see everything that's in the event params, as well as some other useful information.

Then when I eventually get to my mail script later on, I can access the sys_id and number of the KB article, even though my notification is running against the Incident table, and I don't have to hit the server again to get that information. But befor eI can use it, I have to reconstitute my JSON object like this:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    // 'current' in this context is the Incident record, since that's what fired the event
    //gs.log('JAB: Entering mail script, event.parm1 = '+event.parm1); // for debugging only
    var kbObj = JSON.parse(event.parm1);
	// kbObj.kbsys_id = article ID for URL
        // kbObj.kbNumber = display value of article for subject and body
...

})(current, template, email, email_action, event);

Now my mail script has access to everything in event.parm1 as properties of the object kbObj. But because I want to know what was passed in, I can log the contents of the string in event.parm1 to the system log using gs.log().  (I always comment these out after I'm done testing because they really don't serve a purpose in production and they just generate another clal back to the server.) After the JSON.parse() call, I also have an object I can reference individual properties of.

Then later on when I need to set the email subject using the KB number I can do this:

var subjLine = gs.getMessage('Knowledge Article created: {0} based on closure of Incident: {1}', [kbObj.kbNumber, current.number]);
 
And when I want to create the link back to the article I can use kbObj.kbsys_id to build the URL.
 
And I can do all of that without having to query kb_knowledge to get the record for the article.