1

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?

2

You cannot insert record on trigger context. So change your method as follows:

Take the List from Trigger.New and create a new List and finally insert new List.

public static void createIntel (List<Competitive_Intel__c> cisToInsert)
{
    try{
        List<Competitive_Intel__c> newList = new List<Competitive_Intel__c>();
        for (Competitive_Intel__c compObj : cisToInsert)
        {
            newList.add(new Competitive_Intel__c(Id = compObj.Id));
        }
        insert newList;              
    } catch (DmlException ex) {
        System.debug('The following Exception has occured: ' + ex.getMessage() + ' at line ' + ex.getLineNumber());
    }
}
0

Yes you are getting correct error. remove insert statement from you code. I hope it will work. when you are using before insert event, you can't manually insert record on that object.

  • 1
    That didn't work, Santanu's solutions works. – Dan Wooding Apr 20 '17 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.