From 34c1f78d6de215d484de2c10b5bfd355a08fcc6e Mon Sep 17 00:00:00 2001 From: Vinicius Mesel <4984147+vmesel@users.noreply.github.com> Date: Mon, 18 Dec 2023 16:10:34 -0300 Subject: [PATCH] Adds support for custom_field --- target_salesforce_v3/client.py | 11 ++++++++++- target_salesforce_v3/sinks.py | 14 ++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/target_salesforce_v3/client.py b/target_salesforce_v3/client.py index 9b64f8f..9ef8d73 100644 --- a/target_salesforce_v3/client.py +++ b/target_salesforce_v3/client.py @@ -47,6 +47,15 @@ def http_headers(self) -> dict: headers["User-Agent"] = self.config.get("user_agent") return headers + def get_fields_for_object(self, object_type): + """Check if Salesforce has an object type and fetches its fields.""" + req = self.request_api("GET", endpoint="sobjects/") + for object in req.json().get("sobjects", []): + if object["name"] == object_type or object["label"] == object_type or object["labelPlural"] == object_type: + obj_req = self.request_api("GET", endpoint=f"sobjects/{object['name']}/describe").json() + return {f["name"]: f for f in obj_req.get("fields", [])} + + def validate_response(self, response: requests.Response) -> None: """Validate HTTP response.""" if response.status_code in [429] or 500 <= response.status_code < 600: @@ -307,7 +316,7 @@ def process_custom_fields(self, record) -> None: if cf_name not in salesforce_custom_fields: # If there's a custom field in the record that is not in Salesforce # create it - self.add_custom_field(cf['name'],label = cf.get('label')) + self.add_custom_field(cf['name'], label = cf.get('label')) return None diff --git a/target_salesforce_v3/sinks.py b/target_salesforce_v3/sinks.py index 53d905a..c29ccc4 100644 --- a/target_salesforce_v3/sinks.py +++ b/target_salesforce_v3/sinks.py @@ -32,16 +32,7 @@ def reference_data(self): response = response.json()["records"] return [{k: v for k, v in r.items() if k in ["Id", "Name"]} for r in response] - def preprocess_record(self, record, context): - - if isinstance(record.get("addresses"), str): - record["addresses"] = json.loads(record["addresses"]) - - if isinstance(record.get("phone_numbers"), str): - record["phone_numbers"] = json.loads(record.get("phone_numbers")) - - if isinstance(record.get("campaigns"), str): - record["campaigns"] = json.loads(record.get("campaigns")) + def preprocess_record(self, record: dict, context: dict): record = self.validate_input(record) @@ -209,7 +200,6 @@ def upsert_record(self, record, context): except Exception as e: self.logger.exception(f"Could not PATCH to {url}: {e}") if record: - try: response = self.request_api("POST", request_data=record) id = response.json().get("id") @@ -242,7 +232,7 @@ def validate_response(self, response): msg = self.response_error_message(response) raise FatalAPIError(msg) - def assign_to_campaign(self, contact_id, campaigns): + def assign_to_campaign(self,contact_id,campaigns:list) -> None: """ This function recieves a contact_id and a list of campaigns and assigns the contact_id to each campaign