function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFC7SFC7 

Trying to Create Hyperlink in a trigger

Based on the business requirement I have to make text as a link in trigger and then I wanna retrieve/print that value in a standard page.

 

eg. string link = '<a href=/'+ accId +'>'+accName+ '</a>'

 

Please share your thoughts.

Best Answer chosen by SFC7
SFC7SFC7
i supposed to close this ticket long back as i got achieve!, Thanks to all!

All Answers

Bhawani SharmaBhawani Sharma
you can create a formula field with below formula
HYPERLINK('/{!accountId}', {!account.Name}),{})
SFC7SFC7

Thanks for your chain reply!

As i said i wanna make it through a trigger(After insert) because of auto populate in a standard salesforce VF page.

Tejpal KumawatTejpal Kumawat

Hi,

 

Yes, You can write trigger :

 

Firstly Create Custom field on Account Object : Trigger_Populate_Link__c [Rich Text Area(256)]

 

After that write trigger like :

 

trigger accountTrigger on Account (before insert, before update) {
if(Trigger.isBefore && (Trigger.isUpdate || Trigger.isInsert)){
for(Account a : trigger.new){
a.Trigger_Populate_Link__c = '<a href=/'+ a.Id +'>'+ a.Name+ '</a>';
}
}
}

 

SFC7SFC7

Thank you, I tried on this way and the result is just weird.

 

It displayed <a href=/AccounID>Account Name</a>.

 

Thank you in advance.

SFC7SFC7

I appreciate if anyone have any thoughts on this.

Bhawani SharmaBhawani Sharma
I will go again with my first suggestion.
1. Create a field and hold account Id in it using your current trigger.
2. Create a field and hold account Name in it using your current trigger.
3. Create a new formula field with formula :
HYPERLINK('/{!accountId}', {!account.Name}),{})

You can hide, first 2 fields from page layout if not required.
SFC7SFC7

Thanks for your suggestion again, but here is what I'm looking for

 

e.g., ac.link__c = <a href=/'+ a.Id1 +'>'+ a.Name1+ '</a> \n <a href=/'+ a.Id2 +'>'+ a.Name2+ '</a>

 

I want to display those href links(more than one) in a standard page.

Saurabh DhobleSaurabh Dhoble

Try this -

 

ac.link__c = '<a href=''/'+ a.Id1 +'''>'+ a.Name1+ '</a>' + '\r\n' + '<a href=/'''+ a.Id2 +'''>'+ a.Name2+ '</a>

 

I have highlighted the changes in red.

SFC7SFC7

Thank you...I did all the way that you mentioned and it displayed <a href=/a.Id>a.Name1</a>

                                                                                                                   <a href=/a.Id2>a.Name2</a>

instead of links.

 

Thank you in advance.

Saurabh DhobleSaurabh Dhoble

You need to change your field to type "Rich Text Area" in order to display hyperlinks - try creating a new rich text area field and populating it like this in the trigger. That should show up as hyperlinks.

SFC7SFC7

Amego, You are thinking same as me :)...i did this too

Saurabh DhobleSaurabh Dhoble
So did it work ?
SFC7SFC7

nope!

Saurabh DhobleSaurabh Dhoble

I don't understand - I got this code to work in my org. Basically I created a trigger on the Account object, that was putting the HREF in a rich text field (rich text field, not long text field).

 

Are you trying to use the ID field in a "before insert" trigger ? If that is so, your href will be NULL, because the ID field is null before the object is actually created.

 

trigger AccountTrigger on Account (before insert) {
	List<Account> aList = Trigger.New;
    Account a = aList[0];
//test__c is the rich text field. a.test__c = '<a href=/' + '001E000000KC75q' + '>Google</a>'; }

 

Tejpal KumawatTejpal Kumawat

Hi,

 

I have tried this way it is working correctly..

 

Firstly Create Custom field on Account Object : Trigger_Populate_Link__c [Rich Text Area(256)]

 

After that write trigger like :

 

trigger accountTrigger on Account (before insert, before update) {
if(Trigger.isBefore && (Trigger.isUpdate || Trigger.isInsert)){
for(Account a : trigger.new){
a.Trigger_Populate_Link__c = '<a href=/'+ a.Id +'>'+ a.Name+ '</a>';
}
}
}

 

Is your senerio is different or you are writing trigger on different object... Please provide details?

SFC7SFC7
Thank you....I have tried on this way and it wont allow me with
links...btw, i achieved this on a different scenario.

Thanks to all folks :)
Anton YoganathanAnton Yoganathan
Hi, Can you please let me know how have you achived this.
 even i am searching for the method to Create Hyperlink in a trigger.
Tarun Khandelwal 11Tarun Khandelwal 11

I was having the same issue.

What i found is that you need to give full url, with hostname in href.

With hostname (http://google.com)
Without hostname (google.com)

Tarun Khandelwal 11Tarun Khandelwal 11
If you see the link address for both link, it's different.
Tarun Khandelwal 11Tarun Khandelwal 11
From hostname i meant protocol.
SFC7SFC7
i supposed to close this ticket long back as i got achieve!, Thanks to all!
This was selected as the best answer
SadhuSadhu
Thanks alot #SaurabhDhoble...

Your good suggestion worked for me...

Thanks again... :)
rajesh cherukuri 1rajesh cherukuri 1

@sfc7

what is the solution. I have the same requirement.