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
Michael MMichael M 

Update value of rich text area hyperlinks

I had built a trigger in Sandbox, which finds duplicates (with the same external id) and it creates a hyperlink (in the form of a date) on the new record, to be able to link to the old record. I will show you the piece of the code that does that below. However, I accidentally forgot to change the hyperlink from the sandbox link to the production link. I now need to change all the vlaues of those hyperlinks in production. I tried something in the execute anonymous window as a test, but it didn't seem to work. I'll show you both the piece of the Handler and the attempt that I made in the execute window. 

HANDLER:
                string pdi = discharge.Previous_instances__c;
                string disId = discharge.id;
                date disDate = discharge.Discharge_Date__c;
                integer day = oldDischargeMap.get(discharge.Id).Discharge_Date__c.day();
                integer month = oldDischargeMap.get(discharge.Id).Discharge_Date__c.month();
                integer year = oldDischargeMap.get(discharge.Id).Discharge_Date__c.year();
                string dateDisplay = month + '/' + day + '/' + year;
                if(pdi == null){
                    pdi = '';}
                else{
                    pdi = pdi;}
                
                newDischarge.previous_instances__c = pdi+' '+ '<a href=https://centers--partial.lightning.force.com/'+disId+'>'+dateDisplay+ '</a>' + ' ; ' ;

EXECUTE TRIAL
list<discharge__c> disch = [select previous_instances__c from discharge__c ];
for (discharge__c dis: disch){
    if (dis.previous_instances__c != null){
    string link = dis.previous_instances__c;
    link = link.replace('<a href=https://centers--partial.lightning.force.com/', '<a href=https://centers.lightning.force.com/');
    dis.previous_instances__c = link;
        update dis;
    }}
 
Best Answer chosen by Michael M
Maharajan CMaharajan C
Hi Michael,

You can use the URL.getSalesforceBaseUrl().toExternalForm().
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm

newDischarge.previous_instances__c = pdi+' '+ '<a href=' +URL.getSalesforceBaseUrl().toExternalForm()+'/'+disId+'>'+dateDisplay+ '</a>' + ' ; ' ;

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Michael,

You can use the URL.getSalesforceBaseUrl().toExternalForm().
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm

newDischarge.previous_instances__c = pdi+' '+ '<a href=' +URL.getSalesforceBaseUrl().toExternalForm()+'/'+disId+'>'+dateDisplay+ '</a>' + ' ; ' ;

Thanks,
Maharajan.C
This was selected as the best answer
Michael MMichael M
Thank you