Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ketan-benegal/62eae5b337223923fcbb0c0d718af91f to your computer and use it in GitHub Desktop.
Save ketan-benegal/62eae5b337223923fcbb0c0d718af91f to your computer and use it in GitHub Desktop.
public static void cleanEmailFields(Map < String, List < String >> mSobjField) {
// Iterate through SObject->Email Fields
for (String sObjName: mSobjField.keySet()) {
// Build a list of all fields that need to be queried
List < String > lstEmailFields = mSobjField.get(sObjName);
// Generate a SOQL query to get records with non null emails
String soql = createSOQL(sObjName, lstEmailFields);
List < SObject > sObjRecords = new List < SObject > ();
// Iterate through SObject records
for (SObject sObjRecord: Database.query(soql)) {
// Iterate through email fields found on SObject and replace with dummy values
for (String strField: lstEmailFields) {
String strEmail = (String) sObjRecord.get(strField);
if (String.isEmpty(strEmail)) continue;
sObjRecord.put(strField, strEmail.replaceAll('\\W', '_') + '@invalid.invalid.co');
}
sObjRecords.add(sObjRecord);
}
update sObjRecords;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment