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 

Get URL for community question

Hello,
I have created a class for Slack Notification and I would link to create a link to view the Question Post created on community.
I know it can be done via a custom label but that's exactly what I try to avoid.
The link should take me the the question post as seen below:

User-added image
The ideal would be to use Network class; that gets me close to what I need. For example "Network.getSelfRegUrl(myNetwork.id)" takes me correctly to support/s/register/...

User-added image
Network myNetwork = [SELECT Id FROM Network WHERE Name ='Smart Client Portal'];
		System.debug('MyDebug: ' +Network.getSelfRegUrl(myNetwork.id));
		//String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + myNetwork.Id;

        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.getSelfRegUrl(myNetwork.id)+'/'+r.id;
            }
But I cannot find any a way to get to 
support/s/question/...

User-added image

Is there a way to use Network class and get to support/s/question/...
Thank you

here is the whole code:
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;
        
        Network myNetwork = [SELECT Id FROM Network WHERE Name ='Smart Client Portal'];
		System.debug('MyDebug: ' +Network.getSelfRegUrl(myNetwork.id));
		//String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + myNetwork.Id;

        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.getSelfRegUrl(myNetwork.id)+'/'+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);
            }
        }
         
     }

}


 
{tushar-sharma}{tushar-sharma}
These details are community names and won't change regularly. You can use "communitiesLanding()" to get the complete URL and then remove the start page and append the Id.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
Hermann Oure 6Hermann Oure 6

Thank you Tushar,
very much what I am trying to achieve but I am unale to include ma page name and id in the link
User-added image

 

Network myNetwork = [SELECT Id FROM Network WHERE Name ='Smart Client Portal'];
		System.debug('MyDebug: ' +Network.communitiesLanding());
		//String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + myNetwork.Id;

        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()+'/'+r.id;
            }
 



 

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;
            }