Quantcast
Channel: Apex page failing DML transaction - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 2

Apex page failing DML transaction

$
0
0

Let's have a very simple apex page:

<apex:page controller="EventInsert" >
    <apex:form>
    <p>Event insertion test</p>
    <p>{!processParams}</p>
    <apex:pageMessages />
    </apex:form>
</apex:page>

And the corresponding controller class:

public class EventInsert {
    public static Database.SaveResult InsertEvent(String name) {
        Event__c toInsert= new Event__c(name = name);
        Database.SaveResult s;
        // insert record
        try {
            s = Database.insert(toInsert);
            System.debug('insert succeded');
            System.debug(s);
        } 
        catch (DmlException e) {
            System.debug('insert failed');
            System.debug(e);
        }

        //check if it is really there 
        Event__c res = [SELECT Id, Name, Event_ID__c FROM Event__c WHERE Name  = :name LIMIT 1];
        System.debug(res);

        return s;
    }

    public String processParams {
        get {
            String name = ApexPages.currentPage().getParameters().get('name');
            System.debug(name);
            Database.SaveResult s = EventInsert.InsertEvent(name);
            String res = String.valueOf(s);
            return res;
        }
    }
}

The idea is to insert the record on page load. When called independently from anonymous window the record is inserted. When run by the page the code runs (writes debug messages), but the record is never actually inserted (commited). Result page is as expected:

Event insertion test
Database.SaveResult[getErrors=();getId=a002o00000sbOl3AAE;isSuccess=true;]

The problem is likely in the apex page, not in the controller. Where is the error and why does it not show up anywhere?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images