cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Is there a function to get an "item link" from script?

smccoy
1-Newbie

Is there a function to get an "item link" from script?

Hi,

We have got the email notification working and we would like to include an URL link to the item that triggered the sending of the email notificaiton within the email message. Is there a field we can use, or function that we can call, to get theURL link for an item? This feature exists in reports, but we have not found a field or function to duplicate that functionality in scripts.

If there isn't a functional available, could someone provide an example script of how to put build a URL link for an item?

Thanks...

-Sean

1 ACCEPTED SOLUTION

Accepted Solutions
mrump
14-Alexandrite
(To:smccoy)

Hi Sean,

I'm not if I got you right, but I can offer a javascript snippet used in my trigger scripts.

CustomSendMail2AssignedUser.js:

//--------------------------------------------------------------------------

function addText(text){

if (text != null){

msg.append(text);

}

else{

msg.append("<null>");

}

}

....

var eb = bsf.lookupBean("siEnvironmentBean");
var sb = bsf.lookupBean("imServerBean");
var params = bsf.lookupBean("parametersBean");
var delta = bsf.lookupBean("imIssueDeltaBean");

var host = new java.lang.String();
...

// the message that will be send in the mail
var msg = new java.lang.StringBuffer();

var subject = new java.lang.String("Assignment Notification for "+delta.getType()+" [" + delta.getID().toString() +"]");

.....

addText("\n\nWEB: http://");
addText(host);
addText(":7001/im/issues?selection=" );
addText( delta.getID().toString());

addText("\nGUI: url:integrity://");
addText(host);
addText(":7001/im/viewissue?selection=" );
addText( delta.getID().toString());

eb.sendMail(sender, recipient, subject.toString(), msg.toString())

HTH Matthias

View solution in original post

4 REPLIES 4
mrump
14-Alexandrite
(To:smccoy)

Hi Sean,

I'm not if I got you right, but I can offer a javascript snippet used in my trigger scripts.

CustomSendMail2AssignedUser.js:

//--------------------------------------------------------------------------

function addText(text){

if (text != null){

msg.append(text);

}

else{

msg.append("<null>");

}

}

....

var eb = bsf.lookupBean("siEnvironmentBean");
var sb = bsf.lookupBean("imServerBean");
var params = bsf.lookupBean("parametersBean");
var delta = bsf.lookupBean("imIssueDeltaBean");

var host = new java.lang.String();
...

// the message that will be send in the mail
var msg = new java.lang.StringBuffer();

var subject = new java.lang.String("Assignment Notification for "+delta.getType()+" [" + delta.getID().toString() +"]");

.....

addText("\n\nWEB: http://");
addText(host);
addText(":7001/im/issues?selection=" );
addText( delta.getID().toString());

addText("\nGUI: url:integrity://");
addText(host);
addText(":7001/im/viewissue?selection=" );
addText( delta.getID().toString());

eb.sendMail(sender, recipient, subject.toString(), msg.toString())

HTH Matthias

smccoy
1-Newbie
(To:mrump)

Hi Matthias,

You've understand what I'm asking and I get the example. Thanks for the info... I'll give this a try later on today.

-Sean

tketz
4-Participant
(To:smccoy)

Hi Sean,

If you want to use it as a custom button and you have Perl you can try this:

Copy Link to Clipboard (SI)

wperl -mWin32::Clipboard -e "Win32::Clipboard(qq{$ENV{MKSSI_HOST}:$ENV{MKSSI_PORT}/si/viewrevision?projectName=$ENV{MKSSI_MEMBER1_PROJECT}&selection=$ENV{MKSSI_MEMBER1}})"

Copy Link to Clipboard (IM)

wperl -mWin32::Clipboard -e "Win32::Clipboard(qq{$ENV{MKSSI_HOST}:$ENV{MKSSI_PORT}/im/viewissue?selection=$ENV{MKSSI_ISSUE0}})"

Best Regards,

Tobias

dpayne
1-Newbie
(To:smccoy)

Here was an example that I implemented. I trimmed it down to be less complex. Because of this it needs to be debugged in case I made a typo.

// <b>Integrity Manager POST-Event Trigger</b>
//

// This Trigger is designed to be run by a Rule, Schedule, or Manually with Arguments
//
// The idea is to send out an email to only 1 user per day with the body of the email detailing a list of items
// that have been modified or whatever for the previous day. The trigger will loop over all items that were
// triggered, store the users listed on each and build a unique list of users. It will also build an array on
// each user listing all of the items that reference them as the Created By or Modified By user. The trigger
// will then build a java mail object for that user with a table of items in the body and send it.
//*****************************************************************************************************************

/***********************************************************************************
* Script Setup *
***********************************************************************************/
var eb = bsf.lookupBean("siEnvironmentBean");
var sb = bsf.lookupBean("imServerBean");
var params = bsf.lookupBean("parametersBean");
var args = sb.getInvocationArguments();
var delta = false;

var host = { 'name' : eb.getDefaultAPIHostname(), 'port' : eb.getDefaultAPIPort(), 'url' : eb.getHostURL() };

var logging = true;
var isScheduled = false;
var Parameters = {};
var Items = {};
var Users = {};
/**********************************************************************************/


/***********************************************************************************
* Main Method *
***********************************************************************************/
function main ()
{
"use strict";

print("****************************************************************************************************");
print("main()");

var items = getItems(),
total = items.length;

for (var i = 0; i < total; i += 1)
{
var Id = items[i];
print("Item: " + Id);

Items[Id] = new Item(Id);
}

for (var user in Users)
{
print("Building mail for " + user);
Mail.buildMailObject(user);
}

print("Done");
print("****************************************************************************************************");
}
/**********************************************************************************/


/***********************************************************************************
* Get all items passed in from the trigger *
***********************************************************************************/
function getItems ()
{
"use strict";

var items = [];

if (args.getParameter("Id") !== undefined && args.getParameter("Id") !== null)
{
print("Trigger Type: Manual Invocation");
items.push(args.getParameter("Id"));
isScheduled = true;
}
else if (eb.getEventContext() == "Schedule")
{
print("Trigger Type: Schedule");
var stb = bsf.lookupBean("imScheduleTriggerArgumentsBean");
items = stb.getIssues();
isScheduled = true;
}
else
{
print("Trigger Type: Rule");
delta = bsf.lookupBean("imIssueDeltaBean");
items.push(delta.getID());
}

return items;
}
/**********************************************************************************/


/***********************************************************************************
* Item *
***********************************************************************************/
var Item = function (id)
{
"use strict";

this.Id = id;
this.Bean = (isScheduled)? sb.getIssueBean(id) : delta;
this.Type = String(this.Bean.getType());
this.State = String(this.Bean.getState());
this.Text = this.Bean.getFieldValue("Text");

this.CreatedDate = this.Bean.getCreatedDate();
this.CreatedBy = Util.getUserField(this.Bean.getFieldValue("Created By"));
this.ModifiedBy = Util.getUserField(this.Bean.getFieldValue("Modified By"));

this.getUsers();

};
Item.prototype.getUsers = function ()
{
"use strict";

var userFlds = { "CreatedBy" : this.CreatedBy ,
"ModifiedBy" : this.ModifiedBy };

for (var key in userFlds)
{
var total = userFlds[key].length;

for (var u = 0; u < total; u += 1)
{
var user = userFlds[key][u];
if (Users[user] === undefined)
Users[user] = new java.util.HashSet();

if (!Users[user].contains(this.Id))
Users[user].add(this.Id);
}
}

};
/**********************************************************************************/


/***********************************************************************************
* Mail Object *
***********************************************************************************/
var Mail =
{
FromAddress : false,
ToAddresses : false,
Subject : false,
Body : false,

buildMailObject : function (user)
{
"use strict";

this.FromAddress = new Packages.javax.mail.internet.InternetAddress(new java.lang.String("Integrity@imail.org"));
this.ToAddresses = this.buildToAddressList(user);
this.Subject = new java.lang.String("Integrity - Edited Item Notification");
this.Body = this.buildEmailBody(user);

if (this.ToAddresses != "")
this.sendMail();
},

buildToAddressList : function (user)
{
"use strict";

var userName = user;
var userBean = sb.getUserBean(userName);
if (userBean.getEmailAddress() == null)
{
var adminBean = sb.getUserBean("MKSAdmin");

eb.sendMail(adminBean.getEmailAddress(),
adminBean.getEmailAddress(),
"Invalid User Email Address",
"User " + userBean.getFullName() + "(" + userName + ") is attempting to send email in Integrity with out a valid email address.");

return "";
}
else
{
return new Packages.javax.mail.internet.InternetAddress(new java.lang.String(userBean.getEmailAddress()));
}
},

buildEmailBody : function (user)
{
"use strict";

var response = "<div style='font-family: Calibri, sans-serif'>" +
"<p>" +
"The below items were edited." +
"</p>";

var content = Util.convertSetToArray(Users[user]),
total = content.length;

if (total > 0)
{
response += "<table>" +
"<tr>" +
"<th>ID</th>" +
"<th>Type</th>" +
"<th>Text</th>" +
"<th>State</th>" +
"</tr>";

for (var i = 0; i < total; i += 1)
{
var Id = content[i].intValue();
response += "<tr>" +
"<td>" + Util.makeDocUrl(Id, Id) + "</td>" +
"<td>" + Items[Id].Type + "</td>" +
"<td>" + Items[Id].Text + "</td>" +
"<td>" + Items[Id].State + "</td>" +
"</tr>";
}

response += "</table>";
response = "<p>" + response + "</p>";
}

response += "<br />" +
"<hr>" +
"<p><b><i>Note: This is an automated message from the Integrity System. Do not reply to this email.</i></b></p>" +
"<p style='font-size: 70%'>" +
"The information in this e-mail message, including any attachments, is confidential. It is intended only for the named addressee and any use or disclosure by others is strictly prohibited. If you have received this message in error, please accept our apologies, notify us and be so kind as to delete all copies of the message." +
"Thank you." +
"</p></div>";

return response;
},

sendMail : function ()
{
"use strict";

var envprops = eb.getEnvironmentVariables();
var disableEmail = envprops.getProperty("disableEmail", "false");

if(disableEmail != null && "true".equalsIgnoreCase(disableEmail))
{
// email notifications are disabled
print("E-MAIL DISABLED: disableEmail property set in data/triggers/env.properties");
}
else
{
// email notifications are enabled
var jmail_props = new java.util.Properties();
jmail_props.put("mail.smtp.host", this.getSMTPServer());
jmail_props.put("mail.smtp.port", this.getSMTPPort());

// socket connection and I/O timeouts in milliseconds,
// set to five minutes
jmail_props.put("mail.smtp.connectiontimeout", 5*60*1000);
jmail_props.put("mail.smtp.timeout", 5*60*1000);

var session = Packages.javax.mail.Session.getInstance(jmail_props);
var html_msg = new Packages.javax.mail.internet.MimeMessage(session);
html_msg.setFrom(new Packages.javax.mail.internet.InternetAddress(this.FromAddress));
html_msg.setRecipients(Packages.javax.mail.Message.RecipientType.TO, this.ToAddresses);
html_msg.setSubject(this.Subject);
html_msg.setSentDate(new java.util.Date());

var htmlBody = new Packages.javax.mail.internet.MimeBodyPart();
htmlBody.setDataHandler(new Packages.javax.activation.DataHandler(this.Body, "text/html"));

var multiPart = new Packages.javax.mail.internet.MimeMultipart("alternative");
multiPart.addBodyPart(htmlBody);

html_msg.setContent(multiPart);

Packages.javax.mail.Transport.send(html_msg);
}
},

getSMTPServer : function ()
{
var smtpHostPort = eb.getMailHostnameAndPort();
var SMTPserver = smtpHostPort[0];

// Abort if none found, or return found value
if (SMTPserver == null || SMTPserver.length() == 0)
{
config( "'mksis.mailserver' property is not set in server configuration properties");
}
else
{
return SMTPserver;
}
},

getSMTPPort : function ()
{
var smtpHostPort = eb.getMailHostnameAndPort();
var SMTPport = smtpHostPort[1];

// Abort if none found, or return found value
if (SMTPport == null || SMTPport.length() == 0)
{
config( "'mksis.mailserver' property is not set in server configuration properties");
}
else
{
return SMTPport;
}
}
};
/**********************************************************************************/


/***********************************************************************************
* Utility Methods *
***********************************************************************************/
var Util =
{
convertSetToArray : function (set)
{
var newArray = new Array();
if(set.getClass().getName().equals("java.util.HashSet") ||
set.getClass().getName().equals("java.util.IntSet") ||
set.getClass().getName().equals("mks.util.IntSet"))
{
var it = set.iterator();
while(it.hasNext())
newArray.push(it.next());
}

return newArray;
},

getUserField : function (userField)
{
var response = [];

if(userField != null)
{
if(userField.getClass().getName().equals("java.util.HashSet"))
{
var it = userField.iterator();
while(it.hasNext())
{
response.push(it.next());
}
}
else
{
response.push(userField);
}
}

return response;
},

makeUrl : function (itemid, label)
{
return "<a href='integrity://" + host.name + ":" + host.port + "/im/viewissue?selection=" + itemid + "'>" + label + "</a>";
},

makeDocUrl : function (itemid, label)
{
return "<a href='integrity://" + host.name + ":" + host.port + "/im/viewsegment?selection=" + itemid + "'>" + label + "</a>";
}
};
/**********************************************************************************/


/***********************************************************************************
* Print Function *
***********************************************************************************/
function print(s)
{
"use strict";

if (logging)
{
Packages.mks.util.Logger.message("DEBUG", 1, eb.getScriptName() + " : " + s);
}
}
/**********************************************************************************/


/***********************************************************************************
* Abort Function *
***********************************************************************************/
function abort(s)
{
"use strict";

eb.abortScript(s, true);
}
/**********************************************************************************/

main();

Top Tags