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
Hermann Oure 6Hermann Oure 6 

Create Link to redirect to a community Post with Network Id

Hello,

I have created a class to send Slack notification to my community moderators anytime a Question Post is created in our Client Portal.
When clicking on the Link; The community moderator should be redirected to the post.
User-added imageBut I am not able to redirect to the community. I would rather not use a custom label.
I would prefer using the Network Id of the community or if there is something similar to "URL.getOrgDomainUrl().toExternalForm()":
User-added imageMy current code uses a custom label but I really don't want to use a custom label.
public class SlackNotificationCommunityPost {
    
    /*
    -------------------------------------------------------------------------------------------------
    -- - Description   : Class to send Slack notification based on QuestionPost created on Community
    -- -----------  ----  -------  ------------------------------------------------------------------
     */
    
    public class slackRequest {
        @InvocableVariable(label='title')
        public String title; 
        
        @InvocableVariable(label='id')
        public String id; 
        
        @InvocableVariable(label='name')
        public String name;    
    }
    
    @InvocableMethod(label='Publish New Community posts to Slack')
    public static void publishNewCommunityPostsToSlack(List<slackRequest> requests) {

        //String webhookURL='https://hooks.slack.com/services/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';
        String webhookURL = system.label.Param_Slack_Token;
        String msg;
        String channelName;

        for(slackRequest r:requests){
            
            if(r.id != null && r.name != null && r.title != null) {
                System.debug('### SlackNotificationCommunityPosts new post');
                channelName = '#' +Label.Slack_Community_Channel;
                msg = 'A new community post has been created : *'+r.title+'* - By User : (*'+r.name+'*)';
                msg += '\nLink to Community post : '+Label.Smart_Client_Portal+'/'+r.id;
            }
            
            //Generate JSON for request
            try {
                if (r.id != null && r.title !=null && r.name != null) {
                   System.debug('### SlackNotificationCommunityPosts sending message');
                   JSONGenerator gen = JSON.createGenerator(true);
                    gen.writeStartObject(); //Inserts {
                    gen.writeStringField('text', msg);
                    gen.writeStringField('channel', '#salesforce_test');
                    gen.writeStringField('username', 'bot-support');
                    gen.writeStringField('icon_emoji', ':smartplus:');
                    gen.writeEndObject(); //Inserts }
                    String body = gen.getAsString(); //Translates JSONGenerator to string to be passed to callout
                    System.debug('### SlackNotificationCommunityPosts body: '+ body);
                    System.enqueueJob(new qCallOut(webhookURL, 'POST', body)); // Send request
      
               	 } else {
                    System.debug('### SlackNotificationCommunityPosts Id = '+ r.id);
                    return; 
                }
                
            }
            catch (exception e) {
                System.debug('### SlackNotificationCommunityPosts error:' +e);
            }
            
        }
     
    }
    
     public class qCallOut implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public qCallOut(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();
            // to pass when process builder is invoked by another test class
            if(!Test.isRunningTest()){  
              HttpResponse res = http.send(req);
            }
        }
         
     }

}
Thank you.
 
Best Answer chosen by Hermann Oure 6
Hermann Oure 6Hermann Oure 6
Ok I managed to find how to create the link to be redirected to a specific record.Id using the Network class communitiesLanding().
You need to use getUrl() + PageName + record.Id:
msg += '\nLink to Community post : '+Network.communitiesLanding().getUrl()+'question/'+r.id;
This will create a link for a specific record on community.
 
//String webhookURL='https://hooks.slack.com/services/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';
        String webhookURL = system.label.Param_Slack_Token;
        String msg;
        String channelName;
        
        for(slackRequest r:requests){
            
            if(r.id != null && r.name != null && r.title != null) {
                System.debug('### SlackNotificationCommunityPosts new post');
                channelName = '#' +Label.Slack_Community_Channel;
                msg = 'A new community post has been created : *'+r.title+'* - By User : (*'+r.name+'*)';
                msg += '\nLink to Community post : '+Network.communitiesLanding().getUrl()+'question/'+r.id;
            }

 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Hermann, 

Can you check if you are able to redirect to the community using network.Id?
 
Network myNetwork = [SELECT Id FROM Network WHERE Name ='communityName'];
System.debug('MyDebug: ' + Network.getLoginUrl(myNetwork.id);

String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + myNetwork.Id;

See Network class to get info on all available methods

Anudeep

 
Hermann Oure 6Hermann Oure 6

Hi Anudeep,

Thank you very much for your answer.
I am not redirect to the client portal when I use fullRecordURL...

But if I use directly Network.getLoginUrl(myNetwork.id)

User-added image

it redirects me correctly to my client portal but only on the login page:

User-added image
Which is almost what I need...

Now it would be great to change login to question in the URL as shown below:

User-added image

 

Hermann Oure 6Hermann Oure 6
Ok I managed to find how to create the link to be redirected to a specific record.Id using the Network class communitiesLanding().
You need to use getUrl() + PageName + record.Id:
msg += '\nLink to Community post : '+Network.communitiesLanding().getUrl()+'question/'+r.id;
This will create a link for a specific record on community.
 
//String webhookURL='https://hooks.slack.com/services/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';
        String webhookURL = system.label.Param_Slack_Token;
        String msg;
        String channelName;
        
        for(slackRequest r:requests){
            
            if(r.id != null && r.name != null && r.title != null) {
                System.debug('### SlackNotificationCommunityPosts new post');
                channelName = '#' +Label.Slack_Community_Channel;
                msg = 'A new community post has been created : *'+r.title+'* - By User : (*'+r.name+'*)';
                msg += '\nLink to Community post : '+Network.communitiesLanding().getUrl()+'question/'+r.id;
            }

 
This was selected as the best answer