Posts

Showing posts from 2017

System.CalloutException: Callout from triggers are currently not supported

The error message "System.CalloutException: Callout from triggers is currently not supported" indicates that a trigger in your Salesforce org is attempting to make a callout to an external system, which is not allowed. Salesforce does not allow callouts to be made directly from triggers. This restriction ensures the integrity and consistency of data in the Salesforce database. If handled improperly, callouts can result in long-running transactions, leading to performance issues or data inconsistencies. For example, when a new Account record is inserted and this trigger fires, you encounter the "System.CalloutException: Callout from triggers are currently not supported" error. trigger AccountTrigger on Account (after insert) {     for (Account acc : Trigger.new) {         // Make a callout to an external API         HttpRequest req = new HttpRequest();         req.setEndpoint('https://api.example.com');    ...

Lightning Design System (SLDS) in Visualforce pages

Salesforce Lightning Design System provides a set of CSS classes and design guidelines that allow you to create user interfaces that are consistent with the Salesforce Lightning Experience.  You can use the Lightning Design System (SLDS) to build Visualforce pages that match the look and feel of the Salesforce mobile app. To use SLDS, it takes some tweaks in your code and a few things to remember. Visualforce Page with SLDS <apex:page applyHtmlTag="false" applyBodyTag="false">     <html lang="en">     <head>         <title>SLDS Example in Visualforce</title>         <apex:slds />     </head>     <body>         <div class="slds-scope">             <div class="slds-grid slds-grid_vertical">                 <div class="slds-col slds-size_1-of-2">  ...

Exposing Apex Methods as SOAP Web Services

By exposing them as SOAP web services, apex methods are available to external applications. To make Apex methods available as SOAP web services, we must declare them within a global Apex class and use the "webservice" keyword. Salesforce will then automatically create a WSDL document for that class, which can be utilized by external applications to access the exposed web services. Example global class MyWebService {     webservice static Id makeContact(String contactLastName, Account a) {         Contact c = new Contact(lastName = contactLastName, AccountId = a.Id);         insert c;         return c.id;     } } To generate the WSDL file for a Salesforce instance, we can simply append "?wsdl" to the instance URL, followed by the path to the Apex class.  For example, the URL should look like https://yourinstance.salesforce.com/services/wsdl/class/MyWebService. Once you have the WSDL file, you can generat...

Maximum trigger depth exceeded

"Maximum trigger depth exceeded" is an error message in Salesforce that occurs when there's a recursive trigger execution that exceeds the platform's governor limit. Let's consider a scenario with a Contact object with a custom field called Total_Contacts__c. This field represents the total number of contacts associated with an account. We aim to ensure that this field is always up-to-date whenever a new contact is created, updated, or deleted.  To achieve this, we implement triggers on the Contact object that update the Total_Contacts__c field on the related Account object. Unfortunately, due to a mistake in the trigger logic, the triggers recursively update contacts and accounts, causing an error. Here's an example trigger UpdateTotalContacts on Contact (after insert, after update, after delete) {     if (Trigger.isAfter) {         if (Trigger.isInsert || Trigger.isUpdate) {             List<Account> accountsToUpda...

Salesforce Platform Cache

Salesforce Platform Cache is a feature that allows you to store and access data in memory, providing faster access to frequently used or computationally expensive data. It's particularly useful for improving the performance of Salesforce applications by reducing response times and decreasing the load on backend systems. The Platform Cache API lets you store and retrieve data that’s tied to Salesforce sessions or shared across your org. Put, retrieve, or remove cache values by using the Session, Org, SessionPartition, and OrgPartition classes in the Cache namespace. Use the Platform Cache Partition tool in Setup to create or remove org partitions and allocate their cache capacities to balance performance across apps. There are two types of cache: Session cache —Stores data for individual user sessions. For example, in an app that finds customers within specified territories, the calculations that run while users browse different locations on a map are reused. Session cache lives al...

Get Picklist Values Using Dynamic Apex

Image
  The picklist field name and object are the two parameters that the code snippet below requires. We can read either the Picklist label or the value based on these two inputs. String objectName = 'Account'; String fieldName ='Industry'; Schema.SObjectType s = Schema.getGlobalDescribe().get(objectName) ; Schema.DescribeSObjectResult r = s.getDescribe() ; Map<String,Schema.SObjectField> fields = r.fields.getMap() ; Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe(); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); for( Schema.PicklistEntry pickListVal : ple){ System.debug(pickListVal.getLabel() +' ---- '+pickListVal.getValue()); }  

Use Of addFields in ApexPages.StandardController

The add fields method helps developers specify fields for the standard controller to query in Visualforce pages. public AccountController(ApexPages.StandardController stdController){             this.controller = stdController;             List<String> fieldNamesList = new List<String>{Type,Industry};             stdController.addFields( fieldNamesList );             // stdController.addFields(new List<String>{'Name', 'TaxExemption__c', ' Industry '}); } Reference: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_addFields.htm

Lock Records Using Apex in Salesforce

Locking Statements In Apex, you can use FOR UPDATE to lock sObject records while they’re being updated to prevent race conditions and other thread safety problems. Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE]; Locking Consideration When records are locked by a client, that client can modify the record's field values in the database within the same transaction. Other clients must wait until the transaction is completed and the records are no longer locked before they can update the same records. However, other clients can still query the locked records. If a record is already locked by another client, a process will wait for a maximum of 10 seconds for the lock to be released before acquiring a new lock. If the wait exceeds 10 seconds, a QueryException is thrown. Similarly, if a record is locked by another client and the lock is not released within 10 seconds, a DmlException is thrown. If a client tries to modify a locked record, the update can succeed if the lock ...