Sys_id of ticket where the action originated

w_bouwer
Mega Expert

Hi,

I'm looking for an easy way to find the sys_id of the record where the action originated from, from a before insert business rule.

I know this works:

var x = gs.action.getGlideURI().toString();


But that requires me to write a indexOf check to cut the 'sysparm_sys_id' out of there. Plus I am not 100% sure on how these glideURI's are created, and therefore not whether my indexOf will always yield the correct result.

If anyone can help me on this, I'd be much obliged.

Wesley
11 REPLIES 11

mdwallick
Giga Contributor

Hmm...now I'm curious.

Where is the email_client macro XML you're dealing with? I can't seem to find it in my training instance.

There has got to be a better way to do what you want without parsing a URI for a sys_id.


It's server side (a literal xml doing the processing). As a rule of thumb any '.do' that is no macro or page, is an xml. Invisible to us mere mortals 🙂

Wesley


mdwallick
Giga Contributor

Got it!

The email client window creates a skeleton sys_email record as soon as the client window opens, which is why a before insert business rule doesn't work. Once you click send on the email, the sys_email record is updated, which sends out the email.

So...use a before update business rule like this:

Condition: current.type.changesTo('send-ready') && current.target_table == 'incident'



// copy the attachments from the incident to this sys_email record
Packages.com.glide.ui.SysAttachment.copy(current.target_table, current.instance,
current.getTableName(), current.sys_id);


Works like a charm in my instance.


Using this method, we are unable to see the attachments in the email client. As they are not picked up by the email_client handler at that time. And this is what I'm trying to achieve.

I've written the following before insert business rule:



try {checkActionObject();} catch (err) {} // verify whether the action exists, deny everything that is an error.
function checkActionObject(){
var strGlideURISysId = gs.action.getGlideURI().toString(); // get the URI object or break
var strGlideURITable = strGlideURISysId; // using substr, so need second variable
var strSysId = strGlideURISysId.substr(strGlideURISysId.indexOf('sysparm_sys_id=') + 15, 32); // get the sys_id of the originating record
var strTable = strGlideURITable.substr(strGlideURITable.indexOf('sysparm_table=') + 14, strGlideURITable.length()); // get the table of the originating record
strTable = strTable.substr(0, strTable.indexOf('&')); // properly normalizing it as parameter
addAttachment(strTable, strSysId); // use these to add attachment
}
function addAttachment(table, sys_id) {
Packages.com.glide.ui.SysAttachment.copy(table, sys_id, 'sys_email', current.sys_id); // Use the package to copy variables to the email record.
}


Now this works but is not very nice, nor very 'safe'. I'd like a cleaner method for this.

On another note there is an added issue with the email_client not refreshing the attachments when removed. I'm not quite sure why this occurs, but it seems to be related to the way they were added to the form.

If you have a solution for either of the issues, I'm more than willing to hear it.

Thanks in advance!

Wesley


mdwallick
Giga Contributor

Boy, just when I thought I had it...

Here is a safer way to get values from the GlideURI without any parsing nastiness.



// get the hasmap from the GlideURI
var map = gs.action.getGlideURI().getMap();
var table = map.get('sysparm_table');
var sid = map.get('sysparm_sys_id');

if (table == 'incident' && sid != '') {
// copy the attachments from the "source" incident
Packages.com.glide.ui.SysAttachment.copy(table, sid, current.getTableName(), current.sys_id);
}


Now I'm just determined to get you what you want 🙂 Does that do it? There's probably some more validation that could be done...