I have an event handler where I would like it to increment a number on an object after it has been created. Here is the code I am using:
public void afterCreate( RunnerContext context, Comment comment, ExecutionResult<Comment> result ) throws Exception
{
Integer commentCount = comment.getArticle().getCommentCount();
commentCount++;
comment.getArticle().setCommentCount(commentCount);
comment.getArticle().save();
}
The issue that I am having is that this will only execute once, not every time a new comment object is created. If I redeploy the code, it will run after a comment has been posted, but again, only once. Any comment created after does not increment the counter. Any ideas?
Thanks!
There is a problem in the code here: take a look at the arguments: you get:
Comment comment
ExecutionResult<Comment> result
The first one is the original argument, the second one is the result of the Backendless internal invocation (the one that actually saved your comment (1) object. When you modify (1) and save again, you're modifying original unsaved object. And when you save it again, you're causing a recursion which is stopped by Backendless.
How about creating a beforeCreate handler? In there you can modify the incoming object and then it is saved only once. That way you will get what you want (i.e. an incrementally modified field) and better performance on the call.
Thanks for the reply. I am still having an issue with getting this to work properly. I don’t think I explained my issue properly in my first post.
What I need to do is take the incoming Comment object, fetch the related Article object, and then increment the commentCount property on the Article object.
I have removed the afterCreate event handler I was using above and added a beforeCreate. Here is how it looks now:
public void beforeCreate( RunnerContext context, Comment comment) throws Exception
{
// add your code here
Integer commentCount = comment.getArticle().getCommentCount();
commentCount++;
Article article = new Article();
article.setCommentCount(commentCount);
article = Backendless.Persistence.of(Article.class).findById(comment.getArticle());
Backendless.Persistence.of(Article.class).save(article);
}However, it is still not incrementing the value. I tried to do the save operation async to see if I could get some kind of output, and when I did, it returned a Fault saying “you have no permission to thread manipulation”. I don’t know if they’re related at all. But any help again would be greatly appreciated.Thanks,Andrew
Take a look at the following code and the comments I put in there:
// Here you create a brand new article object
Article article = new Article();
// now you set the comment count in that brand new Article object
article.setCommentCount(commentCount);
// Here you retrieve an article object and thus completely overwrite the previous object
// It will be completely gone after the assignment
article = Backendless.Persistence.of(Article.class).findById(comment.getArticle());
// and now you're saving the object which you just retrieved without any changes at all
Backendless.Persistence.of(Article.class).save(article);