This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeUtil.cls
100 lines (88 loc) · 3.43 KB
/
MergeUtil.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Merge one or more sObject records with a string that contains merge fields in format {!SOBJECT_NAME.FIELD_NAME}
*
* Requires the QueryUtil class.
*
* @author Luke
* @date Apr 2013
*/
public with sharing class MergeUtil {
public class MergeException extends Exception {}
public static final Integer MAX_RECORDS = 10;
public static final String DATE_FORMAT = 'EEEE, MMMM d, yyyy';
// public static final String DATE_FORMAT = 'yyyy-MM-dd HH:mm:ss';
/**
* This static method returns a merged string given a string with merge fields and one or more record Ids.
* @param toMerge Unmerged string that contains merge fields in format {!SOBJECT_NAME.FIELD_NAME}.
* @param recordIds Id list of the records that should be merged.
* @return The merged string.
*/
public static String mergeRecords(String toMerge, List<ID> recordIds) {
if(toMerge == null) {
throw new MergeException('Unmerged input cannot be null');
}
String merged = toMerge;
if(recordIds.size() > MAX_RECORDS) {
throw new MergeException('Maximum of ' + MAX_RECORDS + ' can be merged');
}
for(ID recordId : recordIds) {
if(recordId != null) {
merged = mergeSingleRecord(merged, recordId);
} else {
System.debug('Found null record Id');
}
}
return merged;
}
/**
* This static method returns a merged string given a string with merge fields and a single record Id.
* @param toMerge Unmerged string that contains merge fields in format {!SOBJECT_NAME.FIELD_NAME}.
* @param recordId Id of the record that should be merged.
* @return The merged string.
*/
public static String mergeSingleRecord(String toMerge, ID recordId) {
if(toMerge == null) {
throw new MergeException('Unmerged input cannot be null');
}
String merged = toMerge;
String typeName = recordId.getSObjectType().getDescribe().getName();
String queryStr = 'Select * From ' + typeName + ' Where Id = \'' + recordId + '\'';
List<SObject> sobjectList = QueryUtil.query(queryStr);
if(sobjectList.size() != 1) {
throw new MergeException('Record with Id \'' + recordId + '\' does not exist (sObject type is ' + typeName);
}
SObject record = sobjectList[0];
Schema.SObjectType sobjectType = Schema.getGlobalDescribe().get(typeName);
Map<String, Schema.SObjectField> fieldMap = sobjectType.getDescribe().fields.getMap();
for(Schema.SObjectField f : fieldMap.values()) {
Schema.DescribeFieldResult describeField = f.getDescribe();
String fieldName = describeField.getName();
Object fieldVal = record.get(fieldName);
String strVal = null;
// If this field is a Date/time, format it in EST:
if(describeField.getType() == Schema.DisplayType.DateTime) {
if(fieldVal != null) {
Datetime dt = (Datetime)fieldVal;
strVal = dt.format(DATE_FORMAT, 'America/New_York');
} else {
strVal = '';
}
} else if(describeField.getType() == Schema.DisplayType.Date) {
if(fieldVal != null) {
Date d = (Date)fieldVal;
Datetime dt = Datetime.newInstance(d, Time.newInstance(0, 0, 0, 0));
strVal = dt.format(DATE_FORMAT, 'America/New_York');
} else {
strVal = '';
}
} else {
strVal = fieldVal != null ? String.valueOf(fieldVal) : '';
}
// Merge field:
merged = merged.replace('{!' + typeName + '.' + fieldName + '}', strVal);
String link = URL.getSalesforceBaseUrl().toExternalForm() + '/' + record.Id;
merged = merged.replace('{!' + typeName + '.Link}', link);
}
return merged;
}
}