0

I wrote this code looking at references and documentation. However, I want to prevent hardcoding of the URl . Also, the email that I receive goes to my company's junk mail box. What best practice can I apply to prevent hard coding the URL, and from the email going to Junk box?

 public PageReference sendAgreement(){
    Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
    email.setToAddresses(new List<String>{ReceiverEmail});
    EmailTemplate template = [Select id,HTMLValue,Subject from EmailTemplate where DeveloperName =: Label.Agreement_Submission_EmailTemplate];
    String str = template.HTMLvalue;
    str = str.replace('{leadurl}','<a href="https://SB2-blalala.xx00.force.com/AgreementForm?id='+leadId+'&sendAgreement=true" type="button" style="background-color: #2681b3;color: white;padding: 1%;border: 1px solid #2681b3;text-decoration: none;font-size:16px;"><b>Submit Agreement</b></a>');
    
    email.setHTMLBody(str);
    email.setSubject(template.Subject);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
    return new PageReference('/' + leadId );

}

Edit: I tried the following but as seen in the output the URL are different.

String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();

System.debug('baseUrl =' + baseUrl);

**Output:** https://blalala--SB2.my.salesforce.com


String baseHost = System.URL.getSalesforceBaseUrl().getHost(); 

System.debug('baseHost =' + baseHost); 

**Output**: blalala--SB2.my.salesforce.com

Basically URL referenced in the class has the instance name instead of the domain name.

I would prefer using domain name instead of instance name, but not sure why the link breaks when I replace the instance name with domain name.

URL referenced in Apex Class: https://SB2-blalala.xx00.force.com/

6
  • 1
    To prevent email from going to junk you need to add that email to your contact list, as the email client decides whether the email address is safe or not. For getting the URL, can you try getSalesforceBaseUrl().getHost() , see what do you get? – anxiousAvocado May 4 at 15:05
  • 1
    System.URL.getSalesforceBaseUrl().toExternalForm(); should be correct... and make reachable url. – Pranay Jaiswal May 4 at 15:07
  • 3
    You need to investigate in DKIM Keys or Email Relay to make sure clients dont treat it as spam – Pranay Jaiswal May 4 at 15:07
  • toExternalForm vs getHost both give the same URL, the only difference being toExternalForm also appends https:// if i replace the URl referenced in the Apex class with what is returned from the URL class below, I get 'link not exists error' String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm(); System.debug('baseUrl =' + baseUrl); Output: blalala--SB2.my.salesforce.com String baseHost = System.URL.getSalesforceBaseUrl().getHost(); System.debug('baseHost =' + baseHost); Output: blalala--SB2.my.salesforce.com – GroundToCloud May 4 at 15:13
  • 1
    Querying the SiteDetail object for the secureUrl is the most durable solution to getting the site's URL. Here's an example: salesforce.stackexchange.com/a/247372 – Mark Pond May 4 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.