Skip to main content
1-Visitor
November 7, 2018
Solved

How to Create a Hyperlink using a JS Trigger

  • November 7, 2018
  • 1 reply
  • 5505 views

Hi,

 

I am trying to create a trigger that will take in a value from a field and convert it into a hyperlink and then send it back to the field in Integrity. For example, if a user inputted the value "Tree" into the integrity field, the trigger will take that value and convert it into a hyperlink for the following website: google.com/Tree. It will take this hyperlink and it will replace the value of the field to the newly created hyperlink. Similarly, if a user was to input the value "cat". It will create a hyperlink to the website: google.com/cat and replace the old field value with this hyperlink.

 

Below, I have included a code that I have so far. It is able to take in the value from a field in Integrity and it will change the value into the link and send it back to that field. The only thing it is missing is creating the hyperlink itself so when I click on it, it will take me to this URL.  

 

 

function main()

{

var value = delta.getNewFieldValue("Testing for trigger"); // pull value from field called "Testing for trigger"
    if (value != null)  // Run when the field is not empty 

{
 var va = "https://www.google.ca/" + assignee; // adds the field value to the end of the line 
 delta.setFieldValue("Testing for trigger",va); // send the value of va back to the field "Testing for trigger"
 return;
 }
}

Best answer by awalsh

That does explain it. It's a rich text field, which has html tags. The field will always have "<!-- MKS HTML -->" at the beginning, and any link needs to be added manually.

 

I used the function from another thread: https://community.ptc.com/t5/Integrity/Converting-MKS-HTML-in-Standard-HTML-or-Plain-Text/m-p/496872 to strip the html tags from the text, then added "<!-- MKS HTML -->" and html to create the link. The first print statement gives:

Trigger "link.js" - Value for field is: <!-- MKS HTML -->CS83670 Value for field with no html is: CS83670

 

function killTags(string) {
var re1 = new RegExp('<br[^>]*>','g');
var re2 = new RegExp('<p[^>]*>', 'g');
var re3 = new RegExp('<[^>]*>', 'g');
return ('' + string).replace(re1,'\n').replace(re2,'\n').replace(re3,'');
}


var eb = bsf.lookupBean("siEnvironmentBean");
var delta = bsf.lookupBean("imIssueDeltaBean");
eb.setMessageCategory("TRIGGERSCRIPT");

var valuehtml = delta.getNewFieldValue("Description"); // pull value from field called "Testing for trigger"
var value = killTags(valuehtml);
eb.print("Value for field is: " + valuehtml + " Value for field with no html is: " + value);

if (value != null) // Run when the field is not empty
{
var va = "<!-- MKS HTML --><a href=https://www.ptc.com/en/support/article?n=" + value
+ ">https://www.ptc.com/en/support/article?n=" + value + "</a>"; // adds the field value to the end of the line
eb.print("New Value for field is: " + va);
delta.setFieldValue("Description",va); // send the value of va back to the field "Testing for trigger"
}

1 reply

5-Regular Member
November 7, 2018

For a short text field or a long plain text field, then the URL is made a link automatically when viewing the item in GUI or Web.  I tested with your script on my system (using a different URL), and here's what I saw after saving the item:

textlinks.PNG

What version are you using? I can test with that version as well. 

 

 

tpatel1-VisitorAuthor
1-Visitor
November 7, 2018

I am using Integrity version 11.2. I found that the initial potion of the URL acts as a hyperlink but the portion that is added to through the user inputted field does not. For Example: www.google.com will by hyperlinked but not the user entered field "cat".

Thanks.

5-Regular Member
November 7, 2018

That's interesting. What kind of field is it?
What's the exact text in the field after you edit it?

 

Here's the code I used. I only added a couple print lines and changed the URL and the field name. I've run it on 12.0 and 11.2 CPS01

var eb = bsf.lookupBean("siEnvironmentBean");
var delta = bsf.lookupBean("imIssueDeltaBean");
eb.setMessageCategory("TRIGGERSCRIPT");

var value = delta.getNewFieldValue("Test_ShortText"); // pull value from field called "Testing for trigger"
eb.print("Value for field is: " + value);

 if (value != null) // Run when the field is not empty 
{
		var va = "https://www.ptc.com/en/support/article?n=" + value; // adds the field value to the end of the line 
		eb.print("New Value for field is: " + va);
		delta.setFieldValue("Test_ShortText",va); // send the value of va back to the field "Testing for trigger"
 }