This is a migrated thread and some comments may be shown as answers.

Where to Get New Record Id

1 Answer 104 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kelly
Top achievements
Rank 1
Kelly asked on 24 Feb 2014, 11:18 PM
Hi,

I'm working on a scheme where one user can add a record to a TBS content type, and assign ownership of that record to a different user.  Using the .net sdk, I tried to set the owner and/or ACL in the request, but found that they are ignored.  Rather than make two round trips, I decided to move the reassignment of ownership into the cloud code, like this:

Everlive.Events.afterCreate(function(request, response, context, done) {
    if(!response.error)
    {
      var id = request.data._id;
      var owner = request.data.NewOwner;
       
      Everlive.Sdk.withMasterKey().data('Blah').setOwner(owner, id);
      console.log("Changed " + id + " owner to " + owner);
    }
    done();
});

This actually works, but I don't like the looks of that request.data._id field.  That's the only place I could see the record Id of the new record in the console log.  It seems as though the new record id should be somewhere inside the response, not the request, no?  I can't find documentation describing the cloud code arguments, so can you please tell me the best way to do this?  Also, why is specifying the owner or ACL when creating a record disallowed? (or am I just doing it incorrectly...?)

Thank you,

Kelly

1 Answer, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 25 Feb 2014, 10:29 AM
Hi Kelly,

To set the owner of an item client-side, the request must be authenticated with the credentials of the current owner of the item or with a master key. Otherwise, the request will fail (with 'Not found' error) because the current user cannot access the item permissions. Also, keep in mind the general security prescription to not deploy the master key with your app.

We'd suggest that such custom logic resides in the cloud code. You can access the Id of the created item in the response.result object like in the following snippet:
Everlive.Events.afterCreate(function(request, response, context, done) {
  if(!response.error){
     var createdItemId = response.result.Id;
    var ownerId = 'GUID-here';
     
    Everlive.Sdk.withMasterKey().data('MyContent').setOwner(ownerId, createdItemId);
                
  }
    done();
});

Let us know if this works for you.

Regards,
Anton Dobrev
Telerik
Everlive is now Telerik Backend Services, and is part of the Telerik Platform. For more information on the new name, and to learn more about the Platform, register for the free online keynote and webinar on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT)
Tags
Cloud Code
Asked by
Kelly
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Share this question
or