Friday, July 7, 2017

Inner Popup using Salesforce

Visualforce page:

<apex:page controller="showAll" >
<apex:form >
<!--    CSS Style    -->
<style type = "text/css">

.headr
{
font-size:17px;
font-weight:bold;
color:maroon;
}

.popup
{
background-color: white;
border-width: 2px;
border-style: solid;
z-index: 9999;
left: 50%;
padding:10px;
position: absolute;
width: 500px;
height:60%;
margin-left: -250px;
top: 25px;
overflow:scroll;
}
     
.popupBg
{
background-color:black;
opacity: 0.20;
filter: alpha(opacity = 70);
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 9998;
}

</style>
<!--    End of CSS Style    -->

<apex:pageblock >
<table cellspacing = "7" cellpadding = "7">
<tr>
<td><apex:commandLink styleClass="headr" action="{!members}" reRender="members">Members</apex:commandLink></td>
</tr>
<tr>
<td><apex:commandLink styleClass="headr" action="{!blogs}" reRender="blogs">Blogs</apex:commandLink></td>
</tr>
<tr>
<td><apex:commandLink styleClass="headr" action="{!photos}" reRender="photos">Photos</apex:commandLink></td>
</tr>
</table>
</apex:pageblock>
<apex:outputPanel id="members">
<apex:outputPanel styleClass="popupBg" layout="block" rendered="{!displayPopUp}">
        <apex:outputPanel styleClass="popup" layout="block" rendered="{!displayPopUp}">
        <apex:pageblock title="Members" >
         <apex:pageBlockTable value="{!memb}" var="mem">
         <apex:column value="{!mem.Name}"/>
         <apex:column value="{!mem.E_Mail_Id__c}"/>
         <apex:column value="{!mem.Mobile_Number__c}"/>
         </apex:pageBlockTable>
         <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Ok" action="{!closePopup}"/>
         </apex:pageBlockButtons>
         </apex:pageblock>
</apex:outputPanel>
</apex:outputPanel>
</apex:outputPanel>

<apex:outputPanel id="blogs">
<apex:outputPanel styleClass="popupBg" layout="block" rendered="{!displayPopUp}">
        <apex:outputPanel styleClass="popup" layout="block" rendered="{!displayPopUp}">
        <apex:pageblock title="Blogs" >
         <apex:pageBlockTable value="{!blog}" var="blg">
         <apex:column value="{!blg.Name}"/>
         <apex:column value="{!blg.URL__c}"/>
         </apex:pageBlockTable>
         <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Ok" action="{!closePopup}"/>
         </apex:pageBlockButtons>
         </apex:pageblock>
</apex:outputPanel>
</apex:outputPanel>
</apex:outputPanel>

<apex:outputPanel id="photos">
<apex:outputPanel styleClass="popupBg" layout="block" rendered="{!displayPopUp}">
        <apex:outputPanel styleClass="popup" layout="block" rendered="{!displayPopUp}">
        <apex:pageblock title="Photos" >
         <apex:pageBlockTable value="{!photo}" var="pht">
         <apex:column value="{!pht.Name}"/>
         <apex:column value="{!pht.ID}"/>
         </apex:pageBlockTable>
         <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Ok" action="{!closePopup}"/>
         </apex:pageBlockButtons>
         </apex:pageblock>
</apex:outputPanel>
</apex:outputPanel>
</apex:outputPanel>

</apex:form>
</apex:page>

Apex:

public class showAll
{
    public boolean displayPopup {get; set;}
    public List<Member__c> memb {get;set;}
    public List<Blog__c> blog {get;set;}
    public List<Photo__c> photo {get;set;}
  
    public showAll()
    {
     displayPopup = false;
    }
  
    public void members()
    {
     displayPopup = true;
     String sql = 'SELECT Name,E_Mail_Id__c,Mobile_Number__c FROM Member__c';
     memb = Database.Query(sql);
    }
  
    public void blogs()
    {
     displayPopup = true;
     String sql = 'SELECT Name,URL__c FROM Blog__c';
     blog = Database.Query(sql);
    }   
  
    public void photos()
    {
     displayPopup = true;
     String sql = 'SELECT Name,ID FROM Photo__c';
     photo = Database.Query(sql);
    }
  
    public void closePopup()
    {
     displayPopup = false;    
    }
}

Output:






System mode or God mode in Apex and its impact by using “With sharing” keyword in Salesforce
In many of my previous implementations I have used advantage of Apex code running in System mode however never tried how far it can go in terms of capabilties. There is no specific Salesforce documentation which can explain that what is allowed or not allowed in System mode.
In this post, we will go through some scenario and will try to understand what really is possible in Apex System mode and when it can fail.
Before diving more lets discuss how many types of mode do we have in Apex ?
User Mode : As per this Salesforce post, all Profile level permissions, sharing rules and Field level security are enforced in Apex if it runs in User mode. Standard Controller and Anonymous Apex runs in User mode.
System Mode : Same post conforms that custom controller, trigger, Apex class, controller extension works in System mode. Means eventhough user does not have necessary profile level permission, record level permission or field level permission, but still they can perform any operation on it.
Creating test scenario
For testing purpose, I am going to use a user with “Chatter free” license and existing Opportunity record. Consider OWD for opportunity is “private”.
Apex class :
In this class, I am trying to update existing Opportunity
1public class  SystemMode_Scenario {
2    public static void perform(){
3        Opportunity Opp = [SELECT NAME,StageName FROM Opportunity WHERE Id = '006K000000CD1fZIAT'] ;
4        Opp.StageName = 'Negotiation/Review' ;
5        update Opp;
6    }
7}
Trigger on Feeditem :
Whenever chatter free user is posting any chatter comment, below trigger should run and execute method in Apex class
1trigger updateOpportunityFromChatterComment on FeedItem  (before insert) {
2    SystemMode_Scenario.perform();
3}
Chatter free user trying to update Standard Opportunity record – God mode in Action
As we know, Chatter free user cannot access any standard or custom objects. so as per documentation there is no way chatter user can access or update existing Opportunity.
However, if you try to add comment in chatter using chatter user, trigger will be able to update existing Opportunity eventhough user does not has CRUD permission at profile level. So, we can say Apex really works in GOD mode.
When Apex does not work in System or God mode – Gode mode failing because of “with sharing” keyword
Example 1 :
Update Apex class with below code, only difference you can spot is addition of “with sharing” keyword.
1public with sharing class  SystemMode_Scenario {
2    public static void perform(){
3        Opportunity Opp = [SELECT NAME,StageName FROM Opportunity WHERE Id = '006K000000CD1fZIAT'] ;
4        Opp.StageName = 'Negotiation/Review' ;
5        update Opp;
6    }
7}
If you try to add chatter comment, it will fail saying “List has no rows for assignment” because logged in user does not has access to existing Opportunity and Apex class is defined using keyword “with Sharing” and makes sense.
Salesforce System Mode fail- With Sharing keyword
Salesforce System Mode fail- “With Sharing” keyword
Example 2:
In this example, lets try to create new record for Lead object and therefore update Apex class with below code :
1public with sharing class  SystemMode_Scenario {
2    public static void perform(){
3        Lead l = new Lead(LastName='Zaa',Company='Cognizant');
4        insert l;
5    }
6}
In previous example, we have seen that Apex class failed because of “with sharing” keyword and it makes sense because chatter does not has record level permission.
In this example, we are not trying to access any existing record rather creating a new lead record. “with sharing” keyword only checks if user has access to existing record or not and therfore this code should work if Apex works in God mode.
However, code will still fail with below error saying “Invalid User Type”.
Salesforce God Mode failing
Salesforce God Mode failing
Conclusion:
So saying, Apex runs in God mode not realy true if class is defined using keyword “with sharing”. “with sharing” keyword not only checks record level permission however somehow it enforces profile level permission as well.
My two cents – Should we use System mode or rather I will say abuse God mode ?
So, there is way to get out of licensing cost. Consultants or developers may think that we can buy low cost license and using custom Apex and visualforce, same functionality can be achieved. However, I would not suggest because of below two reasons :
  1. Salesforce documentation clearly segregates capabilities of difference licenses and they can change System mode functionality anytime. In fact, they have already started it by introducing “with sharing” keyword.
  2. You’re violating the Master Service Agreement and/or related agreements. You could be charged for full licenses retroactively, and failure to pay is breach of contract that will result in a lawsuit for damages, plus the loss of data that could result.