[SalesForce] How to create a FeedItem with SOAP API

I'm trying to fetch certain FeedItems and then save them. None of these two things I could achieve using Java and SOAP, but here I'll focus on only one, and that is saving.

SOAP API version is 32.0, it's an enterpise WSDL, and I'm using the code below. I'm also aware of two projects, JavaChatterRESTApi and ChatterUtility, however they're not up2date.

String userID = "user id";
FeedItem newFeed = new FeedItem();

// newFeed.setId(feed.getId());
newFeed.setBody("I don't wanna say hello world, because noone sees me.");
newFeed.setParentId(userID);
newFeed.setCreatedById(userID);
com.sforce.soap.enterprise.sobject.SObject[] objects = { newFeed };
SaveResult res = eConn().create(objects)[0];
System.out.println(Arrays.toString(res.getErrors()));

and it gives me this error:

[[Error  fields='{[0]}'
 message='invalid cross reference id'
 statusCode='INVALID_CROSS_REFERENCE_KEY'
]
]

However, when working in Apex, the following, analogue, code works:

String userID = 'user id';
FeedItem newFeed = new FeedItem();

// newFeed.Id = feed.getId = ;
newFeed.Body = 'I dont wanna say hello world, because noone sees me.';
newFeed.ParentId = userID;
newFeed.CreatedById = userID;
insert newFeed;

Question is: Why doesn't the Java code work?

Update #1

In the above code, I've used an enterprise connection to create a FeedItem. Using a partner connection, I got the following error:

[[Error  fields='{[0]}'
 message='Users specified in CreatedById must be active, non-portal users in this organization.'
 statusCode='INVALID_CROSS_REFERENCE_KEY'
]
]

which is probably linked to the this Knowledge Article.

Best Answer

I think the error is happening because the CreatedById field is being set. From the FeedItem SObject docs:

If the logged-in user has the “Insert System Field Values for Chatter Feeds” user permission, the create field property is available on CreatedBy and CreatedDate system fields for this object.

It's not clear to me why you're getting the error in your Java code but not your Apex code. Perhaps your Apex code is running in system mode? Do you need to override the CreatedById field? If not, you can avoid the problem by simply not setting it.

Related Topic