Update app.py
Browse files
app.py
CHANGED
|
@@ -156,7 +156,7 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 156 |
"Status__c": status,
|
| 157 |
"Summarized_Missing_Items__c": summarized_missing,
|
| 158 |
"Alert_Sent__c": alert_sent,
|
| 159 |
-
"Client_PDF_Pack_URL__c": "
|
| 160 |
"Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
|
| 161 |
"Escalation_Flag__c": escalation_flag,
|
| 162 |
"Evaluated_At__c": evaluated_at,
|
|
@@ -172,7 +172,8 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 172 |
record_id = result.get('id')
|
| 173 |
logging.info(f"Record ID: {record_id}")
|
| 174 |
|
| 175 |
-
# Attach the PDF to the record if pdf_path exists
|
|
|
|
| 176 |
if pdf_path and os.path.exists(pdf_path):
|
| 177 |
logging.info(f"Attaching PDF to Salesforce record ID: {record_id}")
|
| 178 |
with open(pdf_path, "rb") as pdf_file:
|
|
@@ -188,17 +189,29 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 188 |
|
| 189 |
attachment_result = sf.Attachment.create(attachment)
|
| 190 |
logging.info(f"Successfully attached PDF to record: {attachment_result}")
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
else:
|
| 193 |
-
logging.warning(f"No PDF file found at {pdf_path}. Skipping attachment.")
|
| 194 |
|
| 195 |
-
return f"Record created successfully. Record ID: {record_id}. PDF attached to
|
| 196 |
except SalesforceError as e:
|
| 197 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 198 |
logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
|
| 199 |
logging.error("Check the following in your Salesforce org:")
|
| 200 |
-
logging.error("- Ensure the user has Create permission on Project_Closure_Handover__c.")
|
| 201 |
-
logging.error("- Ensure the user has permission to create Attachments.")
|
| 202 |
logging.error("- Verify field-level security for all fields in the record.")
|
| 203 |
logging.error("- Check for validation rules or required fields that might be failing.")
|
| 204 |
return f"Salesforce error: {str(e)}"
|
|
|
|
| 156 |
"Status__c": status,
|
| 157 |
"Summarized_Missing_Items__c": summarized_missing,
|
| 158 |
"Alert_Sent__c": alert_sent,
|
| 159 |
+
"Client_PDF_Pack_URL__c": "", # Will be updated after attachment is created
|
| 160 |
"Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
|
| 161 |
"Escalation_Flag__c": escalation_flag,
|
| 162 |
"Evaluated_At__c": evaluated_at,
|
|
|
|
| 172 |
record_id = result.get('id')
|
| 173 |
logging.info(f"Record ID: {record_id}")
|
| 174 |
|
| 175 |
+
# Attach the PDF to the record if pdf_path exists and update the URL
|
| 176 |
+
pdf_download_url = ""
|
| 177 |
if pdf_path and os.path.exists(pdf_path):
|
| 178 |
logging.info(f"Attaching PDF to Salesforce record ID: {record_id}")
|
| 179 |
with open(pdf_path, "rb") as pdf_file:
|
|
|
|
| 189 |
|
| 190 |
attachment_result = sf.Attachment.create(attachment)
|
| 191 |
logging.info(f"Successfully attached PDF to record: {attachment_result}")
|
| 192 |
+
attachment_id = attachment_result.get('id')
|
| 193 |
+
logging.info(f"Attachment ID: {attachment_id}")
|
| 194 |
+
|
| 195 |
+
# Construct the direct download URL for the attachment
|
| 196 |
+
pdf_download_url = f"{SF_INSTANCE_URL}/servlet/servlet.FileDownload?file={attachment_id}"
|
| 197 |
+
logging.info(f"Generated PDF download URL: {pdf_download_url}")
|
| 198 |
+
|
| 199 |
+
# Update the Project_Closure_Handover__c record with the PDF download URL
|
| 200 |
+
update_data = {
|
| 201 |
+
"Client_PDF_Pack_URL__c": pdf_download_url
|
| 202 |
+
}
|
| 203 |
+
sf.Project_Closure_Handover__c.update(record_id, update_data)
|
| 204 |
+
logging.info(f"Updated record {record_id} with Client_PDF_Pack_URL__c: {pdf_download_url}")
|
| 205 |
else:
|
| 206 |
+
logging.warning(f"No PDF file found at {pdf_path}. Skipping attachment and URL update.")
|
| 207 |
|
| 208 |
+
return f"Record created successfully. Record ID: {record_id}. PDF attached and URL set to: {pdf_download_url}"
|
| 209 |
except SalesforceError as e:
|
| 210 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 211 |
logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
|
| 212 |
logging.error("Check the following in your Salesforce org:")
|
| 213 |
+
logging.error("- Ensure the user has Create and Edit permission on Project_Closure_Handover__c.")
|
| 214 |
+
logging.error("- Ensure the user has permission to create and read Attachments.")
|
| 215 |
logging.error("- Verify field-level security for all fields in the record.")
|
| 216 |
logging.error("- Check for validation rules or required fields that might be failing.")
|
| 217 |
return f"Salesforce error: {str(e)}"
|