I am using a flow to generate new records that have a lookup to an object. The user does not and cannot have access to the record in the lookup. Since it looks like the flow runs as the user and not in say system mode like a process builder would I want to use a public without sharing class to handle the record by having the flow populate a text field and then update the lookup with the id stored in the text.
The problem is I cannot figure out how to insert the record without errors.
Handler Class:
public override void beforeInsert(List<SObject> newListGeneric){
List<Competitive_Intel__c> newList = (List<Competitive_Intel__c>) newListGeneric;
List<Competitive_Intel__c> filterlookup = populateLookupFilter(newList);
safePopulateLookup(populateLookup(filterlookup));
}
public static List<Competitive_Intel__c> populateLookupFilter (List<Competitive_Intel__c> newList)
{
List<Competitive_Intel__c> cisToInsert = new List<Competitive_Intel__c>();
Map<ID,Schema.RecordTypeInfo> rt_Map = Competitive_Intel__c.sObjectType.getDescribe().getRecordTypeInfosById();
for (Competitive_Intel__c ci : newList)
{
if(
rt_map.get(ci.recordTypeID).getName().containsIgnoreCase('Update') &&
ci.Competitive_Intelligence_Parent_Id__c != null
)
{
cisToInsert.add(ci);
}
}
return cisToInsert;
}
public static List<Competitive_Intel__c> populateLookup (List<Competitive_Intel__c> cisToInsert)
{
for(Competitive_Intel__c cisLoop : cisToInsert)
{
cisLoop.Competitive_Intelligence__c = cisLoop.Competitive_Intelligence_Parent_Id__c;
}
return cisToInsert;
}
public static void safepopulateLookup (List<Competitive_Intel__c> cisToInsert)
{
NPD_CompetitiveIntelligenceActions.createIntel(cisToInsert);
}
Action Handler takes it from here to do the insert:
public static void createIntel (List<Competitive_Intel__c> cisToInsert)
{
try{
insert cisToInsert;
} catch (DmlException ex) {
System.debug('The following Exception has occured: ' + ex.getMessage() + ' at line ' + ex.getLineNumber());
}
}
When I have the setup like this I get the error:
DML statement cannot operate on trigger.new or trigger.old
Can you suggest what I am doing wrong?