vendredi 19 décembre 2014

JSOM context newly created id reference question when creating more than one item



I am using the COM Javascript/jQuery to get, create & update some list items. I have two lists and everytime I create a new item in one list, the newly created id is something that I want to grab in order to place in a FK like field in the second list (not even using linked at this point, just a generic integer type field).


Simple enough, but what if I want to through this process into a loop and create 10 at the same time.


after I create a new item using



function createItem(tbl, obj, callback) {
var cxt = new SP.ClientContext.get_current();
var list = cxt.get_web().get_lists().getByTitle(list);
var createInfo = new SP.ListItemCreationInformation();
this.listItem = list.addItem(createInfo);
for ( var prop in obj ) {
listItem.set_item(prop, obj[prop])
}
cxt.load(listItem);
cxt.executeQueryAsync(callback, Function.createDelegate(this, this.onQueryFailed));

}

_createItem ('ListA', { Title: 'First List Item' }, function () {
var thisId = listItem.get_id();
var other = {
Title: 'Second List Item',
RelatedId: thisId
};
_createItem('ListB', other, function () {
console.log('created');
});
});


When I use the above inside a loop to create 5 records in both lists it doesn't work, the thisId/item.get_() typically returns a -1 even though the items are getting created in both lists. I am sure there is a perfectly good reason this occurs, I am just unsure of it and was hoping someone could help me understand, and perhaps if there is a way to get at what I am trying to do properly.....I have tried deep copies and this doesn't seem to matter??








0 commentaires:

Enregistrer un commentaire