0

There is the following custom field called Url__c of type Account I am using and the data added to the field is mainly urls and websites. The problem is that the field is rich text and the data entered in the field Url__c is not the proper format. What I mean by not a proper format is:

<span style="color: #5f604b;">
  <span style="font: signacondcolumn-book;">
    <span style="font-size:8.0pt;">https://www.merchant.com/store</span>
  </span>
</span><br>
<a href'="www.account.com">Account</a>

I'll need your advise what can be done in order to be able to get the urls in the proper format. What I would like to know is how to get beginning and the end of the url. For instance I need to get either the 'http', 'https' or 'www' and the end of the url.

The end of the url turned out to be more difficutl to get because in table Account in the field Url__c is rich text and for the added text there is no certain criterium on how to know for certain where the ulr will fininsh. Therfore I've decided to use regular expressions which should give me the url of the added text. The other solution to the problem is to change the field type from rich text to url but the problem is that the url field type is restricted in terms of characters or symbols and it does not allow to enter many web adresses.

Could you please advise an appropriate approach on how to get the urls out the added text in the Url__c field

Have you used similar approach with regular expressions and if you have could you please send examples.

Regards,

Dilyan

1 Answer 1

2

Regular expression to get all urls

(?:(?:(?:[a-z]{3,9}:(?://)?)(?:[-;:&=+$,w]+@)?[a-z0-9.-]+|(?:www.|[-;:&=+$??,w]+@)[a-z0-9.-]+)((?:/[+~%/.w-]*)?\\??(?:[-+=&;%@.w]*)#?w*)?)

Option 1

<span style="color: #5f604b;">
<span style="font: signacondcolumn-book;">
<span style="font-size: 8.0pt;">https://www.merchant.com/store</span></span></span><br>
<a href'="www.account.com">Account</a>

Use string stripHtmlTags(htmlInput) method

List<Account> lst = [SELECT Id, Url__c FROM Account WHERE Url__c != null];

for(Account obj: lst)
{
   obj.Url__c = obj.Url__c.stripHtmlTags();
}

update lst; 

This will remove all the html and return only content

Output

https://www.merchant.com/store

Account

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_stripHtmlTags


Option 2

What you can do query all the accounts with Url__c

List<Account> lst = [SELECT Id, Url__c FROM Account WHERE Url__c != null];

for(Account obj: lst)
{
   obj.Url__c = obj.Url__c.unescapeHtml4();
}

update lst; 

Run this code from developer console.This will make you rich text area well formatted.

Output:

https://www.merchant.com/store

Account

here Account will display as link

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .