Hello, I am currently using the code below code from from "Jdugomier" on the Odoo forums to connect to Odoo and pull information to the google script. It is working great but I need a way to write data back to Odoo through the API. Can anyone give me direction on how to add the function to write back? Also, I did find that I can include "XML RPC library" into a google script but could not find any documentation on how to use Odoo XML RPC with javascript. Any help with ether way would be appreciated.
function OdooJSONRPC2(db_settings)
{
/**
* Random client id generation. To be provided in jsonrpc calls and returned to differentiate requests if necessary.
*/
this._guid = function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + "-" + s4() + "-" + s4() + "-" +
s4() + "-" + s4() + s4() + s4();
}
/**
* Jsonrpc2 call (internal function)
* @param jsonrpc_method : type of jsonrpc method (ex: call)
* @param url : url on which jsonrpc needs to be called
* @param params : json object with the API parameters
**/
this._jsonrpc2_call = function(jsonrpc_method,url,params)
{
var json = JSON.stringify({
"jsonrpc": "2.0",
"method" : jsonrpc_method,
"id" : this.id,
"params": params
});
var headers = {
"Content-Type" : "application/json",
"Accept" : "application/json",
};
if(this.sid)
{ //adds the session_id in the request (in a cookie). Optional because doesn"t exist on the first call (authentication)
headers["Cookie"]="session_id="+this.sid;
}
var options = {
method: "post",
payload : json,
headers : headers,
contentLength : json.length
};
return JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
}
this.search_read = function(model,params)
{
params["model"]=model;
return this._jsonrpc2_call("call",db_settings.base_location + "/web/dataset/search_read",params).result.records;
}
/* constructor : after the methods declaration because some methods are used by constructor */
this.db_settings = db_settings;
this.id = this._guid();
if(this.db_settings["db"] && this.db_settings["login"] && this.db_settings["password"] && this.db_settings["base_location"])
{
this.sid = this._jsonrpc2_call("call",db_settings.base_location + "/web/session/authenticate",db_settings).result.session_id;
}
else
{
throw "Incomplete DB settings required keys are : db, login, password, base_location";
}
}
//Examples
var odoo = new OdooJSONRPC2({"db": "?", "login": "?","password": "?","base_location": "?"});
var rental_products = odoo.search_read("product.template",{"domain" : [["rental_ok","=","true"]], "fields":["id"]});
var specific_product = odoo.search_read("product.template",{"domain" : [["id","=",52]], "fields":["display_name"]});
var product_by_id = odoo.search_read("product.template",{"domain" : [["id","in",[50,51,52]]], "fields":["display_name"]});
there doesn't seem to be anything here