How to write in cloud code ( parse.com ) a short program that automaticaly copies value of a field of one PFObject
to certain field of other PFObject
(of different class than the original one) whenever the original field is being changed? For example I have some user's name and I want it automatically copied to his posts' objects whenever he changes his name.
And I would like it to be done on servers' side. Thus I need it written in cloud code.

- 9,643
- 16
- 47
- 50

- 119
- 1
- 12
-
What have you done so far? Any code? – Abhishek Apr 25 '15 at 11:21
-
Why not just reference the user and pull it when you need it? Updating all of a users posts when something changes could push you well over your request rate limit... – Wain Apr 25 '15 at 11:24
1 Answers
To implement this functionality, you can use the Parse Cloud afterSave procedure. Assume you have a Profile table in Parse and your user has an option where s/he can update the name. So you can write a cloud afterSave function where you search the Post table (based on your userId) and update the name fields (This is a simple scenario) The below code is an example;
Parse.Cloud.afterSave("Profile", function(request) {
var updatedList = [];
var query = new Parse.Query("Post");
query.equalTo("userId", request.object.get("userId"));
query.find({
success: function(records)
{
for(var i = 0;i < records.length;i++)
{
var currentObject = records[i];
currentObject.set("name",request.object.get("name"));
updatedList.push(currentObject);
}
Parse.Object.saveAll(updatedList,{
success: function(list) {},
error: function(error) {},
});
},
error: function(error)
{
}
});
});
The above code is triggered after save in Profile table. It searches the Post class and finds all entries related with your current user (requester). Following than it updates the Post table where it changes the name field.
Also DO NOT FORGET this procedure is triggered for all updates in Profile table. So another option is write a cloud function where you trigger when you update something in Profile (on your application). Hope this helps.
Regards.

- 1,776
- 2
- 14
- 24