Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError when creating an object in update mode #6

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions src/jazkarta/easyformplugin/salesforce/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,47 @@ def onSuccess(self, fields, request):
sobject = getattr(sf, sobject_name)
op_name = operation["operation"]
if op_name == "create":
result = sobject.create(data)
if result["success"]:
sf_id = result["id"]
logger.info(
"Created {} {} in Salesforce".format(sobject_name, sf_id)
)
else:
raise Exception(
"Failed to create {} in Salesforce: {}".format(
sobject_name, result["errors"]
)
)
self._create_sf_object(sobject, data)
elif op_name == "update":
sf_id = request.cookies.get("sf_id")
if sf_id:
result = sobject.update(sf_id, data)
status = sobject.update(sf_id, data)
if status == 204:
request.response.expireCookie(
"sf_id", path=form.absolute_url_path()
)
logger.info(
"Updated {} {} in Salesforce".format(sobject_name, sf_id)
)
else:
raise Exception(
"Failed to update {} {} in Salesforce: {}".format(
sobject_name, sf_id, status
)
)
else:
if operation.get("action_if_no_existing_object") == "create":
result = sobject.create(data)
sf_id = result["Id"]
self._create_sf_object(sobject, data)
else:
raise Exception("No Salesforce object matched for update")
if result == 204:
request.response.expireCookie(
"sf_id", path=form.absolute_url_path()
)
logger.info(
"Updated {} {} in Salesforce".format(sobject_name, sf_id)
)
else:
raise Exception(
"Failed to update {} {} in Salesforce: {}".format(
sobject_name, sf_id, result
)
)
else:
raise ValueError("Unsupported operation: {}".format(operation))

def _create_sf_object(self, sobject, data):
result = sobject.create(data)
if result["success"]:
sf_id = result["id"]
logger.info(
"Created {} {} in Salesforce".format(sobject.name, sf_id)
)
else:
raise Exception(
"Failed to create {} in Salesforce: {}".format(
sobject.name, result["errors"]
)
)
return sf_id

def prepare_salesforce_data(self, fields, form_input, expr_context):
"""Collect data for one Salesforce object in the format expected by simple-salesforce

Expand Down
Loading