+ Start a Discussion
Manolito Mendoza 27Manolito Mendoza 27 

How can I prevent governor limits using Visual Flow?

Hi,

I've created this Visual Flow to delete records one at a time to prevent it from reaching the governor limits.

Visual Flow to delete records one at a time.

This Flow works fine with less then 50000 records. When I tried to use it with an object that has 700K+ records, the first element (Fast Lookup) reaches the governor limits right away.

How do I fix this?

Thanks in Advance!
Best Answer chosen by Manolito Mendoza 27
Gururaj BGururaj B
The Apex class should some what look like below:

public class getLimitedRecord {
    @InvocableMethod(label='getLimitedRecords')
    public static list<AccountContactRole> getLimitedRecords() {
    list<AccountContactRole > lmtAccRole =[select name,Role from AccountContactRole  where Role <>'Key Contact Role' limit 50000];
    return lmtAccRole;
}
}


Please mark as best answer if this solution works for you.

All Answers

Gururaj BGururaj B
Create a apex class wtih a method having annotation @InvocableMethod and reutrn type as list of the object that you are doing lookup in your first step. In the method do a query using SOQL on the object. In this Query use the limit of 50000(as it allows till this no.). Return the result list object.

Use this Apex in your flow as first step replacing your Fast Lookup. here assign the output of the apex to object variable and loop through and delete based on condition by keeping all your other step as it is. Create one more outter loop that check the apex class output and loops through if the apex output returns any values. Apex class will do query every time excluding the deleted records and hence you can achive any no. of record in your flow. I hope this helps, if so please mark as best answer.
Manolito Mendoza 27Manolito Mendoza 27
Thanks, Gururaj B!

However, I am not an expert in Apex coding. Do you happen to have a sample code? The object is AccountContactRole and records should be deleted except for the Role assigned as Key Contact Role.

TIA
Gururaj BGururaj B
Adding to my previous comment its better you include the condition logic, that you are doing in the Decision step, in the apex class iteslf, this way you can exclude the records that doesnt qualify for deletion.
Manolito Mendoza 27Manolito Mendoza 27
Would you be kind enough to send a sample code?

TIA
Gururaj BGururaj B
The Apex class should some what look like below:

public class getLimitedRecord {
    @InvocableMethod(label='getLimitedRecords')
    public static list<AccountContactRole> getLimitedRecords() {
    list<AccountContactRole > lmtAccRole =[select name,Role from AccountContactRole  where Role <>'Key Contact Role' limit 50000];
    return lmtAccRole;
}
}


Please mark as best answer if this solution works for you.
This was selected as the best answer
Manolito Mendoza 27Manolito Mendoza 27
Thank you so much Gururaj B!

I'll have this looked at by tomorrow and get back to you.

Cheers!