The System.TriggerOperation enum has the following values:
BEFORE_INSERE
AFTER_INSERT
BEFORE_UPDATE
AFTER_UPDATE
BEFORE_DELETE
AFTER_DELETE
AFTER_UNDELETE
The new Trigger context variable Trigger.operationType will return System.TriggerOperation enum during trigger context.
If you combine this new context variable and the new Apex switch feature, trigger code becomes much easy to implement and understand.
Sample Switch and enum in Triggers:
trigger AccountTrigger on Account(before insert, after insert, before update, after update, before delete, after delete, after undelete) {
switch on Trigger.operationType {
when BEFORE_INSERT {
//Invoke before insert trigger handler
system.debug('Before Insert');
}
when AFTER_INSERT {
//Invoke after insert trigger handler
system.debug('After Insert');
}
when BEFORE_UPDATE {
//Invoke before update trigger handler
system.debug('Before Update');
}
when AFTER_UPDATE {
//Invoke after update trigger handler
system.debug('After Update');
}
when BEFORE_DELETE {
//Invoke before delete trigger handler
system.debug('Before Delete');
}
when AFTER_DELETE {
//Invoke after delete trigger handler
system.debug('After Delete');
}
when AFTER_UNDELETE {
//Invoke after undelete trigger handler
system.debug('After Undelete');
}
}
}
Salesforce has introduced LogoutEventStream in Summer’18 release, to get the User logout events.
It is a beta feature and to make it available in your org, you need to contact Salesforce Support.
After it will available in your Org, you can enable LogoutEventStream.
After enabled LogoutEventStream, Salesforce publishes logout events when users log out from the UI.
You can add an Apex trigger to subscribe to those events, where you can implement our custom logic during logout.
Enable Logout Events Stream :
Go to Setup | Event Manager (Enter Event Manager in Quick Find) | Click on Logout Event link and click on Update Event on right coroner button and select Enable Streaming.
After enable Logout Events Stream, I have created an Apex trigger on LogoutEventStream object, where I’m saving the User Logout Event information in a Custom Object.
As a Salesforce developer sometimes we face different scenarios, where we need to track production error logs or failed records. To save custom logs, here I have created an object Log__c and a helper class LogHandler, which will help to save custom logs for future references.
Create below apex class for Community User Rest API login:
Apex Class:
/*
Author : Biswajeet Samal
Description : Community users login using rest API
*/
@RestResource(urlMapping='/CommunityLoginAPI/*')
global without sharing class CommunityLoginAPI {
/*
* Validate User login
* @param username : Login User Username
* @param password : Login User Password
* @param domain : Login domain instance ('login' for a prod/dev instance) and ('test' for a sandbox instance)
* @return LoginResponse : For login validate response message
*/
@HttpPost
global static LoginResponse login() {
LoginResponse objResponse = new LoginResponse();
String username = RestContext.request.params.get('username');
String password = RestContext.request.params.get('password');
String domain = RestContext.request.params.get('domain');
try{
string loginXML = '<?xml version="1.0" encoding="utf-8"?>';
loginXML += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">';
loginXML += '<soapenv:Header>';
loginXML += '<urn:LoginScopeHeader>';
loginXML += '<urn:organizationId>'+ UserInfo.getOrganizationId() +'</urn:organizationId>';
loginXML += '</urn:LoginScopeHeader>';
loginXML += '</soapenv:Header>';
loginXML += '<soapenv:Body>';
loginXML += '<urn:login>';
loginXML += '<urn:username>'+ username +'</urn:username>';
loginXML += '<urn:password>'+ password +'</urn:password>';
loginXML += '</urn:login>';
loginXML += '</soapenv:Body>';
loginXML += '</soapenv:Envelope>';
HttpRequest request = new HttpRequest();
request.setEndpoint('https://'+ domain +'.salesforce.com/services/Soap/c/44.0');
request.setTimeout(60000);
request.setMethod('POST');
request.setHeader('SOAPAction', 'login');
request.setHeader('Accept','text/xml');
request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
request.setBody(loginXML);
HttpResponse response = new Http().send(request);
String responseBody = response.getBody();
String sessionId = getValueFromXMLString(responseBody, 'sessionId');
objResponse.statusMessage = response.getStatus();
objResponse.statusCode = response.getStatusCode();
if(string.isNotBlank(sessionId)){
objResponse.isSuccess = true;
objResponse.sessionId = sessionId;
}else{
objResponse.isSuccess = false;
}
}
catch(System.Exception ex){
objResponse.isSuccess = false;
objResponse.statusMessage = ex.getMessage();
}
system.debug('objResponse-' + objResponse);
return objResponse;
}
/*
* Get XML tag value from XML string
* @param xmlString : String XML
* @param keyField : XML key tag
* @return String : return XML tag key value
*/
public static string getValueFromXMLString(string xmlString, string keyField){
String xmlKeyValue = '';
if(xmlString.contains('<' + keyField + '>')){
try{
xmlKeyValue = xmlString.substring(xmlString.indexOf('<' + keyField + '>')+keyField.length() + 2, xmlString.indexOf('</' + keyField + '>'));
}catch (exception e){
}
}
return xmlKeyValue;
}
global class LoginResponse {
public String sessionId {get; set;}
public Boolean isSuccess {get; set;}
public String statusMessage {get; set;}
public Integer statusCode {get; set;}
}
}
1. Create or Open Community and Activate
2. After community creation go to Sites | Click on Community Name | Public Aaccess Setting | Enabled Apex Class Access | Add above class “CommunityLoginAPI” | Save