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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

How to Create a Hyperlink using a JS Trigger

tpatel
12-Amethyst

How to Create a Hyperlink using a JS Trigger

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;
 }
}

ACCEPTED SOLUTION

Accepted Solutions
awalsh
17-Peridot
(To:tpatel)

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"
}

View solution in original post

7 REPLIES 7
awalsh
17-Peridot
(To:tpatel)

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. 

 

 

tpatel
12-Amethyst
(To:awalsh)

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.

awalsh
17-Peridot
(To:tpatel)

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"
 }
tpatel
12-Amethyst
(To:awalsh)

The field is a Long Text Field. I noticed that when I put the link, a MKS HTML is added in between the components. This might be the reason why the hyperlink breaks but I don't know why it gets added. I used the exact same code that you have used excluding the "eb.print("New Value for field is: " + va);" portion.

 

awalsh
17-Peridot
(To:tpatel)

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"
}
tpatel
12-Amethyst
(To:awalsh)

I didn't realize that making it a rich text field caused the html tag to appear. I don't need rich content so I just removed that it and the html tag stopped appearing in the hyperlink. It link finally works. I was wondering if it was possible to set the hyperlink to a display link rather than showing the entire URL. For example, the display link would be "Click Here" and when the user clicks on it, it will lead them to the hyperlink that was produced by the script.

Thanks. 

awalsh
17-Peridot
(To:tpatel)

For that you would need the rich text field and you'd create the hyperlink with "Click Here" rather than the URL.
For the plain text fields, there's no way to do this.
Announcements


Top Tags