You need to sign in to do that
Don't have an account?
Hermann 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:
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/...
support/s/question/...
Is there a way to use Network class and get to support/s/question/...
Thank you
here is the whole code:
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:
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/...
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/...
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); } } } }
If this answer helps you, please mark it as accepted.
Regards,
Tushar Sharma
https://newstechnologystuff.com/
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
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.