我已成功将Google Apps脚本认证为Salesforce,并且可以从Google Apps脚本查询Salesforce中的记录。
下一个障碍是通过Google Apps脚本更新Salesforce中的单个记录。这是我到目前为止的内容:
我从Patch payload question in Google Script REST API 的代码开始
function updateQuoteUrls() {
//custom object name i360__Quote__c
var quoteId = "ABC123"; // ID
var folderUrl = "https://drive.google.com/drive/folders/DEFXXX"; // Google_Drive_Folde_URL__c
var sheetUrl = "https://docs.google.com/spreadsheets/d/XYZ123"; // Pricing_Sheet_URL__c
var instanceUrl = "https://na53.salesforce.com";
var queryUrl = instanceUrl + "/services/data/v25.0/sobjects/Account/"+MyId+"?_HttpMethod=PATCH";
var payload = "{\"Google_Drive_Folde_URL__c\":\" + folderUrl + "\;\"Pricing_Sheet_URL__c\":\" + sheetUrl + \"}";
var headers =
{
"Authorization" : "OAuth "+accessToken,
"Content-type" : "application/json"
};
var options =
{
headers : headers,
method : "POST",
payload : payload
};
var response = UrlFetchApp.fetch(queryUrl, options);
}
问题:如何访问自定义对象和特定记录?我没有定义MyId。那是记录的ID吗?
这是我更新的代码:(更接近,但仍然无法正常工作。)
function updateQuoteUrls3() {
var service = getService();
var accessToken = service.getAccessToken();
var quoteId = "a1Gf200000GkZex"; // ID
var folderUrl = "https://drive.google.com/drive/folders/DEFXXX"; // Google_Drive_Folde_URL__c
var instanceUrl = "https://na53.salesforce.com";
var queryUrl = "/services/data/v25.0/sobjects/i360__Quote__c/" + quoteId + ".json";
var payload = {
"Google_Drive_Folde_URL__c": folderUrl,
"Pricing_Sheet_URL__c": sheetUrl
};
var headers = {
//"Authorization": "Bearer " + accessToken, // Double-check if you should use Bearer or OAuth. (I use Bearer.)
"Authorization": "OAuth " + accessToken,
"Content-Type": "application/json"
};
var options = {
"method": "patch",
"contentType": "application/json",
"headers": headers,
"payload": JSON.stringify(payload)
};
var url = instanceUrl + queryUrl;
var response = UrlFetchApp.fetch(url, options);
return response; // HTTPresponse - https://developers.google.com/apps-script/reference/url-fetch/http-response
}
答案 0 :(得分:0)
您正确假设MyId
的值应该是记录ID。您只需在Id
语句中包含SELECT
,就可以轻松地从原始查询中获得该信息。
实际上,您不需要在URL后面附加?_HttpMethod=PATCH
,因为您可以将PATCH指定为请求中的方法(如advanced params documentation中所列)。
您需要做的是将.json
附加到请求的末尾,如Salesforce documentation中暗含的指示。
我建议通过将payload
创建为常规JS对象,然后使用JSON.stringify()
对其进行字符串化处理来简化您的生活。如果这样做,您将不必执行当前正在执行的所有字符串连接和字符转义。
表面上,您的代码随后可以修改为:
function updateQuoteUrls(recordId) {
var quoteId = "ABC123"; // ID
var folderUrl = "https://drive.google.com/drive/folders/DEFXXX"; // Google_Drive_Folde_URL__c
var sheetUrl = "https://docs.google.com/spreadsheets/d/XYZ123"; // Pricing_Sheet_URL__c
var instanceUrl = "https://na53.salesforce.com";
var queryUrl = "/services/data/v25.0/sobjects/Account/" + recordId + ".json";
var payload = {
"Google_Drive_Folde_URL__c": folderUrl,
"Pricing_Sheet_URL__c": sheetUrl
};
var headers = {
"Authorization": "Bearer " + accessToken, // Double-check if you should use Bearer or OAuth. (I use Bearer.)
"Content-Type": "application/json"
};
var options = {
"method": "patch",
"contentType": "application/json",
"headers": headers,
"payload": JSON.stringify(payload)
};
var url = instanceUrl + queryUrl;
var response = UrlFetchApp.fetch(url, options);
return response; // HTTPresponse - https://developers.google.com/apps-script/reference/url-fetch/http-response
}