Gary Marroquin
Gary Marroquin

Reputation: 11

Is there a way to create a hyperlink in the apex class body for Salesforce?

I'm using this as reference to create a Slack notification when an opportunity is Closed/Won in Salesforce.

However, I am wondering if there's a way to include a link to the opportunity as part of the Apex class body.

Here's what I have so far:

public with sharing class SlackPublisher {
 
private static final String SLACK_URL = 'HIDDEN URL';
 
public class Oppty {
    @InvocableVariable(label='Opportunity Name')
    public String opptyName;
      @InvocableVariable(label='Opportunity Owner')
    public String opptyOwnerName;
    @InvocableVariable(label='Account Name')
    public String acctName;
    @InvocableVariable(label='Amount')
    public String amount;
}
 
@InvocableMethod(label='Post to Slack')
public static void postToSlack ( List<Oppty> opps ) {
    Oppty o = opps[0]; // bulkify the code later
    
    Map<String,Object> msg = new Map<String,Object>();
     msg.put('text','Deal ' + o.opptyName + ' was just Closed/Won' + ':champagne:' + '\n' + 'for a total of ' + '$' + o.amount + '\n' + ' Check out the details in the document links below');
    msg.put('mrkdwn', true);

    
    String body = JSON.serialize(msg);   
    System.enqueueJob(new QueueableSlackPost(SLACK_URL, 'POST', body));
}
 
public class QueueableSlackPost implements System.Queueable, Database.AllowsCallouts {
     
    private final String url;
    private final String method;
    private final String body;
     
    public QueueableSlackPost(String url, String method, String body) {
        this.url = url;
        this.method = method;
        this.body = body;
    }
     
    public void execute(System.QueueableContext ctx) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod(method);
        req.setBody(body);
        Http http = new Http();
        HttpResponse res = http.send(req);
    }
}
}

Which sends a message to Slack like this:

Slack Notification Image

What I'm looking for is either a way to make the opptyName (Gary Test 1 - Gong) a hyperlink OR add the opportunity URL at the end.

Upvotes: 1

Views: 4451

Answers (2)

sfdx_
sfdx_

Reputation: 1

Please try something like this while creating the message request body replace the https://SalesforceBaseUrl.com with your salesforce base URL

[ URL.getSalesforceBaseUrl().toExternalForm() ]

    {
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Deal <https://SalesforceBaseUrl.com/opp.Id |Gary Test1 - Gong> was just Closed/Won \n for total $5,000"
            }
        }
    ]
}

Should give following output on slack message

Let me know if you have any questions

Upvotes: 0

eyescream
eyescream

Reputation: 19612

Check out the URL class, especially getSalesforceBaseUrl, getOrgDomainUrl.

Id i = '0060g00000zQO77AAG';
String href = URL.getSalesforceBaseUrl().toExternalForm() + '/' + i;
System.debug(href);
// https://redacted--sandbox.my.salesforce.com/0060g00000zQO77AAG

Upvotes: 0

Related Questions