Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function UpdateForeignKey(id, field, fieldValue) {
- var context = getContext();
- var coll = context.getCollection();
- var link = coll.getSelfLink();
- var response = context.getResponse();
- if (!id) throw new Error('The ID is undefined.');
- if (!field) throw new Error('The field is undefined.');
- if (!fieldValue) throw new Error('The fieldValue is undefined')
- var query = 'SELECT * FROM Devices hr WHERE hr.id = "' + id + '"';
- var run = coll.queryDocuments(link, query, {}, callback);
- function callback(err, docs) {
- if (err) throw err;
- if (docs.length > 0) UpdateDoc(docs[0]);
- else response.setBody('The document was not found.');
- }
- if (!run) { throw new Error('The stored procedure could not be processed.'); }
- function UpdateDoc(doc) {
- switch (field) {
- case "Customer":
- doc.CustomerId = fieldValue;
- case "Hub":
- doc.IoTHubId = fieldValue;
- case "Files":
- doc.FileId = fieldValue;
- case "Location":
- doc.LocationId = fieldValue;
- case "Attributes":
- doc.AttributesId = fieldValue;
- case "Key":
- doc.Key = fieldValue;
- case "Schedule":
- doc.DeviceId = fieldValue;
- case "Status":
- doc.Status = fieldValue;
- case "StatusCode":
- doc.StatusCode = fieldValue;
- }
- var replace = coll.replaceDocument(doc._self, doc, {}, function(err, newdoc) {
- if (err) throw err;
- response.setBody(newdoc);
- });
- if (!replace) { throw new Error('The document could not be updated.'); }
- }
- }
Add Comment
Please, Sign In to add comment