diff --git a/target_salesforce_v3/client.py b/target_salesforce_v3/client.py index 8f58cf6..57e8732 100644 --- a/target_salesforce_v3/client.py +++ b/target_salesforce_v3/client.py @@ -124,7 +124,7 @@ def process_record(self, record: dict, context: dict) -> None: # Getting custom fields from record # self.process_custom_fields(record) - fields = self.sf_fields_description + fields = self.sf_fields_description() for field in fields["external_ids"]: if record.get(field): @@ -234,7 +234,8 @@ def sf_fields_description(self, object_type=None): return fields def get_pickable(self, record_field, sf_field, default=None, select_first=False): - pickable_fields = self.sf_fields_description["pickable"] + fields_dict = self.sf_fields_description() + pickable_fields = fields_dict["pickable"] if sf_field not in pickable_fields: return default valid_options = [re.sub(r'\W+', '', choice).lower() for choice in pickable_fields[sf_field]] @@ -259,10 +260,11 @@ def sf_field_detais(self, field_name): def validate_output(self, mapping): mapping = self.clean_payload(mapping) payload = {} - if not self.sf_fields_description["createable"]: + fields_dict = self.sf_fields_description() + if not fields_dict["createable"]: raise NoCreatableFieldsException(f"No creatable fields for stream {self.name} stream, check your permissions") for k, v in mapping.items(): - if k.endswith("__c") or k in self.sf_fields_description["createable"] + ["Id"]: + if k.endswith("__c") or k in fields_dict["createable"] + ["Id"]: payload[k] = v # required = self.sf_fields_description["required"] @@ -292,7 +294,8 @@ def process_custom_fields(self, record) -> None: return None # Checking if the custom fields already exist in - salesforce_custom_fields = self.sf_fields_description['custom'] + fields_dict = self.sf_fields_description() + salesforce_custom_fields = fields_dict['custom'] for cf in record: cf_name = cf['name'] diff --git a/target_salesforce_v3/sinks.py b/target_salesforce_v3/sinks.py index af3c7cd..9d417a0 100644 --- a/target_salesforce_v3/sinks.py +++ b/target_salesforce_v3/sinks.py @@ -184,7 +184,8 @@ def process_record(self, record, context): if record.get("Id"): fields = ["Id"] else: - fields = self.sf_fields_description["external_ids"] + fields_dict = self.sf_fields_description() + fields = fields_dict["external_ids"] for field in fields: if record.get(field): @@ -465,7 +466,7 @@ def preprocess_record(self, record, context): installment_period, "npe03__Installment_Period__c" ) - self.sf_fields_description + fields_dict = self.sf_fields_description() if record.get("created_at"): created_at = parse(record.get("created_at")) else: @@ -568,7 +569,8 @@ def process_record(self, record, context): if record.get("Id"): fields = ["Id"] else: - fields = self.sf_fields_description["external_ids"] + fields_dict = self.sf_fields_description() + fields = fields_dict["external_ids"] for field in fields: if record.get(field): @@ -791,31 +793,23 @@ def process_record(self, record, context): if len(missing_fields) > 0.5 * len(fields): self.logger.info(f"This record may require more fields to be mapped. Missing fields: {missing_fields}") - for field in fields: - if record.get(field): - try: - update_record = record.copy() - update_record.pop(field) - url = "/".join([endpoint, field, record[field]]) - response = self.request_api( - "PATCH", endpoint=url, request_data=update_record - ) - id = response.json().get("id") - self.logger.info(f"{self.name} updated with id: {id}") - return - except: - self.logger.info(f"{field} with id {record[field]} does not exist. \nWill attepmt to create it.") - record = update_record - - try: - response = self.request_api("POST", endpoint=endpoint, request_data=record) - id = response.json().get("id") - self.logger.info(f"{self.name} created with id: {id}") - except Exception as e: - self.logger.exception("Error encountered while creating campaign") - raise e - - - + if record.get("Id") or record.get("id"): + object_id = record.pop("Id") or record.pop("id") + url = "/".join([endpoint, object_type, object_id]) + try: + response = self.request_api("PATCH", endpoint=url, request_data=record) + id = response.json().get("id") + self.logger.info(f"{object_type} updated with id: {id}") + except Exception as e: + self.logger.exception(f"Error encountered while updating {object_type}") + raise e + else: + try: + response = self.request_api("POST", endpoint=endpoint, request_data=record) + id = response.json().get("id") + self.logger.info(f"{object_type} created with id: {id}") + except Exception as e: + self.logger.exception(f"Error encountered while creating {object_type}") + raise e