+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Chatter post as a link via apex class

Hi,

I have a requirement where i am posting certain field information from my child object to parent object as a chatter post via apex which happens corretly

My issue is i have a auto number called recovery number which i want to make as a hyperlink when posted to chatter and when clicking on the same it should take me to the respective record. 

I have written the code but for some reasons its not working for me
public static void postToFeed(List<Recovery__c> recoveries , String status)
{
	for(Recovery__c rec : recoveries)
	{  
		String linkURL = '<a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/'+ rec.id+'" target="_blank">'+ rec.Name + '</a>';
        //String codeSnippet = '<html>\n\t<body>\n\t\tHello, world!\n\t</body>\n</html>';		
        String codeSnippet = '<html>\n';
			   codeSnippet += '\t<body>\n\t\t';
			   codeSnippet += 'Recovery Fulfillment ' + status +  ' for ' + rec.Type__c + '\n';
			   codeSnippet += 'Recovery Number: '+linkURL+ '\n';
			   codeSnippet += 'Recovery Value:' + ( rec.Value__c != null ? rec.Value__c : 0) + '\n';
			   codeSnippet += 'Feedback: ' + (String.isNotBlank(rec.Approver_Comment__c) ? rec.Approver_Comment__c : 'N/A') + '\n';
			   codeSnippet += 'Original Currency: ' + (String.isNotBlank(rec.Original_Currency__c) ? rec.Original_Currency__c : 'N/A') + '\n';
			   codeSnippet += '\t</body>\n</html>';

		ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
		input.subjectId = rec.Case_Number__c;
		input.feedElementType = ConnectApi.FeedElementType.FeedItem;

		ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
		ConnectApi.TextSegmentInput textSegment;

		messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

		textSegment = new ConnectApi.TextSegmentInput();
		textSegment.text = codeSnippet;
		messageInput.messageSegments.add(textSegment);

		input.body = messageInput;

		ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), input);
	}
}

Kindly help me on the same and let me know the issue , Thanks in advance