1

I am attempting to update information in a visualforce component, and am getting the error

System.LimitException: DML currently not allowed

Everything I've searched says that that happens when you try to put an update in the constructor, but I don't think I'm doing that. Can someone help me figure out what's going on?

Code is as follows:

VisualForce Page:

<apex:component>
   <apex:attribute name="MVRController" type="BC2MVR" description="The page's controller" />

          <!-- APPLY INFORMATION -->
         <apex:actionFunction reRender="serializedCapturedByHTML" name='callApplyMVR' action='{!MVRController.applyInformation}'>
              <apex:param name='violationsJSON' value='' assignTo="{!MVRController.violationsJSON}"></apex:param>
              <apex:param name='medicalInfoJSON' value='' assignTo="{!MVRController.medicalInfoJSON}"></apex:param>
         </apex:actionFunction>
         <script>
                     function MVRApply(){
                         let violationArr = [];
                         let medicalArr = [];
                         $('.violation-parent-div').each(function(){
                             let violation = {
                                 Id: $(this).attr('id'),
                                 Fine_Amount__c: $(this).find('.fine-amount').val(),
                                 Fine_State__c: $(this).find('.fine-state').val(),
                                 State_Points__c: $(this).find('.state-points').val()
                             }
                             violationArr.push(violation);
                         });
                         $('.medical-parent-div').each(function(){
                             let medInfo = {
                                 Id: $(this).attr('id'),
                                 Examiner_Name__c: $(this).find('.examiner-name').val(),
                                 Examiner_Phone__c: $(this).find('.examiner-phone').val(),
                                 Examiner_Registration_Number__c: $(this).find('.examiner-registration-num').val(),
                                 Examiner_Specialty_Code__c: $(this).find('.examiner-specialty-code').val()
                             }
                             medicalArr.push(medInfo);
                         });
                         let JSONViolations = JSON.stringify(violationArr);
                         let JSONMedInfo = JSON.stringify(medicalArr);
                         callApplyMVR(JSONViolations, JSONMedInfo);
                     }
           </script>
 </apex:component>

Apex Controller

public void applyInformation(){
    Datetime now = Datetime.now();
    Integer offset = UserInfo.getTimezone().getOffset(now);
    Datetime local = now.addSeconds(offset/1000);

    tempNewViolation = (List<Violation__c>) JSON.deserialize(violationsJSON, List<Violation__c>.class);
    tempMedicalInfo = (List<Medical_Info__c>) JSON.deserialize(medicalInfoJSON, List<Medical_Info__c>.class);


    for(Violation__c newViolation : tempNewViolation){
        for(Violation__c oldViolation : violations){
            if(newViolation.Id == oldViolation.Id){
                System.debug('Found a match!');
                Map<String, Schema.SObjectField> M = Schema.SObjectType.Violation__c.fields.getMap();
                for(String field : M.keySet()){
                    if((newViolation.get(field) != oldViolation.get(field)) && newViolation.get(field) != null){
                        if(oldViolation.Original_Object__c != null){
                            oldViolation.Original_Object__c = JSON.serialize(oldViolation);
                        }
                        oldViolation.put(field, newViolation.get(field));
                    }
                }
            }
        }
    }
    for(Medical_Info__c newMedInfo : tempMedicalInfo){
        for(Medical_Info__c oldMedInfo : medicalInfo){
            if(newMedInfo.Id == oldMedInfo.Id){
                Map<String, Schema.SObjectField> M = Schema.SObjectType.Medical_Info__c.fields.getMap();
                for(String field : M.keySet()){
                    if((newMedInfo.get(field) != oldMedInfo.get(field)) && newMedInfo.get(field) != null){
                        if(oldMedInfo.Original_Object__c != null){
                            oldMedInfo.Original_Object__c = JSON.serialize(oldMedInfo);
                        }
                        oldMedInfo.put(field, newMedInfo.get(field));
                    }
                }
            }
        }
    }
    update medicalInfo;
    update violations;
}

I initially thought it might be action='{!MVRController.applyInformation}', so I tried removing the MVRController and no longer got the error, but the code was not called on, either.

Anyone have any guesses as to why this isn't working? Thanks!

1
  • PLs format the code and then copy paste here. Also use {} to format code correctly Commented Aug 28, 2019 at 17:01

1 Answer 1

2

You should be setting allowDML to true according to documentation

If this attribute is set to "true", you can include DML within the component. The default is "false". Allowing DML can cause side-effects that could be problematic for consumers using the component with partial page updates. When allowing DML within a component, you should include rerender attributes so the consumer can appropriately refresh their page. In addition, you should detail, in the description of the component, what data is manipulated by the DML so that consumers of the component are aware of potential side-effects.

1
  • That was it. Thank you for the help!
    – ccarlson
    Commented Aug 28, 2019 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .