function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve Connelly 5Steve Connelly 5 

Creating a self service deduping tool with Flow and Invokable Method - Stuck on the code

I am so close on this one I think. I have built an invocable method to pass two contacts from a flow into Apex to merge the contacts but I am stuck in the part of the code to pass the IDs into the merge command.

Here is the code:

User-added image

The code indicates that there the survivorCont and mergeCont variables do not exist.

I feel like I am very close on this one but could ureally use some help taking this accross the finish line.

Any thought sout there on this one?
Thanks much
Steve
Best Answer chosen by Steve Connelly 5
Pawel BorysPawel Borys
First of all the variables are declared inside the loop so they are not visible outside of it.

But you don't have to query the records if you already have ids.
for (mergeRecords record : recordIds) {
    Contact survivorCon = new Contact (Id = record.survivorId);
    Contact mergeCon = new Contact (Id = record.mergeId);
    merge survivorCon mergeCon;
}

Bear in mind that it can hit the 150 DMLs limit because it's inside a loop. Unfortunately the merge method doesn't seem to work with lists but only single ids/records.
 

All Answers

Pawel BorysPawel Borys
First of all the variables are declared inside the loop so they are not visible outside of it.

But you don't have to query the records if you already have ids.
for (mergeRecords record : recordIds) {
    Contact survivorCon = new Contact (Id = record.survivorId);
    Contact mergeCon = new Contact (Id = record.mergeId);
    merge survivorCon mergeCon;
}

Bear in mind that it can hit the 150 DMLs limit because it's inside a loop. Unfortunately the merge method doesn't seem to work with lists but only single ids/records.
 
This was selected as the best answer
Steve ConnellySteve Connelly
WOW! that did the trick. Another person pointed me in the direction of querying for the ID. I didn't think that I needed to as I already had it as you mentioned but didn't really know how to delare it properly. You helped with that.

Question though, is this better run outsid ethe loop? and how would that work?

Sc
Pawel BorysPawel Borys
Running a DML operation inside a loop is generally a red flag but in this case we cannot pass a list to the merge function as it only takes single records. However when an invocable method is called for multiple records, they are grouped together and passed as a list - that's why it requires a list as an input parameter. So if you don't do your merge action inside the loop then you're only merging one pair of the passed records ids.

There is no ideal way to do it at the moment. You have to either only process one record pair at a time (which could work if it's called e.g. from a button) or protect the code against DML limit exceptions (which cannot be caught in a try..catch block) for example by checking the limits (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm) with each iteration.

You can read more about invocable methods here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm