0

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.

1

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.

  • Hi @alouie! Thank you for your answer. I've updated my original question. I want to have an as exact as possible copy of a feed item, so I need CreatedById. While I have an answer as to why it can't be done in some cases, I'm still puzzled by the Apex code doing it. – Haris Osmanagić Dec 9 '14 at 8:48
  • What execution context is your Apex code running under? (e.g. anonymous, trigger, batch, etc.) – alouie Dec 12 '14 at 3:41
  • It's anonymous. – Haris Osmanagić Dec 12 '14 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.