System mode is nothing but running apex code by ignoring user’s permissions. For example, if logged in user does not have create permission but they will able to create a record.
In system mode, Apex code has access to all objects and fields permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user.
In Salesforce, all apex code run in system mode. It ignores user’s permissions. Only exception is anonymous blocks like developer console and standard controllers. Even runAs() method doesn’t enforce user permissions or field-level permissions, it only enforces record sharing.
User Mode:
User mode is nothing but running apex code by respecting user’s permissions and sharing of records. For example, if logged in user does not have create permission they are not able to create a record.
In User mode, Profile level permissions, field-level security, and sharing rules are applied for the current user.
In Salesforce, only standard controllers and anonymous blocks like developer console run in user mode.
Mode of execution:
Trigger – System
Validation Rule – System
Auto Response Rule – System
Assignment Rule – System
Workflow Rule – System
Escalation Rule – System
All Types of calculation behind formula, Rollup Summary – System
Process Builder – System
Visual Workflow or flow – User
if flow is called from Process Builder – System
if flow is called from Workflow – System
if flow is called from Apex – (depends on with or without sharing of apex class)
if flow is called from Custom Button – System
if flow is embed in Visualforce – Depends on VFP context
if flow is called from REST API – System
Approval Process – System
Publisher Action – System
InvocableMethod
if this is called from flow – User
if this is called from Process Builder (does it depends on with or without sharing is specified on that Class) – System
if this is called from REST API – (depends on with or without sharing of the class)
Custom Button – System
Test method with System.runAs() – User
Test method without System.runAs() – System
Visualforce Page (StandardController) – User
Visualforce Page (StandardController with extension) – System
Visualforce Page (Custom Controller)
depends on with or without sharing of the controller
Visualforce Component – depends on Visualforce page where it is used
Macros – System
Annonymous Apex – User
Chatter in Apex – User
Email Service – User
All types of Jobs – System
Apex Webservices (SOAP API and REST API) – System (Consequently, the current user’s credentials are not used, and any user who has access to these methods can use their full power, regardless of permissions, field-level security, or sharing rules.)
Basically Salesforce offers two types of relationship:
One To Many Relationship
Many To Many Relationship (Using the concept of Junction object)
Sometimes we may need One To One relationship, But unfortunately Salesforce doesn’t allow any direct methodology to build One To one relationship.
Let’s consider the scenario that we want to establish a One to One relationship between two custom objects Employee__c and PAN_Card__c.
So, here are few ways to implement One To One relationship between two objects in Salesforce. We can achieve this by using configuration only and can also achieve this by using code to make it more scalable.
Option 1:
Create a lookup field on PAN_Card__c to Employee__c.
Create a custom field on the PAN_Card__c object and make the field unique. This field would be used to hold the ID of the associated Employee__c. Hide this field from all page layouts.
Create a Workflow rule on PAN_Card__c. For any change of the lookup field on PAN_Card__c object, update the custom field on the PAN_Card__c object with the value of the associated Employee Id.
We have established a one to one relationship between PAN_Card__c and Employee__c. When we try to add a second PAN_Card__c to the Employee__c, the “unique” constraint would be violated and an error would be thrown. This approach will work on both standard and custom object.
Option 2:
Create a master detail relationship on PAN_Card__c to Employee__c object.
Create a roll up summary field on Employee__c object of PAN_Card__c with count type.
Create a validation rule on Employee__c object rollup summary field to check if count > 1.
In this way also, We have established a one to one relationship between PAN_Card__c and Employee__c. So it will throw an error if Employee__c has more than one PAN Card.
Option 3:
Create lookup fields on both objects PAN_Card__c and Employee__c, to each other.
Write triggers, for any change on these lookups, to either copy the record ID into the other object’s lookup field when the other object’s lookup field is empty, or disallow the change to the original record when the other object’s lookup field is already populated with a different ID from the original record.
This is already having a one-to-onePassport relation.
Option 4:
Create a trigger on PAN_Card__c object to check if the PAN Card record already exists for an Employee. If it exist, then throw an error, else allow the user to create.
Here is the example for Employee__c and PAN_Card__c object:
trigger PANCardValidation on PAN_Card__c (before insert, before update) {
Set<id> employeeIds = new Set<id>();
Map<id, Employee__c> mapEmployee = new Map<id, Employee__c>();
for (PAN_Card__c p : trigger.New) {
employeeIds.add(p.Employee__c);
}
List<Employee__c> lstEmployee = [SELECT Id, Name FROM Employee__c WHERE Id IN : employeeIds];
if (!lstEmployee.isEmpty()) {
for (Employee__c emp : lstEmployee) {
mapEmployee.put(emp.Id, emp);
}
for (PAN_Card__c p : trigger.New) {
if (mapEmployee.containsKey(p.Employee__c)) {
p.addError('A PAN Card already exist for the employee - ' + mapEmployee.get(p.Employee__c).Name);
}
}
}
}
If you are setting your docType="html-5.0" and having a validation error on cancel, after the immediate="true" for a command button, action function etc.
Here are two options for disable all validation for all command buttons and disable validations for specific command button.
Option 1:
If your requirement is to disable all validation for entire page, then you can add html-novalidate="novalidate" attribute to apex form. This will disable all validations for all command buttons.
Option 2:
If your requirement is to disable the validation for specific command button, then you can add immediate="true" and html-formnovalidate="formnovalidate" attribute to that command button. This will disable all validations for all command buttons.