I have a contact list I am looping over and one of the fields in the contact object holds 1 or more contactIDs in a delimited list using a carriage return.
Standard Contact Object:
ContactId: 0030W00003kuP8bQAE
PastContactId:
0030W00003kuP8RQAU
0035400000WqTx3AAF
--
ContactId: 0030W00003kuP8bABX
PastContactId:
0030W00003kuP8RDEF
0035400000WqTx3JKL
My end goal is that anywhere that the PastContactId
is found on ExternalCustomObject
, I need to update it to the corresponding Current ContactId (ContactId
).
example pseudo code:
UPDATE ExternalCustomObject SET CurrentContactID = ContactId WHERE CurrentContactID IN PastContactId
i.e
UPDATE ExternalCustomObject SET CurrentContactID = '0030W00003kuP8bABX' WHERE CurrentContactID IN ['0030W00003kuP8RDEF', '0035400000WqTx3JKL']
The big piece here is that the list of contacts is varying so I don't think this update should be done within a loop.
I am just having trouble figuring out how to structure this so that it can build an update statement that I can run against the ExternalCustomObject
at the end, and not during the iteration.
I thought about trying something like this where it builds out an array/list and try and iterate that, but I think it would still require the update to be done within a loop and I don't know if that's frowned upon.
[
'0030W00003kuP8bQAE', ['0030W00003kuP8RQAU', '0035400000WqTx3AAF'],
'0030W00003kuP8bABX', ['0030W00003kuP8RDEF', '0035400000WqTx3JKL']
]
Any thoughts on how to approach this?
Example of a very pseudo attempt of my thinking:
// Source contact list that we will have for a merge
List<Id> sourceContacts = new List<Id>();
sourceContacts.add('0030W00003kuP8bQAE');
// Final list that will hold the current and all past Contact Ids
List<Id> finalList = new List<Id>();
try {
// Loop over contacts
for (Contact con : [Select PastContactIDs__c FROM Contact WHERE Id IN :sourceContacts]){
// Split the dupId field into a list
List<String> dupId = con.PastContactIDs__c.split('\n');
// If we have data in the list, continue
if(dupId.size() > 0){
// Can we add both the ContactID and the PastContactIDs to a list here so that we can build an update statement for later on?
for (String dup: dupId){
finalList.add(dup);
}
}
// Do we have data we need to update?
if(finalList.size() > 0){
// Attempt to update the external object
try{
// Do update Here
} catch(exception e){
System.debug(e);
}
}
}
System.debug(finalList);
} catch (exception e) {
System.debug(e);
}