Explain about “Too Many SOQL Error: 101”


Error: System.LimitException: Too many SOQL queries: 101

Too many SOQL queries: 101




“System.LimitException: Too many SOQL queries: 101” errors occur when you exceed SOQL queries governor limit. Actuval limit is “you can run up to a total 100 SOQL queries in a single call or context”.




Ho to resolve this “Error: System.LimitException: Too many SOQL queries: 101”

  • Change your code by fallowing apex code best practices so that the number of SOQL fired is less than 100.
  • If you need to change the context, you can use @future annotation which will run the code asynchronously.

Apex runs on a multi-tenant platform, the apex run time engine strictly enforces limits to ensure code does not monopolize shared resources.

  • Avoid SOQL queries in for loops.
  • Fallow Apex code key principals while writing Triggers and bulk requests.

Reference for Apex code best practices: https://developer.salesforce.com/page/Apex_Code_Best_Practices




Explain about Asynchronous Apex in Salesforce?

Asynchronous Apex Salesforce

Asynchronous Apex

Asynchronous Apex is used to run process in a separate thread at later time. It is process or function that executes a task “in the back ground” without the user having to wait for the task to finish.



We can typically use asynchronous process for callouts to external systems, operations that require higher limits, and code that needs to run at a certain time.

Benefits of Asynchronous process are User efficiency, Scalability and higher limits.

There different ways to implement Asynchronous process in Apex.

  1. Future methods
  2. Batch Apex
  3. Queueable Apex
  4. Scheduled Apex




To know more about Asynchronous Process refer this URL: https://trailhead.salesforce.com/trails/force_com_dev_intermediate/modules/asynchronous_apex/units/async_apex_introduction




What is Platform Cache in Salesforce?

Platform Cache – Salesforce

What is a Cache? Cache is a temporary storage for frequently accessed data from a database

What is Platform Cache? It is a memory layer that stores Salesforce session and org data for later access. When you use Platform Cache, your applications can run faster because they store reusable data in memory. Applications can quickly access this data; they don’t need to duplicate calculations and requests to the database on subsequent transactions. In short, think of Platform Cache as RAM for your cloud application.



Types of Platform Cache:

  1. Org Cache: Org Cache stores org-wide data that anyone in the org can use. Org Cache is accessible across sessions, requests, and org users and profiles.
  2. Session Cache: Session Cache stores data for an individual user and is tied to that user’s session. The maximum life of a session is 8 hours.

What is Cache Partition? Cache partition allows you to allocate space to balance usage and performance across apps. Caching data to designated partitions ensures that the cache space isn’t overwritten by other apps or by less critical data. Before you can use cache space in your org, you must create partitions to define how much capacity you want for your apps. Each partition capacity is broken down between org cache and session cache.



Cache allocation by edition: Platform Cache is available to customers with Enterprise Edition orgs and above.

Enterprise Edition – 10 MB by default, Unlimited Edition – 30 MB by default, Performance Edition – 30 MB by default.





Future method in salesforce – @future

Explain about future method in salesforce?

Future method in salesforce: Future methods are used to run the process in a separate thread, at later time when system resources are available. We can use future methods for any operation we would like to run asynchronously in its own thread.




Future methods provide the benefits of not blocking the users from performing other operations and providing higher governor and execution limits for the processes.

Future method syntax:



  • Use @future annotation before method declaration.
  • Future methods must be static methods, and can only return a void type.
  • The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
  • Future methods can’t take standard or custom objects as arguments.
  • A common pattern is to pass the method a list of record IDs that you want to process asynchronously.

global class YourClassName {

  @future

  public static void yourFutureMethodName(List<Id> recordIds) {

    List<Account> acc = [Select Id, Name from Account Where Id IN :recordIds];

    // process account records to do awesome stuff

  }

}

Best practices to implement future methods:



  • Ensure that future method execute as fast as possible.
  • If using webservice callouts, try to bundle all callouts together from same future method, rather than using a separate future method for each callout.
  • Conduct thorough testing at scale. Test that a trigger enqueuing the @future calls is able to handle a trigger collection of 200 records. This helps determine if delays may occur given the design at current and future volumes.

 Consider using Batch Apex instead of future methods to process large number of records asynchronously. This is more efficient than creating a future request for each record.

To know more,  refer below Salesforce Trailhead URL.

https://trailhead.salesforce.com/trails/force_com_dev_intermediate/modules/asynchronous_apex/units/async_apex_future_methods




How to access metadata from APEX?

How to access metadata from APEX? – Summer’17 release feature

Access Metadata from Apex, What is metadata in salesforce? In Salesforce, if you create object, field, class, page, trigger and any other component type which represents org configuration and customization will be stored in the form of metadata. We can access this metadata by using Metadata API. With Summer’17 Salesforce release, we can use classes in the metadata name space to access metadata components from Apex Code.




As per the salesforce release notes documentation, Access metadata from your Apex code to handle below tasks.

Customizing app installs or upgrades: During or after an install (or upgrade), your app can create or update metadata to let users configure your app.




Customizing apps after installation: After your app is installed, you can use metadata in Apex to let admins configure your app using the UI that your app provides rather than having admins manually use the standard Salesforce setup UI.

Securely accessing protected metadata: Update metadata that your app uses internally without exposing these types and components to your users.

Creating custom configuration tools: Use metadata in Apex to provide custom tools for admins to customize apps and packages.

This feature (Metadata access in apex) is currently limited to custom metadata records and page layouts. We can retrieve, create or update custom metadata but we cannot delete metadata through Apex.




– Use Metadata.Operations class to retrieve and deploy metadata.

– Use the Metadata.Operations.retrieve() method to synchronously retrieve metadata from the current org.

-Use the Metadata.Operations.enqueueDeployment() method to asynchronously deploy metadata to the current org.

Refer Salesforce Apex developer guide to know how to use this feature.





System assertEquals Salesforce – System methods

System assertEquals Salesforce

System assertEquals Salesforce

System assertEquals Salesforce, Here I am posting about assert methods in System class. This helps everyone to find the differences between assert methods in salesforce.




Below are the three assert methods in system class.
1. System.assert(condition, msg)
2. System.assertEquals(expected, actual, msg)
3. System.assertNotEquals(expected, actual, msg)

What is System.assert(condition, msg)?

This method asserts that the specified condition is true. If it is not true, a fatal error is returned that causes code execution to halt.



Signature of this method:
Public static void assert(Boolean condition, Object msg)

This method is having two parameters, one is condition which is Boolean type, the other one is msg which is optional and object type.

 What is system.assertEquals(expected, actual, msg)?

This asserts that the first two arguments are the same. if they are not same, a fatal error is returned that causes code execution halt.

Signature of this method:
Public static void assertEquals(Object expected, Objecet actual, object msg)

This method is having three parameters, one is expected which is a type of object, second one is actual which is also type of object and the other one msg which is optional and type of object.

What is system.assertNotEquals(expected, actual, msg)?

This asserts that the first two arguments are not same. if they are the same, a fatal error is returned that causes code execution halt.



Signature of this method:
Public static void assertNotEquals(Object expected, Objecet actual, object msg)

This method is having three parameters, one is expected which is a type of object, second one is actual which is also type of object and the other one msg which is optional and type of object.

Note: We cannot catch an assertion failure using a try/catch block even though it is logged as an exception in case all above three methods.

To know more about this refer below Salesforce documentation URL about salesforce system class.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm#apex_System_System_methods




Salesforce integration testing – WebServiceMock interface

Salesforce integration testing – WebServiceMock interface

Salesforce integration testing: Generating web service class and how to use those web service classes was explained in my previous post. Here I am going to explain about how to write test class for web services class generated WSDL2Apex.




Every Salesforce developer know about importance of test classes. At least 75% code coverage is required to deploy code to production. Test coverage includes classes generated by WSDL2Apex.

As a Salesforce developer, to write test classes for web service you should spend some time as test methods do not support web service callouts. Apex provides inbuild interface “WebServiceMock” and the “Test.setMock” method. Developers can use this interface to receive fake response in a test method.

What is WebServiceMock interface?



WebServiceMock interface enables sending fake response when testing web service callouts of a class auto-generated from WSDL.

doInvoke is the method of this interface.

doInvoke(stub, soapRequest, responseMap, endpoint, soapAction, requestName, responseNameSpace, responseName, responseType)



The implementation of this method is called by the Apex Runtime to send a fake response when a service callout is made after Test.setMock has been called.

You can refer below Salesforce documentation to know more about this interface.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_webservicemock.htm




SOAP web service – Creating a class from WSDL

SOAP web service – How to creating a class from WSDL document?

SOAP web service class can be generated automatically by using WSDL document. Generating WSDL document allows Salesforce developers to make callouts to the external web service in their Apex code.




Follow below steps to generate apex class from a WSDL:

1.Go to Apex Classes form setup menu.
2.Click “Generate from WSDL” button.

SOAP Web Service 1
3.Select WSDL document(XML file) you are using to generate apex class by clikcing choose file lookup button. This WSDL document is base to generate apex class.

SOAP Web Service 2Here I selected Test-SOAP webservice WSDL file which is rounded by red color line in the above image. You can select WSDL by clicking on choose File button.

4.Click Parse WSDL button which is highleted in the above image to verify WSDL document contents. It generates default class name based on WSDL document. It report errors in case of the WSDL contains schema types or constructs that aren’t supported by Apex classes, or if the resulting classes exceed the 1-million-character limit on Apex classes. For example, the Salesforce SOAP API.



SOAP Web Service 3You can rename the class. And click on Generate Apex button. The final page of the wizard shows which classes were successfully generated, along with any errors from other classes. The page also provides a link to view successfully generated code.

Generated Apex classes include stub and type classes for calling the third-party Web service represented by the WSDL document. These classes allow you to call the external Web service from Apex.

When you generate Apex classes by using WSDL, it generated 2 classes, one is with given name for synchronus callouts and other is prefixed with Async for asynchronus callouts.



SOAP Web Service 4

Refere here to know more about SOAP callouts: https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_soap_callouts




Salesforce Integration – SOAP API basics

SOAP API basics

SOAP API (Simple Object Access Protocol API) can be used to create, retrieve, delete, update records. We can use SOAP API in any language that supports web services.

We can create, update, delete, retrieve records in force.com from any external system that supports SOAP based web services by using force.com SOAP API (like Java, .net etc.…) with more than 20 different calls.




SOAP-API also allows you to maintain passwords, perform searches, retrieve metadata.

Salesforce provides two different SOAP API WSDLs (WSDL: Web service description language).

     1. Enterprise WSDL

     2. Partner WSDL

What is WSDL?

WSDL (Web service description language) is an XML document which contains a standardized description on how to communicate using a web service.



What is Enterprise WSDL?

Enterprise WSDL is a strongly typed WSDL for customers who want to build an integration with their Salesforce organization.  It is intended primarily for customers.

What is partner WSDL?

Partner WSDL is a loosely typed WSDL for customers and partners who are building client applications for multiple originations. This WSDL can be used to access data within any organization. It I intended primarily for partners.



How to download WSDL file?

Click Setup -> Customize -> Develop -> API
Click the link to download the appropriate WSDL.
and Save the file locally, giving the file a “.wsdl” extension.