使用 Google 应用脚本更新 Salesforce 字段

Update Salesforce field with Google App Script

我可以得到这样的案例:

function getCase() {

    var access_token = authenticateSF();
    
    var baase = 'https://company.salesforce.com/services/data/v50.0/';
    var caseNum = '12345';

    var url = base + "query?q=SELECT+CaseNumber,Document_No__c+from+Case+WHERE+CaseNumber='" + caseNum + "'";


    var myBody = ''; // {"Document_No__c":"x"};
    var headers = {
        "Authorization": 'Bearer ' + access_token,
        "Accept": "application/json"
    };

    var params = {
        'method': 'GET',
        'muteHttpExceptions': true,
        'headers': headers,
        'contentType': 'application/json',
        'payload': myBody
    };

    var res = UrlFetchApp.fetch(url, params);
    
    var myObj = JSON.parse(res.getContentText());
    
    Logger.log(myObj)

}

这个returns

 {records=[{attributes={url=/services/data/v50.0/sobjects/Case/12345, type=Case}, CaseNumber=12345, Document_No__c=null}], done=true, totalSize=1.0}

我想将 Document_No__c 更新为 myString

我希望能够将其更改为 patch 并像这样添加有效负载:

function getCase() {

    var access_token = authenticateSF();
    
    var baase = 'https://company.salesforce.com/services/data/v50.0/';
    var caseNum = '12345';

    var url = base + "query?q=SELECT+CaseNumber,Document_No__c+from+Case+WHERE+CaseNumber='" + caseNum + "'";

    var myString = 'myString'
    var myBody = {"Document_No__c":myString};
    var headers = {
        "Authorization": 'Bearer ' + access_token,
        "Accept": "application/json"
    };

    var params = {
        'method': 'PATCH',
        'muteHttpExceptions': true,
        'headers': headers,
        'contentType': 'application/json',
        'payload': myBody
    };

    var res = UrlFetchApp.fetch(url, params);
    
    var myObj = JSON.parse(res.getContentText());
    
    Logger.log(myObj)

}

然而这是返回错误:

405
Message: HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET
Error: METHOD_NOT_ALLOWED

我需要指向正确的终点,以及使用@Tanaike 对负载的更改。完成的代码示例是:

function getCase() {

    var access_token = authenticateSF();
    
    var baase = 'https://company.salesforce.com/services/data/v50.0/';
    var caseNum = '12345';

    var url = base + 'sobjects/Case/CaseNumber/' + caseNum;

    var myString = 'myString'
    var myBody = {"Document_No__c":myString};
    var headers = {
        "Authorization": 'Bearer ' + access_token,
        "Accept": "application/json"
    };

    var params = {
        'method': 'PATCH',
        'muteHttpExceptions': true,
        'headers': headers,
        'contentType': 'application/json',
        'payload': JSON.stringify(myBody)
    };

    var res = UrlFetchApp.fetch(url, params);
    
    var myObj = JSON.parse(res.getContentText());
    
    Logger.log(myObj)

}

回复证实了这一点:

200
{success=true, errors=[], created=false, id=id_num}