// Validate inputs
If(
IsBlank(cmbCondition.Selected),
Notify("Please select a Condition", NotificationType.Error),
IsEmpty(cmbRack.SelectedItems),
Notify("Please select at least one Rack", NotificationType.Error),
IsBlank(txtDaysShift.Value) || Value(txtDaysShift.Value) <= 0,
Notify("Please enter a valid number of days", NotificationType.Error),
// If validation passes, collect affected records BEFORE update
ClearCollect(
colAffectedRecords,
AddColumns(
Filter(
SEL,
cmbCondition.Selected.Value in Condition.Value &&
!IsBlank(LookUp(cmbRack.SelectedItems, Value = Rack))
),
OldEWD_Backup, EWD,
NewEWD_Calc, DateAdd(EWD, Value(txtDaysShift.Value), TimeUnit.Days),
ConditionText, cmbCondition.Selected.Value
)
);
// Update records
ForAll(
colAffectedRecords,
Patch(
SEL,
LookUp(SEL, ID = ID),
{EWD: NewEWD_Calc}
)
);
// Send email notification
Office365Outlook.SendEmailV2(
User().Email,
"EWD Update Notification - " & CountRows(colAffectedRecords) & " Records Modified",
"<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th { background-color: #0078D4; color: white; padding: 10px; text-align: left; }
td { border: 1px solid #ddd; padding: 8px; }
tr:nth-child(even) { background-color: #f2f2f2; }
.summary { background-color: #E8F4FD; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
.summary p { margin: 5px 0; }
</style>
</head>
<body>
<h2>EWD Update Notification</h2>
<div class='summary'>
<p><strong>Updated By:</strong> " & User().FullName & " (" & User().Email & ")</p>
<p><strong>Date & Time:</strong> " & Text(Now(), "dd/mmm/yyyy hh:mm AM/PM") & "</p>
<p><strong>Records Affected:</strong> " & CountRows(colAffectedRecords) & "</p>
<p><strong>Days Shifted:</strong> " & txtDaysShift.Value & " day(s)</p>
</div>
<h3>Updated Records:</h3>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Incubation Date</th>
<th>Mfg Date</th>
<th>Exp Date</th>
<th>Condition</th>
<th>EWD (Before)</th>
<th>EWD (After)</th>
</tr>
" &
Concat(
colAffectedRecords,
"<tr>
<td>" & Text(ID) & "</td>
<td>" & Title & "</td>
<td>" & Text(IncDate, "dd/mmm/yyyy") & "</td>
<td>" & Text(MfgDate, "dd/mmm/yyyy") & "</td>
<td>" & Text(ExpDate, "dd/mmm/yyyy") & "</td>
<td>" & ConditionText & "</td>
<td>" & Text(OldEWD_Backup, "dd/mmm/yyyy") & "</td>
<td>" & Text(NewEWD_Calc, "dd/mmm/yyyy") & "</td>
</tr>"
) &
"
</table>
<p style='margin-top: 20px; color: #666; font-size: 12px;'>This is an automated notification from the Stability Studies Management System.</p>
</body>
</html>",
{
Cc: "[email protected]; [email protected]",
Importance: "Normal"
}
);
Notify(
"Successfully updated " & CountRows(colAffectedRecords) & " records and sent notification email!",
NotificationType.Success
);
// Reset the form
Reset(cmbCondition);
Reset(cmbRack);
Reset(txtDaysShift)
)