Guest User

Untitled

a guest
Nov 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function UpdateForeignKey(id, field, fieldValue) {
  2. var context = getContext();
  3. var coll = context.getCollection();
  4. var link = coll.getSelfLink();
  5. var response = context.getResponse();
  6. if (!id) throw new Error('The ID is undefined.');
  7. if (!field) throw new Error('The field is undefined.');
  8. if (!fieldValue) throw new Error('The fieldValue is undefined')
  9. var query = 'SELECT * FROM Devices hr WHERE hr.id = "' + id + '"';
  10. var run = coll.queryDocuments(link, query, {}, callback);
  11.  
  12.  
  13. function callback(err, docs) {
  14. if (err) throw err;
  15. if (docs.length > 0) UpdateDoc(docs[0]);
  16. else response.setBody('The document was not found.');
  17. }
  18. if (!run) { throw new Error('The stored procedure could not be processed.'); }
  19. function UpdateDoc(doc) {
  20. switch (field) {
  21. case "Customer":
  22. doc.CustomerId = fieldValue;
  23. case "Hub":
  24. doc.IoTHubId = fieldValue;
  25. case "Files":
  26. doc.FileId = fieldValue;
  27. case "Location":
  28. doc.LocationId = fieldValue;
  29. case "Attributes":
  30. doc.AttributesId = fieldValue;
  31. case "Key":
  32. doc.Key = fieldValue;
  33. case "Schedule":
  34. doc.DeviceId = fieldValue;
  35. case "Status":
  36. doc.Status = fieldValue;
  37. case "StatusCode":
  38. doc.StatusCode = fieldValue;
  39.  
  40.  
  41. }
  42. var replace = coll.replaceDocument(doc._self, doc, {}, function(err, newdoc) {
  43. if (err) throw err;
  44. response.setBody(newdoc);
  45. });
  46. if (!replace) { throw new Error('The document could not be updated.'); }
  47. }
  48. }
Add Comment
Please, Sign In to add comment