Skip to content

Commit 2dbd802

Browse files
committed
refactor: salesforce date field convetion added
1 parent 3b5ac6f commit 2dbd802

File tree

1 file changed

+86
-5
lines changed

1 file changed

+86
-5
lines changed

includes/Actions/Salesforce/RecordApiHelper.php

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public function generateReqDataFromFieldMap($data, $fieldMap)
3131
foreach ($fieldMap as $key => $value) {
3232
$triggerValue = $value->formField;
3333
$actionValue = $value->selesforceField;
34-
if ($triggerValue === 'custom') {
35-
$dataFinal[$actionValue] = Common::replaceFieldWithValue($value->customValue, $data);
36-
} elseif (!\is_null($data[$triggerValue])) {
37-
$dataFinal[$actionValue] = $data[$triggerValue];
38-
}
34+
35+
$dataFinal[$actionValue] = self::convertToSalesforceFormat(
36+
$triggerValue === 'custom' && isset($value->customValue)
37+
? Common::replaceFieldWithValue($value->customValue, $data)
38+
: $data[$triggerValue]
39+
);
3940
}
4041

4142
return $dataFinal;
@@ -277,4 +278,84 @@ public function execute($integrationDetails, $fieldValues, $fieldMap, $actions)
277278

278279
return true;
279280
}
281+
282+
public static function convertToSalesforceFormat($inputDate)
283+
{
284+
// First, strip any leading/trailing spaces
285+
$inputDate = trim($inputDate);
286+
287+
// Handle 2-digit year date formats (like 25/11/13 or 11/25/13)
288+
if (preg_match("/^\d{2}\/\d{2}\/\d{2}$/", $inputDate)) {
289+
// Convert DD/MM/YY or MM/DD/YY to YYYY-MM-DD
290+
$parts = explode('/', $inputDate);
291+
if (\strlen($parts[2]) == 2) {
292+
// Handling 2-digit year (assuming the year is in the 2000s)
293+
$year = '20' . $parts[2];
294+
$month = $parts[1];
295+
$day = $parts[0];
296+
$formattedDate = "{$year}-{$month}-{$day}";
297+
}
298+
// Validate and return the formatted date
299+
if (strtotime($formattedDate)) {
300+
return $formattedDate; // Return in YYYY-MM-DD format
301+
}
302+
303+
return $inputDate;
304+
}
305+
306+
// Handle formats like DD/MM/YYYY or MM/DD/YYYY
307+
if (preg_match("/^\d{2}\/\d{2}\/\d{4}$/", $inputDate)) {
308+
// Convert DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD
309+
$parts = explode('/', $inputDate);
310+
$year = $parts[2];
311+
$month = $parts[1];
312+
$day = $parts[0];
313+
$formattedDate = "{$year}-{$month}-{$day}";
314+
// Validate and return the formatted date
315+
if (strtotime($formattedDate)) {
316+
return $formattedDate; // Return in YYYY-MM-DD format
317+
}
318+
319+
return $inputDate;
320+
}
321+
322+
// Handle formats like YYYY-MM-DD
323+
if (preg_match("/^\d{4}-\d{2}-\d{2}$/", $inputDate)) {
324+
// It's already in the correct format (YYYY-MM-DD)
325+
return $inputDate;
326+
}
327+
328+
// Handle formats like YYYY/MM/DD
329+
if (preg_match("/^\d{4}\/\d{2}\/\d{2}$/", $inputDate)) {
330+
// Convert YYYY/MM/DD to YYYY-MM-DD
331+
$formattedDate = str_replace('/', '-', $inputDate);
332+
// Validate and return the formatted date
333+
if (strtotime($formattedDate)) {
334+
return $formattedDate; // Return in YYYY-MM-DD format
335+
}
336+
337+
return $inputDate;
338+
}
339+
340+
// Handle date formats with time: YYYY-MM-DD HH:MM:SS
341+
if (preg_match("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?$/", $inputDate)) {
342+
// It's a datetime with or without milliseconds
343+
$date = strtotime($inputDate);
344+
if ($date === false) {
345+
return $inputDate;
346+
}
347+
348+
// Convert to Salesforce ISO 8601 datetime with UTC (Z)
349+
return gmdate("Y-m-d\TH:i:s\Z", $date); // Convert to UTC and add Z
350+
}
351+
352+
// Handle datetime formats with ISO 8601 style (e.g., 2025-09-20T10:30:00Z or with milliseconds)
353+
if (preg_match("/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|(\.\d{1,3})?)?$/", $inputDate)) {
354+
// It's already in ISO 8601 format, return as is
355+
return strtoupper($inputDate); // Normalize to uppercase 'Z' if needed
356+
}
357+
358+
// If input contains no recognizable date/time format, return an error
359+
return $inputDate;
360+
}
280361
}

0 commit comments

Comments
 (0)