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?

2

Thanks to @PranayJaiswal, got the documentation

You can't use data manipulation language (DML) operations in a “getxxx” method in a controller. For example, if your controller had a getName method, you could not use insert or update in the method to create an object.

Referred from Is DML not allowed in a Visualforce getter?

Note: Still not clear why its not throwing error and other missing-clarity in above post


Previous

You cannot insert/create objects in page constructor. But you are trying to create an object in a property getter - its a big NO - not sure why its not throwing error (might be bug). You are right that although it shows success, we are unable to actually find the record - not even in deleted records. Also ONLY insert context is being invoked and no other trigger context.

However, here is the solution for your issue. You can create object in page action.

Apex:

public static Event__c createdEvent{get;set;}

public static PageReference InsertEvent() {
    String name = ApexPages.currentPage().getParameters().get('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);
    createdEvent = res;
    return null;
}

Page:

<apex:page controller="EventInsert" action="{!InsertEvent}" >
    <apex:form>
    <p>Event insertion test</p>
    <p>{!createdEvent.Id} - {!createdEvent.Name}</p>
    <apex:pageMessages />
    </apex:form>
</apex:page>
4
  • 1
    It cannot use the InsertEvent directly as is as it does not pass the parameter, but altering the original processParams made it work. - Thanks!
    – Lukas
    Aug 28 '19 at 13:16
  • i forgot to remove param and add name getter - i guess you figured it. Aug 28 '19 at 13:24
  • 1
    @salesforce-sas DML are not allowed in getter :) salesforce.stackexchange.com/questions/158938/… Aug 28 '19 at 13:28
  • thanks! @PranayJaiswal, added in answer Aug 28 '19 at 13:40

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.