+ Start a Discussion
Amit LohAmit Loh 

connected API sample to post to URL via code

I see
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectapi_examples_define_post_action_link.htm
Above link code works for me to post to API.
http://salesforce.stackexchange.com/questions/69472/ui-actionlink-to-salesforce1-custom-visualforce-page

but this only posts to API and second link does not have complete sample.
I need a sample code that opens up another apex page.
I tried but it gives error
// Create the action link definition.
actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Api; (I need this to be Ui)
actionLinkDefinitionInput.actionUrl = '/services/data/v33.0/chatter/feed-elements';
actionLinkDefinitionInput.headers = new List<ConnectApi.RequestHeaderInput>();
actionLinkDefinitionInput.labelKey = 'Post';
actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpPost;
actionLinkDefinitionInput.requestBody = '{\"subjectId\": \"me\",\"feedElementType\": \"FeedItem\",\"body\": {\"messageSegments\": [{\"type\": \"Text\",\"text\": \"This is a test post created via an API action link.\"}]}}';
actionLinkDefinitionInput.requiresConfirmation = true;
 
Best Answer chosen by Amit Loh
alouie_sfdcalouie_sfdc
Can you please: 1. Paste in the UI action link code that you're trying to execute, and 2. Let me know exactly which line the Null Pointer Exception is happening on.

I'd rather help you through this than provide a full code sample.

All Answers

Amit LohAmit Loh
User-added image
This post works but I need a click on this post to open a new apex URL and not API post.
alouie_sfdcalouie_sfdc
You need to:
  • Change the actionType to ConnectApi.ActionLlinkType.Ui
  • Change the actionUrl to the URL of your VisualForce page (or whatever webpage you want to use)
  • Change the method to ConnectApi.HttpRequestMethod.HttpGet
  • Remove the requestBody, since that's only for API-type action links
So it would be something like this:

// Create the action link definition.
actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Ui;
actionLinkDefinitionInput.actionUrl = 'https://my.website.com';
actionLinkDefinitionInput.headers = new List<ConnectApi.RequestHeaderInput>();
actionLinkDefinitionInput.labelKey = 'Post';
actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpGet;
actionLinkDefinitionInput.requiresConfirmation = true;
alouie_sfdcalouie_sfdc
You also don't need the headers, since those are for API-type action links only.

// Create the action link definition.
actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Ui;
actionLinkDefinitionInput.actionUrl = 'https://my.website.com';
actionLinkDefinitionInput.labelKey = 'Post';
actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpGet;
actionLinkDefinitionInput.requiresConfirmation = true;
 
Amit LohAmit Loh
Thanks! But when I change the code snippet to this it gives error
caused by: System.NullPointerException: Attempt to de-reference a null object
Can someone give me working simple full sample with
actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Ui;

sample working post code-
ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new ConnectApi.ActionLinkGroupDefinitionInput();
ConnectApi.ActionLinkDefinitionInput actionLinkDefinitionInput = new ConnectApi.ActionLinkDefinitionInput();
ConnectApi.RequestHeaderInput requestHeaderInput1 = new ConnectApi.RequestHeaderInput();
ConnectApi.RequestHeaderInput requestHeaderInput2 = new ConnectApi.RequestHeaderInput();

// Create the action link group definition.
actionLinkGroupDefinitionInput.actionLinks = New List<ConnectApi.ActionLinkDefinitionInput>();
actionLinkGroupDefinitionInput.executionsAllowed = ConnectApi.ActionLinkExecutionsAllowed.OncePerUser;
actionLinkGroupDefinitionInput.category = ConnectApi.PlatformActionGroupCategory.Primary;
// To Do: Verify that the date is in the future.
// Action link groups are removed from feed elements on the expiration date.
datetime myDate = datetime.newInstance(2016, 3, 1);
actionLinkGroupDefinitionInput.expirationDate = myDate;

// Create the action link definition.
actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Api; this I need .Ui
actionLinkDefinitionInput.actionUrl = '/services/data/v33.0/chatter/feed-elements';
actionLinkDefinitionInput.headers = new List<ConnectApi.RequestHeaderInput>();
actionLinkDefinitionInput.labelKey = 'Post';
actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpPost;
actionLinkDefinitionInput.requestBody = '{\"subjectId\": \"me\",\"feedElementType\": \"FeedItem\",\"body\": {\"messageSegments\": [{\"type\": \"Text\",\"text\": \"This is a test post created via an API action link.\"}]}}';
actionLinkDefinitionInput.requiresConfirmation = true;


// To Do: Substitute an OAuth value for your Salesforce org. 
requestHeaderInput1.name = 'Authorization';
requestHeaderInput1.value = 'OAuth 00DD00000007WNP!ARsAQCwoeV0zzAV847FTl4zF.85w.EwsPbUgXR4SAjsp';
actionLinkDefinitionInput.headers.add(requestHeaderInput1);

requestHeaderInput2.name = 'Content-Type';
requestHeaderInput2.value = 'application/json';
actionLinkDefinitionInput.headers.add(requestHeaderInput2);

// Add the action link definition to the action link group definition.
actionLinkGroupDefinitionInput.actionLinks.add(actionLinkDefinitionInput);

// Instantiate the action link group definition.
ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition = ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(), actionLinkGroupDefinitionInput);

ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new ConnectApi.AssociatedActionsCapabilityInput();
ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

// Set the properties of the feedItemInput object.
feedItemInput.body = messageBodyInput;
feedItemInput.capabilities = feedElementCapabilitiesInput;
feedItemInput.subjectId = 'me';

// Create the text for the post.
messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
textSegmentInput.text = 'Click to post a feed item12345.';  
messageBodyInput.messageSegments.add(textSegmentInput);


// The feedElementCapabilitiesInput object holds the capabilities of the feed item.
// Define an associated actions capability to hold the action link group.
// The action link group ID is returned from the call to create the action link group definition. 
feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
associatedActionsCapabilityInput.actionLinkGroupIds = new List<String>();
associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);

// Post the feed item. 
ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
   ConnectApi.FeedElement feedElement1 = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), 'me', ConnectApi.FeedElementType.FeedItem, 'I am on vacation this week.</br>Please approve tender Urgently');     
alouie_sfdcalouie_sfdc
Can you please: 1. Paste in the UI action link code that you're trying to execute, and 2. Let me know exactly which line the Null Pointer Exception is happening on.

I'd rather help you through this than provide a full code sample.
This was selected as the best answer
Amit LohAmit Loh
Ya!1! It worked!!!! Thank you so much man!
alouie_sfdcalouie_sfdc
Happy to help!
Amit LohAmit Loh
Hi Alouie,

How to post to a salesforce group?
Right now I am posting with me keyword. I want to post to a public group created.

ConnectApi.FeedElement feedElement1 = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), 'me', ConnectApi.FeedElementType.FeedItem, 'I am on vacation this week.</br>Please approve tender Urgently');     
 
alouie_sfdcalouie_sfdc
Instead of 'me', use the ID of the group.
Amit LohAmit Loh
Thanks! I am little new to SF(1 week old!).
I tried
String SOQL = 'SELECT Id,name FROM Group WHERE Type = \'PRApprovers\'';
group1 = Database.Query(SOQL);
Group group2=group1.get(0); but its giving me error
 
alouie_sfdcalouie_sfdc
No problem. Welcome to Force.com development!

This is confusing, but the groups in Chatter use the CollaborationGroup sObject, not the Group sObject. So you'll want to do something like this if you want to make a SOQL query:

List<CollaborationGroup> g = [SELECT Id, Name FROM CollaborationGroup WHERE ...];

See the CollaborationGroup sObject documentation for more info:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_collaborationgroup.htm
Amit LohAmit Loh
sorry, I mean normal public group. The groups are not chatter groups. I created sample Group(public) BTApprovers and added one user. I need this post to go to this group.
alouie_sfdcalouie_sfdc
That's not possible in Chatter. With Chatter, you can only post to Chatter groups. You can't post to the non-Chatter kind of group.

As an alternative, you could make the post somewhere in Chatter and @mention everyone in the group as part of your message. Or you make individual Chatter posts to each user's profile (by setting the subjectId to the user ID when you call postFeedElement -- there's also a method called postFeedElementBatch that lets you make multiple posts in a single call).
Amit LohAmit Loh
Thanks! I will try to post to chatter groups and will keep you posted. Thank you!
Amit LohAmit Loh
Thanks that worked for me!!
string MGGroupId = [SELECT Name FROM CollaborationGroup where Name = 'TenderApprovers'].Id;
System.debug(MGGroupId);

Now how can I add 6 to 7 lines of text with parameters in this chatter post(I was able to add 2 buttons approve/reject) coming from querystring like below
TenderID=querystringval1
Tendername=querystringval2
TenderDate=querystringparam3 etc..
Amit LohAmit Loh
I got it!! Its via \n