From c01229d5d15ee529dec06b6398847fc1021965c5 Mon Sep 17 00:00:00 2001 From: Key Date: Wed, 4 Dec 2024 09:38:19 -0500 Subject: [PATCH] fix link_attachment_to_object logic --- target_salesforce_v3/sinks.py | 52 +++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/target_salesforce_v3/sinks.py b/target_salesforce_v3/sinks.py index de3db57..f48c878 100644 --- a/target_salesforce_v3/sinks.py +++ b/target_salesforce_v3/sinks.py @@ -1062,29 +1062,29 @@ def upsert_record(self, record, context): def link_attachment_to_object(self, file_id, linked_object_id): - if self.name == "ContentVersion" and not linked_object_id: - self.logger.info(f"Object id not found to link file with id {file_id}") - return - - try: - # get contentdocumentid - content_endpoint = "query" - params = {"q": f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{file_id}'"} - content_document_id = self.request_api("GET", endpoint=content_endpoint, params=params) - content_document_id = content_document_id.json() - if content_document_id.get("records"): - content_document_id = content_document_id["records"][0]["ContentDocumentId"] - else: - raise Exception(f"Failed while trying to link file {file_id} and object {linked_object_id} because ContentDocumentId was not found") - - endpoint = "sobjects/ContentDocumentLink" - record = { - "ContentDocumentId": content_document_id, - "LinkedEntityId": linked_object_id, - "ShareType": "V" - } - response = self.request_api("POST", endpoint=endpoint, request_data=record) - self.logger.info(f"File with id {file_id} succesfully linked to object with id {linked_object_id}. Link id {response.json()['id']}") - except Exception as e: - self.logger.info(f"Failed while trying to link file {file_id} and object {linked_object_id}") - raise e \ No newline at end of file + if self.name == "ContentVersion": + if not linked_object_id: + self.logger.info(f"Object id not found to link file with id {file_id}") + return + try: + # get contentdocumentid + content_endpoint = "query" + params = {"q": f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{file_id}'"} + content_document_id = self.request_api("GET", endpoint=content_endpoint, params=params) + content_document_id = content_document_id.json() + if content_document_id.get("records"): + content_document_id = content_document_id["records"][0]["ContentDocumentId"] + else: + raise Exception(f"Failed while trying to link file {file_id} and object {linked_object_id} because ContentDocumentId was not found") + + endpoint = "sobjects/ContentDocumentLink" + record = { + "ContentDocumentId": content_document_id, + "LinkedEntityId": linked_object_id, + "ShareType": "V" + } + response = self.request_api("POST", endpoint=endpoint, request_data=record) + self.logger.info(f"File with id {file_id} succesfully linked to object with id {linked_object_id}. Link id {response.json()['id']}") + except Exception as e: + self.logger.info(f"Failed while trying to link file {file_id} and object {linked_object_id}") + raise e \ No newline at end of file