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
sfdcboysfdcboy 

Unable to understand when is user mode / system mode active on apex class ?

I know the difference between system and user mode . 
If my class has definition like 
public class A {
}
It will run in System mode that means whoever user has access to this apex will be able to crud on all record of any object ignoring his object and field level security.

When i tried same thing I created a vf page with custom controller which has functionality to search Account by its name and result is displayed in table.
I Logged in with user who doesnt have access to account and tried searching for account by entering name. 
Expectation were i will be abel to see records.
Result was nothing was shown in table .
Does that means apex class was fired in user mode rather than system mode.
Below is my code: 
Controller:
public class c1 {

List<account> acct = new List<Account>() ;
List<contact> con = new List<Contact>();
List<account> updtedAccount = new List<Account>();
Double sum =0;
  
public Date date1{get;set;}
public Date date2{get;set;}
public Boolean flag =false;
public boolean dis = true;
public string searchTerm{get;set;}

public c1(){

flag=false;
}

public List<account> getacct (){
return acct ;
}

public Double getSum(){
return sum ;
}

public PageReference search(){
setSearch();
return null ;
}

public boolean getflag(){
      return flag;
        }
    
    public boolean getdis(){
        return dis;
    }

public PageReference saving(){
update acct;
return null;
}

   public void setSearch(){
    
    if(!String.isNotBlank(searchTerm)){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Search Term cant be empty'));
        acct.clear();
        flag=false;
        dis =true;
    }
    else if (searchTerm.length()<2 || searchTerm.isNumeric())
    {
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Atleast give 2 characters to search and  you cant give alpha numeric or  numeric search term '));
        acct.clear();
        flag=false;
        dis =true;
    }
    else if(String.isNotBlank(searchTerm)){
        dis=false;
    List<List<SObject>> searchList = [FIND :searchTerm IN NAME FIELDS RETURNING Account (Id, Name,AnnualRevenue,Rating where annualrevenue !=null),Contact(id) ];
                acct = ((List<account>)searchList[0]);
                        con=((List<Contact>)searchList[1]);
    if(acct.size()<=0 && con.size() > 0){
    acct= [select id,name,AnnualRevenue,Rating from account where id in (select accountid from contact where id in: con)];
    }
    else if(acct.size()<0 && con.size()<0){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'No Result found'));
        acct.clear();
        flag=false;
        dis=true;
    }
/* acct= [select name from account where industry=:searchTerm ] ; 
    if(acct.size()>0)
    flag=true;
*/
       
    }
    
    else if(!String.isNotBlank(searchTerm) && date1!=null && date2!=null) {
        acct = [select id,name,AnnualRevenue,Rating from account where SLAExpirationDate__c >: date1 and SLAExpirationDate__c <: date2 ];
        dis=false;
    }
    sum=0;
    for(Account a : acct){
        sum=sum+a.AnnualRevenue ;
        }

    if(acct.size()>0)
        flag=true;     
}
}
---
V F: 
<apex:page controller="c1" docType="html-5.0" >
  <apex:outputPanel id="myPanel" title="My panel">
        <apex:pageMessages id="errorMessage" />
    </apex:outputPanel>
   
<apex:form >
<apex:pageblock title="Custom">
<!--<apex:pageBlockTable value="{!c1.getopt()}" var="a"> -->
<apex:pageblockSection >
Search Term: <apex:inputText value="{!searchTerm}"/>
Start Date <apex:input type="date" value="{!date1}"/>
End Date <apex:input type="date" value="{!date2}"/>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Search" action="{!search}" rerender="mytable,as,errorMessage,but"/>
<apex:commandButton id="but" value="Save" action="{!saving}" disabled="{!dis}" />
</apex:pageBlockButtons>
<br/>
<br/>
<apex:pageblocktable value="{!acct}" var="a" id="mytable"  >
<apex:column headerValue="Name" rendered="{!flag}">
<apex:inputField value="{!a.name}" />
</apex:column>
<apex:column headerValue="Rating" rendered="{!flag}">
<apex:inputField value="{!a.Rating}"/>
</apex:column>


</apex:pageblocktable>
<apex:pageblockSection id="as" >
<apex:outputPanel>
<apex:outputText value="{!sum}" rendered="{!flag}">
 </apex:outputText>
</apex:outputPanel>
</apex:pageblockSection>
</apex:pageblock>
</apex:form>
</apex:page>
VamsiVamsi
Hi,

Please go through the following link, you can get answer to the above question.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

Please mark as best answer, if the above helps ...!!!
sfdcboysfdcboy
Hi Vamsi , 

Please re-read the question .
In docs its written 
If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.

I guess this means public class class_name{} //runs in system mode.