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
como_estascomo_estas 

trigger DML exception

Hey folks.  I can't for the life of me figure out why I'm getting a DML exception in my trigger test suite.  Can somebody help?

 

Here's the trigger code:

 

trigger updateStatusPicklist on Application__c (after insert, after update) {
    
    updateStatusPicklist up = new updateStatusPicklist();
    Application__c[] apps = new list<Application__c>();
    
    if(trigger.isInsert) {
        apps = trigger.new;
    } else {
        for(Application__c app : trigger.new)
        {
            /* Only deal with apps whose status changed */
            if(trigger.oldMap.get(app.id).Status__c != trigger.newMap.get(app.id).Status__c)
            {
                apps.add(app);
            }
        }
    }
    
    up.handleTrigger(apps);
    
}

 

I won't paste in the handleTrigger method, but what the handleTrigger method does is loop through the apps and based on the value in the Status__c field, it sets a different field to a particular value.... I think this is where my problem lies but continuing on....

 

Here's the test method code:

 

    static testMethod void testPicklistStatus() {

        Contact con = new Contact(LastName = 'Collins');
        insert con;

            Application__c app = new Application__c(Contact__c = con.id);
            insert app;    
            system.assertEquals(app.isStatus__c,null);
            
            app.TargetX_SRMb__Status__c = 'Potential Applicant';
            update app;
            system.assertEquals(app.isStatus__c,'Inquiry','Status = ' + app.isStatus__c);
           

The DML exception is happening when I try to "update app".  I've tried putting it into a System.runAs() block but that doesn't help.  I wonder if the problem is this:

 

In the test method, I create the app record.  that fires the trigger.  then, the trigger also updates the app record, which in turn, fires the trigger again.  However, this time, it shouldn't get updated again, because trigger.old and trigger.new should have the same status..  but maybe i'm wrong.  Then, the test method tries to update the record but it fails because of some state that the record is in.  However I just don't know the root of the problem so I don't know how to fix it.  Can somebody help me out or point me in the right direction to get info about this kind of stuff?

Best Answer chosen by Admin (Salesforce Developers) 
James LoghryJames Loghry

como_estas,

 

Try one of two things:

If you're only performing the DML on the Application__c record, I might suggest using a before insert / before update instead of after insert or after update.

 

If that won't work, then in your updatestatuspickup class, add a static boolean variable.  When the insert happens, set it to true, and then check to see if it executes when records are updated.  This static boolean variable will prevent the dml logic from occurring twice in a single transaction.

 

Hope that helps.

 

 

Edit://

For more info, read the salesforce docs on recursive triggers:

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm