Skip to content

Instantly share code, notes, and snippets.

@chrismauck
Forked from pfeilbr/veeva.js
Created August 30, 2016 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismauck/f039ceb8aee440b47bf11675a46493ce to your computer and use it in GitHub Desktop.
Save chrismauck/f039ceb8aee440b47bf11675a46493ce to your computer and use it in GitHub Desktop.
Veeva API helper library
// Veeva API helper library
// dependencies: async
// Brian Pfeil - 2012-03-01 - init
(function() {
window.veeva = {
getRandomFunctionName: function() {
var randomNumber = Math.floor(Math.random() * (new Date()).getTime());
var millisecondsSinceEpoch = (new Date().getTime());
var cbFnName = "veevacallback" + millisecondsSinceEpoch + '' + randomNumber;
return cbFnName;
},
getDataForObject: function(objectName, objectFieldName, cb) {
if ( (navigator.userAgent.match(/iPad/i) != null) ) {
var cbFnName = this.getRandomFunctionName();
window[cbFnName] = function(result) {
// preprocess the result
// if the value is the same as the ObjectName.FieldName requested, then there was
// no value so set it to the empty string
if (result[objectName][objectFieldName] === (objectName + '.' + objectFieldName)) {
result[objectName][objectFieldName] = '';
}
// call the callback
cb(result);
};
document.location = 'veeva:getDataForObject(' + objectName + '),fieldname(' + objectFieldName + '),' + cbFnName + '(result)';
} else {
var result = {};
result[objectName] = {};
result[objectName][objectFieldName] = objectName + '.' + objectFieldName;
cb(result);
}
},
// get a set of field values for a single object
getDataElementsForObject: function(objectName, objectFieldNames, cb) {
var that = this;
var fns = [];
$.each(objectFieldNames, function(i, fieldName) {
fns.push(
function(callback) {
that.getDataForObject(objectName, fieldName, function(result) {
callback(null, {objectName: objectName, fieldName: fieldName, result: result});
});
}
);
});
var obj = {}
async.series(fns, function(err, results) {
$.each(results, function(i, result) {
obj[result.fieldName] = result.result[result.objectName][result.fieldName];
});
cb(obj);
});
},
getDataForObjectWithId: function(objectName, objectId, objectFieldName, cb) {
if ( (navigator.userAgent.match(/iPad/i) != null) ) {
var cbFnName = this.getRandomFunctionName();
window[cbFnName] = function(result) {
// preprocess the result
// if the value is the same as the ObjectName.FieldName requested, then there was
// no value so set it to the empty string
if (result[objectName][objectFieldName] === (objectName + '.' + objectFieldName)) {
result[objectName][objectFieldName] = '';
}
// call the callback
cb(result);
};
document.location = 'veeva:getDataForObject(' + objectName + '),objId(' + objectId + '),fieldName(' + objectFieldName + '),' + cbFnName + '(result)';
} else {
var result = {};
result[objectName] = {};
result[objectName][objectFieldName] = objectName + '.' + objectFieldName;
cb(result);
}
},
getDataElementsForObjectWithId: function(objectName, objectId, objectFieldNames, cb) {
var that = this;
var fns = [];
$.each(objectFieldNames, function(i, fieldName) {
fns.push(
function(callback) {
that.getDataForObjectWithId(objectName, objectId, fieldName, function(result) {
callback(null, {objectName: objectName, fieldName: fieldName, result: result});
});
}
);
});
var obj = {}
async.series(fns, function(err, results) {
$.each(results, function(i, result) {
obj[result.fieldName] = result.result[result.objectName][result.fieldName];
});
cb(obj);
});
},
saveObject: function(objectName, obj, cb) {
var cbFnName = this.getRandomFunctionName();
window[cbFnName] = cb;
var jsonDataAsString = JSON.stringify(obj);
var request = "veeva:saveObject(" + objectName + "),value(" + jsonDataAsString + "),callback(" + cbFnName + ")";
document.location = request;
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment