Update app.py
Browse files
app.py
CHANGED
|
@@ -101,6 +101,26 @@ def init_salesforce():
|
|
| 101 |
logging.error("Check your Salesforce org settings, network restrictions, or API access.")
|
| 102 |
raise
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Summarize text using Hugging Face Inference API
|
| 105 |
def summarize_text(text, max_length=100, min_length=30):
|
| 106 |
if not HF_AVAILABLE:
|
|
@@ -137,8 +157,8 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 137 |
summarized_checklist = summarize_text(checklist_summary)
|
| 138 |
summarized_missing = summarize_text(missing_summary)
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
|
| 143 |
# Ensure inputs are properly formatted
|
| 144 |
score = float(score) if score is not None else 0.0
|
|
@@ -157,7 +177,7 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 157 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
| 158 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 159 |
|
| 160 |
-
# Create the record in Project_Closure_Handover__c without the PDF URL
|
| 161 |
record = {
|
| 162 |
"Readiness_Score__c": score,
|
| 163 |
"Checklist_Summary__c": checklist_summary,
|
|
@@ -173,9 +193,12 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 173 |
"Open_Punch_Items__c": open_punch_items,
|
| 174 |
"Punch_List__c": punch_list_text,
|
| 175 |
"QA_Report__c": qa_report
|
| 176 |
-
# Project_ID__c omitted as it's a Lookup field
|
| 177 |
}
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
logging.debug(f"Attempting to create Salesforce record in Project_Closure_Handover__c with data: {record}")
|
| 180 |
result = sf.Project_Closure_Handover__c.create(record)
|
| 181 |
logging.info(f"Successfully created Salesforce record: {result}")
|
|
@@ -215,14 +238,15 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 215 |
else:
|
| 216 |
logging.warning(f"No PDF file found at {pdf_path}. Skipping attachment and URL update.")
|
| 217 |
|
| 218 |
-
|
|
|
|
| 219 |
except simple_salesforce.SalesforceError as e:
|
| 220 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 221 |
logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
|
| 222 |
logging.error("Check the following in your Salesforce org:")
|
| 223 |
logging.error("- Ensure the user has Create and Edit permission on Project_Closure_Handover__c.")
|
| 224 |
logging.error("- Ensure the user has permission to create and read Attachments.")
|
| 225 |
-
logging.error("- Verify field-level security for all fields in the record.")
|
| 226 |
logging.error("- Check for validation rules or required fields that might be failing.")
|
| 227 |
return f"Salesforce error: {str(e)}"
|
| 228 |
except Exception as e:
|
|
|
|
| 101 |
logging.error("Check your Salesforce org settings, network restrictions, or API access.")
|
| 102 |
raise
|
| 103 |
|
| 104 |
+
# Function to query a valid Project ID from the related object (assumed to be Project__c)
|
| 105 |
+
def get_project_id(sf):
|
| 106 |
+
try:
|
| 107 |
+
# Query the most recent active project (modify the WHERE clause as needed)
|
| 108 |
+
project_query = sf.query("SELECT Id FROM Project__c WHERE IsActive__c = true ORDER BY CreatedDate DESC LIMIT 1")
|
| 109 |
+
if project_query['records']:
|
| 110 |
+
project_id = project_query['records'][0]['Id']
|
| 111 |
+
logging.info(f"Found Project ID: {project_id}")
|
| 112 |
+
return project_id
|
| 113 |
+
else:
|
| 114 |
+
logging.warning("No active Project__c records found. Omitting Project_ID__c.")
|
| 115 |
+
return None
|
| 116 |
+
except simple_salesforce.SalesforceError as e:
|
| 117 |
+
logging.error(f"Error querying Project__c: {str(e)}")
|
| 118 |
+
logging.error("Possible issues: Object does not exist, insufficient permissions, or incorrect field name.")
|
| 119 |
+
return None
|
| 120 |
+
except Exception as e:
|
| 121 |
+
logging.error(f"Unexpected error while querying Project ID: {str(e)}")
|
| 122 |
+
return None
|
| 123 |
+
|
| 124 |
# Summarize text using Hugging Face Inference API
|
| 125 |
def summarize_text(text, max_length=100, min_length=30):
|
| 126 |
if not HF_AVAILABLE:
|
|
|
|
| 157 |
summarized_checklist = summarize_text(checklist_summary)
|
| 158 |
summarized_missing = summarize_text(missing_summary)
|
| 159 |
|
| 160 |
+
# Query a valid Project ID for the Lookup field
|
| 161 |
+
project_id = get_project_id(sf)
|
| 162 |
|
| 163 |
# Ensure inputs are properly formatted
|
| 164 |
score = float(score) if score is not None else 0.0
|
|
|
|
| 177 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
| 178 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 179 |
|
| 180 |
+
# Create the record in Project_Closure_Handover__c without the PDF URL for now
|
| 181 |
record = {
|
| 182 |
"Readiness_Score__c": score,
|
| 183 |
"Checklist_Summary__c": checklist_summary,
|
|
|
|
| 193 |
"Open_Punch_Items__c": open_punch_items,
|
| 194 |
"Punch_List__c": punch_list_text,
|
| 195 |
"QA_Report__c": qa_report
|
|
|
|
| 196 |
}
|
| 197 |
|
| 198 |
+
# Include Project_ID__c only if a valid ID was found
|
| 199 |
+
if project_id:
|
| 200 |
+
record["Project_ID__c"] = project_id
|
| 201 |
+
|
| 202 |
logging.debug(f"Attempting to create Salesforce record in Project_Closure_Handover__c with data: {record}")
|
| 203 |
result = sf.Project_Closure_Handover__c.create(record)
|
| 204 |
logging.info(f"Successfully created Salesforce record: {result}")
|
|
|
|
| 238 |
else:
|
| 239 |
logging.warning(f"No PDF file found at {pdf_path}. Skipping attachment and URL update.")
|
| 240 |
|
| 241 |
+
project_id_message = f"Project ID: {project_id}" if project_id else "Project ID: Not set (no active Project__c records found)"
|
| 242 |
+
return f"Record created successfully. Record ID: {record_id}. PDF attached and URL set to: {pdf_download_url}. {project_id_message}"
|
| 243 |
except simple_salesforce.SalesforceError as e:
|
| 244 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 245 |
logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
|
| 246 |
logging.error("Check the following in your Salesforce org:")
|
| 247 |
logging.error("- Ensure the user has Create and Edit permission on Project_Closure_Handover__c.")
|
| 248 |
logging.error("- Ensure the user has permission to create and read Attachments.")
|
| 249 |
+
logging.error("- Verify field-level security for all fields in the record, including Project_ID__c.")
|
| 250 |
logging.error("- Check for validation rules or required fields that might be failing.")
|
| 251 |
return f"Salesforce error: {str(e)}"
|
| 252 |
except Exception as e:
|