• sfdcboy
  • NEWBIE
  • 15 Points
  • Member since 2017
  • Consultant

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
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>
  • September 06, 2017
  • Like
  • 0
Everyone writes below code to stop recursion.
public Class checkRecursive{
    private static boolean run = true;   
  public static boolean runOnce()
{   
  if(run)
{
     run=false;   
  return true;  
   }
else{
        return run;    
}
    }
}
But how does it work considering if there are 100 users simutaneouly using the appilication and they updated something to fire the trigger.
if(Trigger.isBefore)
{
if(Trigger.isInsert && checkRecursive.runOnce()){
}
}

Now for the first user it will run set run to false , for rest 99 users it wont even execute as run= false as static variable are shared across sessions ie value wont reset .


I'll be really grateful if somebody could shed some light on it.
  • September 06, 2017
  • Like
  • 0
I know the relationship between account - contact and account - opportunity is lookup on UI with cascade delete set to True in backend.
Considering how you can update a parent from child if relationship is master detail.
I was trying to write a workflow rule to update Account from Contact but account fields werent there to be selected , But when i tried doing same with opportunity Account fields were there in drop down .
I dont get it considering account has same relationship with both objects ,Why is there such difference.
Thanks 
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>
  • September 06, 2017
  • Like
  • 0
Everyone writes below code to stop recursion.
public Class checkRecursive{
    private static boolean run = true;   
  public static boolean runOnce()
{   
  if(run)
{
     run=false;   
  return true;  
   }
else{
        return run;    
}
    }
}
But how does it work considering if there are 100 users simutaneouly using the appilication and they updated something to fire the trigger.
if(Trigger.isBefore)
{
if(Trigger.isInsert && checkRecursive.runOnce()){
}
}

Now for the first user it will run set run to false , for rest 99 users it wont even execute as run= false as static variable are shared across sessions ie value wont reset .


I'll be really grateful if somebody could shed some light on it.
  • September 06, 2017
  • Like
  • 0
Hi,
How to combine 3 map type field in apex.. then with the unique combination. i have to send mail also. Please help

I am trying to create a rule.This is my criteria:

 

I have a field on Opportunity which will change with workflow or either admin CAN only change.I need a rule so that other users can't update that field.

 

How can I say that admin and workflow can change the field but not other users..Please help!!