0

I have this script that gets time entries from a time tracking tool and that I'm trying to push into a custom object in Salesforce : Time_Entry__c. After following this help page : https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm I came up with this script. The issue I'm facing is that I manage to create new records, but I can't update existing one, I receive this error every time : BadRequest 400 Bad Request. What is weird is that the same request works for inserting record.

Has anyone an idea on what might cause this behavior? I've looked everywhere and coulnd't find many examples of upserts.

Thanks in advance.

Notes : the #{id} in the url is the external ID of the time entry, not the Salesforce one.

url = URI("#{instance_url}/services/data/v35.0/sobjects/Time_Entry__c/Toggl_ID__c/#{id}")
    p url
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Patch.new(url)
    # request["method"] = "patch"
    request["Authorization"] = "Bearer #{token}"
    request["Content-Type"] = 'application/json'
    case project
    when "Strategic Accompaniment", "Technical Accompaniment"
      billable = true
    else
      billable = false
    end
    p "date : #{date}, duration : #{duration}, billable : #{billable} project : #{project}, consultant :  #{consultant}, details : #{details}"
    body = {
              Account__c: "0015800001GR3Wl",
              Duration__c: duration,
              Date__c: date,
              Project__c: project,
              Consultant__c: "Brieuc",
              Details__c: "Jean test"
            }.to_json
    request.body = body
    response = http.request(request)
  • Do you get any error message or it's just generic 400? I see this on the documentation, just in case if yours has a similar scenario -- If the relationship type is master-detail and the relationship is set to not allow reparenting, and you try to update the parent external ID, you get an HTTP status code 400 error with an error code of INVALID_FIELD_FOR_INSERT_UPDATE. – Jayant Das May 3 '18 at 13:46
  • Perhaps you have duplicate ID values in your database? What is the response body from the call-out? – sfdcfox May 3 '18 at 13:56
  • @Jayant Das and sfdcfox, thanks for your answers, indeed the error code is "INVALID_FIELD_FOR_INSERT_UPDATE" because of the Account__c field. There is a master-detail relationship between time entries and account. So I thought I might allow the reparenting of child record (time entries in this case), but now I'm getting no response from the request : simply nil. – Brieuc LAMBERT May 3 '18 at 14:24
  • Did you verify if the record was updated? I see that in case of updates, I really didn't see any response (was using postman, so not sure if I was looking at wrong place) but only the successful status code 204, which kind of states that the record was updated successfully. – Jayant Das May 3 '18 at 14:32
0

Rolling up the comments as an answer so that it helps anyone in same situation.

Based on the information in comment, the error code being returned was:

INVALID_FIELD_FOR_INSERT_UPDATE

The object in consideration is a detail record of Account and that the request consisted of account id here. Per the documentation(buried down in the last section of the page), this is what it states in case of updates around master-detail relationship:

If the relationship type is master-detail and the relationship is set to not allow reparenting, and you try to update the parent external ID, you get an HTTP status code 400 error with an error code of INVALID_FIELD_FOR_INSERT_UPDATE.

Once you allow re-parenting in this scenario, updates should work perfectly fine.

Additional note - During my test, I observed that in case of successful update using external id, there's an empty response body returned. You only get to see the status code 204, which signifies that the record was successfully updated. So if you don't see anything in the response, look for the status code and verify that the record was updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.