Skip to content
Advertisement

Update Salesforce field with Google App Script

I can get a case like this:

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)

}

This returns

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

I would like to update Document_No__c to myString

I was expecting to be able to change this to a patch and add the payload like this:

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)

}

However this is returning the error:

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

Advertisement

Answer

I needed to point to the right end point, as well as use @Tanaike’s change to the payload. Completed code example is:

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)

}

This is confirmed by the response:

200
{success=true, errors=[], created=false, id=id_num}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement