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
svdsvd 

System.Exception: DML currently not allowed..help needed.

Hi,

I'm trying to call a action method in my controller through my java script code in the visualforce page. but my action method consists of 'Insert' DML statement and so I'm getting the following error.

System.Exception: DML currently not allowed. External entry point

here is a piece from my code.

<apex:page>

 <script type="text/javascript">

          function register(regId)
          {
          // I have some code to add regId to my query String. Then I call my action method in controller.  
             {!register};                          
          }
    </script>

< // some visual force component that calls Java script 'register' function />

</apex:page>

my action method code is as follows:

 public Pagereference getRegister()
     {
   
        scheduleId = System.currentPageReference().getParameters().get('schID'); // getting the query string added by my javascript method
        userId = UserInfo.getUserId();
        Registration__c reg = new
Registration__c(Activity_Schedule_ID__c = scheduleId, User__c = userId );
       
        insert reg;
       
        return null;
    }


Is there any other way that I can get this done.
Any help is appreciated. Thanks in advance.

Thanks,
DSV
jwetzlerjwetzler
You can't call DML in a getter.  Getters get called a lot during the lifecycle, so it doesn't make sense that you'd want to do an insert within a getter.  You need to do your DML inside of an action method, and for that you'll need to use an action component. actionFunction will probably do what you need although I question whether you really need to do this through javascript.  What triggers your register javascript function?  Why do you have to do it through javascript rather than calling the action directly using either commandButton, commandLink or actionSupport?
svdsvd
Hi Jill,

Thanks for your reply.

 My requirement :
 when I press a button in Visual Force page, I need to get a confirmation window (yes/no type) and when user hits Yes, then I need to get my register function called.So for this confirmation window, I'm using javascript. My problem is I'm unable to call action methods/ getter methods that consist of DML operations from java script code. Is there a way a achieve this. If so, please give me some details on how to achieve it.

Thanks again,
DSV
jwetzlerjwetzler
You are able to call action methods with DML in them -- that's the only place that DML is allowed.

It sounds like you're making this overly complicated.  Somethings like this should be fine:

Code:
<apex:commandButton value="Register" action="{!register}" onclick="if (!confirm('Are you sure?')) return false;"/>

 
Also notice that you should now have a register() method instead of getRegister().



Message Edited by jwetzler on 11-24-2008 11:00 AM
svdsvd
Hi Jill,

thanks for your time and help. Yes, this info helps. But here is the problem I face with this logic.

My VF page has some n courses and each course has a Register button. When I click register button and after user confirmation I need that particular subject to be registered in the system. (Add a record with UserID and SubjectID to a custom object).To do this I need to send the Subject ID to my controller. But VF can not pass arguments to APEX functions. So I was doing it through dummy query strings.

So is there a way that I can set query string (add an ID parameter) when user clicks Register button and then ask for confirmation.

Thank you,
SVD
jwetzlerjwetzler
Please look into the apex: param component, particularly the assignTo attribute.
wesnoltewesnolte
It does makes sense to do it in a getter if I'm writing a logger:P