Skip to content

Commit

Permalink
Changes fields behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
vmesel committed Nov 9, 2023
1 parent f09b8ca commit 5d62f65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
13 changes: 8 additions & 5 deletions target_salesforce_v3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]]
Expand All @@ -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"]
Expand Down Expand Up @@ -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']
Expand Down
52 changes: 23 additions & 29 deletions target_salesforce_v3/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 5d62f65

Please sign in to comment.