1

I have a method (shown below in Python) that tries to insert or update 3 related records based on various criteria via composite REST API. I figure out whether each should be posted or patched and if it exists already, I give it the ID of the record I want to update.

 def upsertAccountContactLSP(self, familyInfo, contactInfo, lspInfo):
        #if there's no account
        if self.recordDict['Account']["id"] == "":
            accountMethod = "POST"
            accountId = ""
            accountRecordType = self.recordTypes['Account']['LSP_ORI'] 
        else:
            accountMethod = "PATCH"
            accountId = self.recordDict['Account']['id'] #string
            accountRecordType = self.figureOutRecordType('Account')

        #if there's no contact
        if self.recordDict['Contact']["id"] == "":
            contactMethod = "POST"
            contactId = ""
            contactRecordType = self.recordTypes['Contact']['LSP_ORI']
        else:
            contactMethod = "PATCH"
            contactId = self.recordDict['Contact']['id']
            contactRecordType = self.figureOutRecordType('Contact')

        # if there's no lsp
        if self.recordDict['LSP']["id"] == "":
            lspMethod = "POST"
            lspId = ""
        else:
            lspMethod = "PATCH"
            lspId = self.recordDict['LSP']['id']
        lspRecordType = self.recordTypes['LSP_ORI']['LSP'] 

        compositeData = {
            "compositeRequest": [{
                "method": accountMethod,
                "url": "/services/data/v50.0/sobjects/Account/" + accountId,
                "referenceId": "refAccount",
                "body":  {**familyInfo, "RecordTypeId": accountRecordType} #, "Id": accountId - took out because of error
            },{
                "method": contactMethod,
                "url": "/services/data/v50.0/sobjects/Contact/" + contactId,
                "referenceId": "refContact",
                "body": {**contactInfo, "AccountId" : "@{refAccount.id}", "RecordTypeId": contactRecordType}
            },{
                "method": lspMethod,
                "url": "/services/data/v50.0/sobjects/LSP_ORI__c/" + lspId,
                "referenceId": "refLSP",
                "body": {**lspInfo, "Account_Name__c" : "@{refAccount.id}", "Soldiers_Name__c" : "@{refContact.id}", "RecordTypeId": lspRecordType}
            }
            ]
        }

        result = self.sf_api_call('/services/data/v50.0/composite/', method="post", data=compositeData)
        if('errorCode' in result['compositeResponse'][i]['body'][0] for i in range(len(result['compositeResponse']))):
            print("An error occured. See result:")
            print(result)
        return result

The response I get from the first patch to an Account record is [{'body': None, 'httpHeaders': {}, 'httpStatusCode': 204, 'referenceId': 'refAccount'}], which does not include an 'id' field. I believe that is why when running the above composite request I get the following error from the second request: 'Invalid reference specified. No value for refAccount.id found in refAccount'}.

How can I fix my composite request while still allowing the flexibility of posting or patching based on external criteria?

1 Answer 1

0

Below is my updated method. It seems to be working for me now. I had to do some preprocessing to send in the proper body to the composite request. If the account existed already (before posting or patching within the composite request), I use the accountId that I already had. Otherwise I used the refAccount.id which comes from the account that is created within the composite request. Same idea for connecting my lsp object to Contact.

    def upsertAccountContactLSP(self, familyInfo, contactInfo, lspInfo):
        
        #if there's no account
        if self.recordDict['Account']["id"] == "":
            accountMethod = "POST"
            accountId = ""
            accountRecordType = self.recordTypes['Account']['LSP_ORI'] #once it exists, get ID by going to record types under object explorer, click on the record type and look in the URL
        else:
            accountMethod = "PATCH"
            accountId = self.recordDict['Account']['id'] #string
            accountRecordType = self.figureOutRecordType('Account')

        #if there's no contact
        if self.recordDict['Contact']["id"] == "":
            contactMethod = "POST"
            contactId = ""
            contactRecordType = self.recordTypes['Contact']['LSP_ORI']
            contactBody = {**contactInfo, "AccountId" : accountId, "RecordTypeId": contactRecordType} if accountId != "" else {**contactInfo, "AccountId" : "@{refAccount.id}", "RecordTypeId": contactRecordType}
        else:
            contactMethod = "PATCH"
            contactId = self.recordDict['Contact']['id']
            contactRecordType = self.figureOutRecordType('Contact')
            contactBody = {**contactInfo, "RecordTypeId": contactRecordType}

        # if there's no lsp
        if self.recordDict['LSP']["id"] == "":
            lspMethod = "POST"
            lspId = ""
            lspRecordType = self.recordTypes['LSP_ORI']['LSP'] #regardless if exists or not, lsp record type should always be LSP and not ORI
            lspBody = {**lspInfo, "Account_Name__c" : accountId, "RecordTypeId": lspRecordType} if accountId != "" else {**lspInfo, "Account_Name__c" : "@{refAccount.id}", "RecordTypeId": lspRecordType}
            lspBody["Contact_Name__c"] = contactId if contactId != "" else "@{refContact.id}"
        else:
            lspMethod = "PATCH"
            lspId = self.recordDict['LSP']['id']
            lspBody = lspInfo

        compositeData = {
            "compositeRequest": [{
                "method": accountMethod,
                "url": "/services/data/v50.0/sobjects/Account/" + accountId,
                "referenceId": "refAccount",
                "body":  {**familyInfo, "RecordTypeId": accountRecordType}

            },{
                "method": contactMethod,
                "url": "/services/data/v50.0/sobjects/Contact/" + contactId,
                "referenceId": "refContact",
                "body": contactBody
            },{
                "method": lspMethod,
                "url": "/services/data/v50.0/sobjects/LSP_ORI__c/" + lspId,
                "referenceId": "refLSP",
                "body": lspBody
            }]
        }

        result = self.sf_api_call('/services/data/v50.0/composite/', method="post", data=compositeData)
        if('errorCode' in result['compositeResponse'][i]['body'][0] for i in range(len(result['compositeResponse']))):
            print("An error occured. See result:")
            print(result)
        return result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .