0

List update return Update access denied for object, controller action methods cannot be executed I would like a help with update for a list... For some reason when I click to update it returns the following error: "core.apexpages.exceptions.ApexPagesGenericException: system.security.NoAccessException: Update access denied for Business_Opportunity__c, controller action methods cannot be executed" so I searched the ''without sharing" should solve, but even putting it I keep getting an error..

Visualforce page

<apex:page language="{!IF(ISNULL(language),'pt_BR',language)}" id="OppNegocios" showHeader="false" docType="html-5.0" standardStylesheets="false" cache="false"  controller="aSiteOppNegociosController" action="{!validate}">
        <apex:define name="body">
            <body class="nav-md footer_fixed" >
                           <center>
                    <div style="background-color:white; margin-top:100px; padding:20px; width:80%">
                        <div class="page-title">
                            <div class="title_left">
                                <h3 style="color:black">Oportunidades de Negócios</h3><!--Título: Anexo I-->
                            </div>
                        </div>
                        <hr width="90%" style="color:black;" />
                        <apex:form >       
                            <apex:pageMessages />
                            <table>
                                <tr>
                                    <th style="color:black;">Variedades:</th>
                                    <th style="color:black;">Demanda:</th>
                                    <th style="color:black;">Oferta:</th>
                                </tr>
                                <tr>
                                    <td style="padding:5px;">
                                        <apex:selectList id="selectedVariedade" value="{!selectedVariedade}" size="1" >
                                            <apex:selectOptions value="{!VariedadeList}"></apex:selectOptions> 
                                        </apex:selectList>
                                    </td>
                                    <td style="padding:5px;"><apex:inputText value="{!demanda}"/></td>
                                    <td style="padding:5px;"><apex:inputText value="{!oferta}"/></td>
                                    <!--variedade-->
                                </tr>
                                <tr> 
                                    <td><apex:commandButton value="Inserir" action="{!save}"/></td>
                                </tr>
                                
                            </table>
                        </apex:form>
                        <br/>
                        <hr width="90%" style="color:black; margin-bottom:0px;" />
                        <div >
                         <apex:pageMessages />
                           <br/><br/>
                            <apex:form id="changeStatusForm">
                                <apex:pageBlock>
                                    
                                    <apex:pageBlockTable value="{!OppNegocios}" var="c">
                                        
                                        <apex:column headerValue="Variedades">
                                            <apex:outputText value="{!c.Variedade__r.Name}"/>
                                        </apex:column>
                                        
                                        <apex:column headerValue="Demanda">
                                            <apex:inputText value="{!c.Demanda_Compra__c}"/>
                                        </apex:column>
                                        
                                        <apex:column headerValue="Oferta">
                                            <apex:inputText value="{!c.Oferta_Venda__c}"/>
                                        </apex:column>
                                        
                                    </apex:pageBlockTable>
                                    
                                    <div style="margin:10px"></div>
                                    <apex:pageBlockButtons>
                                        <apex:commandButton value="Atualizar" action="{!updateoppNegocios}"/>
                                    </apex:pageBlockButtons>
                                    <div style="margin:5px"></div>
                                    
                                </apex:pageBlock>
                            </apex:form><br/><br/>
                            
                        </div>
                    </div>
                </center>
            </body>
        </apex:define>
</apex:page>
Controller
global without sharing class aSiteOppNegociosController {
    private String token;
    private String idlicenciado {get; set;}
    private SiteUser__c logUser;
    private Account activeAccount;
    private SiteUserSession__c activeSession;
    public String safraActual {get;set;}
    public String language {get;set;}
    public Oportunidade_de_Negocios__c a {get;set;}
    public List<Oportunidade_de_Negocios__c> oppNegocios;
    public Double demanda{get;set;}
    public Double oferta{get;set;}
    public String selectedVariedade {get; set;}
    
    //public List<Oportunidade_de_Negocios__c> oppNegocios = new List<Oportunidade_de_Negocios__c>();
    public List<Variedade__c> Variedade = new List<Variedade__c>();
    //public List<Safra__c> Safra=new List<Safra__c>();
    
    global aSiteOppNegociosController () {
        try{
            language = [select id, LanguageLocaleKey from User where id =: UserInfo.getUserId()].LanguageLocaleKey;
        }
        catch(Exception e){
        }
       
        init();//Call the init method for the update
    }
    
    public PageReference validate() {
        token = System.currentPageReference().getParameters().get('token');
        if(token!=null){
            logUser = aSiteAuthManager.VerifyUserSession(token);
            System.debug('logUser: ' + logUser);
            if(logUser!=null){
                activeSession = aSiteAuthManager.getUserSession(token);
                activeAccount = aSiteAuthManager.getActiveAccount_byUser(logUser);
                safraActual = aSiteUtils.buildSafraByYear();
                idlicenciado =  logUser.Id;
                return null;  
            }          
        }
         
        PageReference p = new PageReference('/apex/aSiteLogin');
        String marca = aSiteAuthManager.getMarcaBySession(token);
        if (marca != null)
            p.getParameters().put('comp',marca);
        p.setRedirect(true);
        return p;
        
    }    
    
    //Insert the business opportunity
    public  PageReference save(){
        try {
            a=new Oportunidade_de_Negocios__c();
            
            a.Demanda_Compra__c = demanda;
            a.Oferta_Venda__c = oferta;
            a.Licenciado__c=logUser.Id;
            a.Conta__c  =activeAccount.Id;
            a.Variedade__c=selectedVariedade ;
            a.Safra__c=safraActual;
            a.OwnerId='005f4000000RIxAAAW';//to do
            insert a;
            //refresh the pag
            PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
            pageRef.setRedirect(true);
            return pageRef;  
            
        } catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
    }
        
 
    public void init(){
        oppNegocios=[Select Id, Variedade__c,Variedade__r.Name, Oferta_Venda__c, LastModifiedDate, Licenciado__c, Safra__c,Demanda_Compra__c  
                     FROM Oportunidade_de_Negocios__c WHERE Licenciado__c =: idlicenciado AND Safra__c =: safraActual ORDER BY LastModifiedDate DESC ];
        System.debug('oppNegocios no init: '+oppNegocios);
    }
    
    public PageReference updateoppNegocios(){
        System.debug('oppNegocios logo após o clique: '+oppNegocios);
        try{
            update oppNegocios; 
            
        }catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        
        init();
        return null;
    }

    //Assemble the product variety picklist options
    public List <SelectOption>getVariedadeList(){
         List<SelectOption> var=new List<SelectOption>();
         List<Variedade__c> allvar=[Select Id, Name FROM Variedade__c WHERE Obtentor__c = 'HO' and Ativa__c = TRUE AND Disponivel_para_Negociacao__c = true];   
        for(Variedade__c i : allvar){
            var.add(new SelectOption(i.Id, i.Name));  
        }
        return var;
    }    
    //Builds the list that displays all the licensee's business opportunities
    public List<Oportunidade_de_Negocios__c>getOppNegocios(){
          if(oppNegocios==null){
           oppNegocios=[Select Id, Variedade__c,Variedade__r.Name, Oferta_Venda__c, LastModifiedDate, Licenciado__c, Safra__c,Demanda_Compra__c  
                       FROM Oportunidade_de_Negocios__c WHERE Licenciado__c =: idlicenciado AND Safra__c =: safraActual ORDER BY LastModifiedDate DESC ];
          }
         // System.debug('oppNegocios: '+oppNegocios);

        return oppNegocios;
      }
   

    
    
}

      

enter image description here

1
  • Does the running user have EDIT (CRUD) access to the Business_Opportunity__c object? (which I'm guessing is indirectly updated by updating Oportunidade_de_Negocios__c)
    – cropredy
    Commented May 5, 2022 at 16:38

1 Answer 1

0

I found this link in my searches, hope it helps anyone who has the same problem in the future

VFP

<apex:page controller="customController" showHeader="false" lightningStyleSheets="true">
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection columns="2" title="SObject Record" id="myPanel"> 
        <apex:inputtext value="{!sobjectRecord.Some_Field__c} /> <apex:commandButton action="{!doUpdate}" value="Save" reRender="myPanel />
    </apex:pageBlockSection>
    </apex:pageBlock> 
    </apex:form> 
</apex:page>


Controller

//customController.cls public with Sharing class 
customController { 
    public Some_Object__c sobjectRecord {get;set;}
    //1. Store the recordId in a private variable. 
    private sobjectRecordId {get; set;} 
    
    public customController(){
        sobjectRecord =[select Id,Some_Field__c FROM Some_Object__c WHERE id = 'someIdvaluehere'];
        
        //1. Store the recordId in a private variable. 
        sobjectRecordId = sobjectRecord.Id; 
        
        //2. Nullify the Id field of the record variable before it’s passed to the visualforce page. 
        sobjectRecord.Id = null; 
    }
    public PageReference doUpdate() { 
        //3. Set the Id field on the record, from the private variable, right before the update dml.
        sobjectRecord.Id = sobjectRecordId; 
        sobjectRecord = customControllerWS.doUpdate(sobjectRecord); 
        return null; 
    }
} 
customControllerWS.cls
    public without Sharing class customControllerWS { 
    public static Some_Object__c doUpdate(Some_Object__c sobjectRecord) {
        update sobjectRecord; 
        //4. Re-nullify the Id field of the record variable after the dml. 
        sobjectRecord.Id = null; 
        return sobjectRecord; } 
    }



You must log in to answer this question.

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