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
Everton CP7Everton CP7 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hi there,

 

I create a VF page on case.

And the page don't read the assignment rules.

 

So, I tried to use trigger.

But giving me a huge error, I don't even know how to figure out.

 

trigger AtribuicaoDeCasoTr on Case (after insert) {
    
	Database.DMLOptions dmo = new Database.DMLOptions();
	dmo.assignmentRuleHeader.useDefaultRule= true;

Case newCase = new Case(Status ='new');
newCase.setOptions(dmo);
insert newCase;

}

SF accept the code.

But when I will create a case. This error appears to me:

 

"AtribuicaoDeCasoTr: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AtribuicaoDeCasoTr: maximum trigger depth exceeded Case trigger event AfterInsert for [500J0000001IUue] Case trigger event AfterInsert for [500J0000001IUuf] Case trigger event AfterInsert for [500J0000001IUug] Case trigger event AfterInsert for [500J0000001IUuh] Case trigger event AfterInsert for [500J0000001IUui] Case trigger event AfterInsert for [500J0000001IUuj] Case trigger event AfterInsert for [500J0000001IUuk] Case trigger event AfterInsert for [500J0000001IUul] Case trigger event AfterInsert for [500J0000001IUum] Case trigger event AfterInsert for [500J0000001IUun] Case trigger event AfterInsert for [500J0000001IUuo] Case trigger event AfterInsert for [500J0000001IUup] Case trigger event AfterInsert for [500J0000001IUuq] Case trigger event AfterInsert for [500J0000001IUur] Case trigger event AfterInsert for [500J0000001IUus] Case trigger event AfterInsert for [500J0000001IUut]: [] Trigger.AtribuicaoDeCasoTr: line 8, column 1"

 

Thanks,.

Everton.

JayDP123JayDP123
Im no vet but sort of look likes it is stuck in a loop. Maybe it is in an infinite loop of inserting newCase.
Everton CP7Everton CP7

So...How can I do the trigger that I want without get stuck in a loop?

You know how?

 

Thanks JayDP.

JayDP123JayDP123

Probably you should post the VisualForce Page code, and explain what it is you are trying to do. As far as I can tell you have a trigger that triggers each time a case is inserted, and it inserts a new case each time it is triggered. So the trigger is triggering itself! Hence the infinite loop. If you are trying to modify the case which is being inserted, try using a beforeInsert instead of afterInsert, and when you are using before insert you can remove the 'insert newCase' line as it will be done automatically.

 

 

Everton CP7Everton CP7

I nerver used a trigger, just create pages.

Thats why I'm facing some chalenges.

 

I made the changes and don't appear any errors to me, but still don't read the assignment rule.

 

The page renders based on the picklist "motivo". I have several options in this field, so this is just a piece of my page

 

<apex:page standardController="case">
<apex:sectionHeader title="Novo Chamado"/>
    <apex:form >
            <apex:pageBlock id="tipoderegistro" >
                <apex:pageMessages />                                      
                        <apex:pageBlockButtons location="top">
                             <apex:commandButton value="Criar Chamado" action="{!Save}"/>
                             <apex:commandButton value="Cancelar" action="{!Cancel}" immediate="true"/>                                
                        </apex:pageBlockButtons>
                            <apex:pageBlockButtons location="bottom">
                                <apex:commandButton value="Criar Chamado" action="{!Save}"/>
                                <apex:commandButton value="Cancelar" action="{!Cancel}" immediate="true"/>
                            </apex:pageBlockButtons>
            
<apex:pageBlockSection columns="2">
    <apex:inputfield value="{!case.Func_abertura_chamado__c}" required="true"/>
    <apex:inputfield value="{!case.func_resolve_chamado__c}" />
    
<apex:pageBlockSectionItem >
    <apex:outputLabel value="Tipo de Registro"/>
            <apex:actionRegion >
                <apex:outputfield value="{!case.RecordTypeId}">
                    <apex:actionSupport event="onchange" rerender="tipoderegistro" status="atualizando"/>
                </apex:outputfield>
            </apex:actionRegion>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
        <apex:outputLabel value="Motivo"/>    
            <apex:actionRegion >
                <apex:inputfield value="{!case.motivo__c}" required="true">
                    <apex:actionSupport event="onchange" reRender="campos" status="atualizando"/>
                </apex:inputfield>
            </apex:actionRegion>
    </apex:pageBlockSectionItem>
</apex:pageBlockSection>
                
    <apex:pageBlockSection title="Informações sobre o Chamado">
        <apex:inputfield value="{!case.Origin}" required="true"/>
        <apex:inputfield value="{!case.Prazo_para_resolucao__c}" required="true"/>
        <apex:inputfield value="{!case.Priority}" required="true"/>
    </apex:pageBlockSection>
    
<apex:outputPanel id="campos">
<apex:actionStatus startText="ATUALIZANDO..." id="atualizando"/>      
    <apex:pageBlockSection columns="1" title="Orçamento de Eventos" rendered="{!case.motivo__c == 'Orçamento de eventos'}">
        <apex:inputfield value="{!case.Subject}" required="true"/>
        <apex:inputfield value="{!case.description}" style="width: 280px !important; height: 95px !important;" required="true"/>
    </apex:pageBlockSection>

</apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
              

 

 Thanks for helping me JayDP.

Vinit_KumarVinit_Kumar

Try using Trigger helper class,which would prevent the Trigger to go into loop.

 

Create an Apex Class in your Org like below :-

 

public class TriggerHelperClass {   

    private static boolean flagvalue = false;


    public static boolean hasAlreadyfired() {
        return flagvalue;
    }
    
    public static void setAlreadyfired() {
        flagvalue = true;
    }

}

 

And,Use this Class in your Trigger as follows :-

 

trigger AtribuicaoDeCasoTr on Case (after insert) {

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;


if(!TriggerHelperClass.hasAlreadyfired()){
Case newCase = new Case(Status ='new');
newCase.setOptions(dmo);
insert newCase;
TriggerHelperClass.hasAlreadyfired();//This would prevent the invocation of Trigger in a loop
}

}

 

Everton CP7Everton CP7

Doesn't work :(..

 

For now I'm using workflow, but I don't want to waste my limit with this.

So, this is important that works and I know it is possible.

 

Practically same error appears.

 

"AtribuicaoDeCasoTr: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AtribuicaoDeCasoTr: maximum trigger depth exceeded Case trigger event AfterInsert for [500J0000001IVOz] Case trigger event AfterInsert for [500J0000001IVP0] Case trigger event AfterInsert for [500J0000001IVP1] Case trigger event AfterInsert for [500J0000001IVP2] Case trigger event AfterInsert for [500J0000001IVQu] Case trigger event AfterInsert for [500J0000001IVQv] Case trigger event AfterInsert for [500J0000001IVQw] Case trigger event AfterInsert for [500J0000001IVQx] Case trigger event AfterInsert for [500J0000001IVQy] Case trigger event AfterInsert for [500J0000001IVQz] Case trigger event AfterInsert for [500J0000001IVR0] Case trigger event AfterInsert for [500J0000001IVR1] Case trigger event AfterInsert for [500J0000001IVR2] Case trigger event AfterInsert for [500J0000001IVR3] Case trigger event AfterInsert for [500J0000001IVR4] Case trigger event AfterInsert for [500J0000001IVR5]: [] Trigger.AtribuicaoDeCasoTr: line 9, column 1"

 

Thanks Vinit.

Vinit_KumarVinit_Kumar

Not sure why it is not working,it should work as per me!!

Everton CP7Everton CP7

Do you know if "version setings" in some way affects the funcionality ?

JayDP123JayDP123

I don't know much about setting dmloptions.etc.. but assuming that you are just trying to modify the  case which is being inserted, couldn't you just try something like this: 

 

trigger AtribuicaoDeCasoTr on Case (before insert) {
    
	Database.DMLOptions dmo = new Database.DMLOptions();
	dmo.assignmentRuleHeader.useDefaultRule= true;

for (case c : trigger.new){
     c.status = new;
     c.setOptions(dmo);
}
}

 

Vinit_KumarVinit_Kumar

Everton,

 

Try below code in your Trigger :-

 

trigger AtribuicaoDeCasoTr on Case (after insert) {

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;


if(!TriggerHelperClass.hasAlreadyfired()){
Case newCase = new Case(Status ='new');
newCase.setOptions(dmo);
insert newCase;

}

TriggerHelperClass.hasAlreadyfired();//This would prevent the invocation of Trigger in a loop

}

 

Hope this works :)

Everton CP7Everton CP7

I tried both codes and the same happens to me.

 

I was thinking...

What I need is the case read my assignment rules that means go to the right user.

 

If I use a code to update the field owner case, this will do what I need.

I'm right?

 

Can I do this code in VF or I have to do a trigger?

Vinit_KumarVinit_Kumar

That depends upon your requirement ,

 

1.) if you want that to happen on a button click,then you need VF page and controller

2.) If you want it automatically,like(insert/update) event you need a Trigger for the same.