+ Start a Discussion
kodeXkodeX 

Add a comment to Post

Hi, i am trying to add a comment to a Post through java SOAP code.

 

Here is the code snippet-

 

..

string feeditemid= "0D53000000aZwx6CAC" (i.e- id of the Post to which i want to add the comment)

string comment= "This is a comment from me"

..

 

SObject s=new SObject();
       s.setType("FeedComment");
       s.setId(feeditemid);

       s.setField("CommentBody",comment);
      
       SaveResult[] results= connection.update(new SObject[]{s});
       if (!results[0].isSuccess()) {
               System.out.println("Error updating user status: "
                 + results[0].getErrors()[0].getMessage());
               }
               else {
                  System.out.println("Comment added successfuly");
                  }
  


It is giving error-

Error updating user status: '0D53000000aZwx6CAC' is not a valid Salesforce ID for the type Feed Comment

 

Note-

The type of ID for FeedComment object, is of "reference" field type.

 

How to convert simple string ID to a "reference" type ID ?

Any other API function i need to call?

Best Answer chosen by Admin (Salesforce Developers) 
kodeXkodeX

Web Services logic-

static void addcomment()throws Exception
 {
	 String feeditemid= "0D590000006ptxOCAQ";

	 String comment= "This is a comment from me";

 

	 SObject s=new SObject();
	        s.setType("FeedComment");
	        s.setField("FeedItemId",feeditemid);
	        s.setField("CommentBody",comment);
	       
	        SaveResult[] results= connection.create(new SObject[]{s});
	        if (!results[0].isSuccess()) {
	                System.out.println("Error updating user status: "
	                  + results[0].getErrors()[0].getMessage());
	                }
	                else {
	                   System.out.println("Comment added successfuly");
	                   }
	   	 
	 
 }

 Successfully added comments!!!

All Answers

ChrisOctagonChrisOctagon

I think that when making a new comment you should supply the feed item id as the "feed item ID" not the "ID". The "ID" is generated after insertion into the database.

kodeXkodeX

Hi Chris,

I am not supplying an ID, actually the value contained in the variable feeditemid is the Feed item id of a specific feed on my Profile Page. I retrieved this value by printing out the ID's of all the feeds on my profile page, feeditemid is one id of a feed among the list of feed item id's i have.

 

So, what is the solution?

ChrisOctagonChrisOctagon

Call s.setFeedItemId(feeditemid); instead of  s.setId(feeditemid);

kodeXkodeX

Chris,

Eclipse is giving error for the above method as "undefined method".

 

I also searched apex,soap,rest api's for the above method but didn't find it anywhere!

 

 

 

kodeXkodeX

Solved by using Rest logic.

kodeXkodeX

Web Services logic-

static void addcomment()throws Exception
 {
	 String feeditemid= "0D590000006ptxOCAQ";

	 String comment= "This is a comment from me";

 

	 SObject s=new SObject();
	        s.setType("FeedComment");
	        s.setField("FeedItemId",feeditemid);
	        s.setField("CommentBody",comment);
	       
	        SaveResult[] results= connection.create(new SObject[]{s});
	        if (!results[0].isSuccess()) {
	                System.out.println("Error updating user status: "
	                  + results[0].getErrors()[0].getMessage());
	                }
	                else {
	                   System.out.println("Comment added successfuly");
	                   }
	   	 
	 
 }

 Successfully added comments!!!

This was selected as the best answer