You need to sign in to do that
Don't have an account?
Hermann 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.
But 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()":
My current code uses a custom label but I really don't want to use a custom label.
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.
But 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()":
My 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.
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.
All Answers
Can you check if you are able to redirect to the community using network.Id?
See Network class to get info on all available methods
Anudeep
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)
it redirects me correctly to my client portal but only on the login page:
Which is almost what I need...
Now it would be great to change login to question in the URL as shown below:
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.