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
Rajaa ABOUMAADRajaa ABOUMAAD 

DML currently not allowed when trying to update a record

hello, I'm trying to affect a value to a field depending on the value of two other, for example if the quantity of a product requested by a seller is greater than the quantity in the store, the value of our field must be the value of the quantity in the store, this is my code and thank you in advance

VF page:
 
<apex:page controller="ProduitDemandeClass"  >
    
    <apex:form >
        
        <apex:pageBlock >
            <apex:commandButton value="Enregistrer" />
            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!produitsDemande}" var="pr">
                    <apex:column value="{!pr.Produit__c}"/>
                    <apex:column value="{!pr.Quantit_demande__c}" />
                    <apex:column value="{!pr.Quantit_Accorde__c}"/>
                    <apex:column headerValue="Action">
                        <apex:commandButton value="Modifier"  onclick="window.location='/apex/ValeurAccorde?id={!pr.Produit__c}'; return false;" />
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

apex class:
 
public with sharing class ProduitDemandeClass {
    public String  idcharg {get;set;}
    LIST<les_Produit_demander__c> produitsDemande  {get;set;} 
    
    public List<les_Produit_demander__c> getproduitsDemande() {
        
        if(produitsDemande == null){
            
            produitsDemande = [select Id,Produit__c,Quantit_demande__c,Quantit_Accorde__c from  les_Produit_demander__c where Demande_de_Chargement__c= :idcharg];
            Integer i=0;
            for(les_Produit_demander__c pr :produitsDemande){
                list<Stock_par_agence__c> p =[select QuantiteTotaleRestante__c from 	Stock_par_agence__c where 	Produit__c= :pr.Produit__c];
                if(p != null && p.size() > 0){
                    if(p.get(0).QuantiteTotaleRestante__c > pr.Quantit_demande__c){
                        pr.Quantit_Accorde__c=pr.Quantit_demande__c;
                   
                        update pr;
                    }
                    else {
                        pr.Quantit_Accorde__c=p.get(0).QuantiteTotaleRestante__c;
                        
                       update pr;
                       
                    }
                } 
            }
             
        }
        return produitsDemande;
    }
    
    public ProduitDemandeClass(){
        idcharg=ApexPages.currentPage().getParameters().get('id');
    }
}



 
Best Answer chosen by Rajaa ABOUMAAD
shashi lad 4shashi lad 4
This error occurs typically when we use DML in constructor or Get methods. This is a limitation in salesforce that we can not use DML in get methods. You can refactor the code accordingly and it should work fine. This is a good article that can help you

http://th3silverlining.com/2009/11/23/dml-currently-not-allowed/

Hope this helps. 

Thanks
shashi

All Answers

shashi lad 4shashi lad 4
This error occurs typically when we use DML in constructor or Get methods. This is a limitation in salesforce that we can not use DML in get methods. You can refactor the code accordingly and it should work fine. This is a good article that can help you

http://th3silverlining.com/2009/11/23/dml-currently-not-allowed/

Hope this helps. 

Thanks
shashi
This was selected as the best answer
Rajaa ABOUMAADRajaa ABOUMAAD
thank you for your reply, it was helpful :)