 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about interview questions and answers related to Apex programming language. So, if you're giving any interview and interviewer asks like, what is Apex? So, you can just say Apex is object-oriented programming language, which is used in Salesforce. It is strongly typed as it validates references to objects at compile time integrated with the database. So, with the help of Apex, you can interact with the S objects. So, we have SOQL, SOSL, and DML for that purpose. Then, it enables developers to add business logic. And we can call Apex through WAP service requests and triggers on objects. What all data types available in Apex? It can be another question. So, you can say like we have primitive, which have integer, double string, long date, ID, Boolean, and more. Then we have S object. So, either as a generic S object or as a specific S object, for example, account. Then we have three collection, which is list, set, and map. So, these three you can use as a data type in Apex. So, if you want to store values in a collection, in a group. So, for that purpose, you can use list, set, and map. Then, we have enum. Then we have user defined Apex classes and system supplied Apex classes. So, Apex classes are also of two types like if Salesforce already providing you any Apex class. So, you can say system supplied, if you are creating, so it will be user defined. And Apex class will also become a data type. So, if you want to create its instance, so that is also possible. So, as a fresher, if you given the interview, so there can be another question like what collections are available in Apex. So, we have three types of collections, list, set, and map. Now, what is list? It can be another question. So, it is an ordered collection of elements. Each element of list has an index for identification. Index position of first element is always zero. List can be nested and even multi-dimensional. And elements can be of any data type. It can be primitive, collections, S objects, user defined types, built-in types. So, if you are creating any list, so data type can be anything, right? So, for example, if you want to store five account records together, so you can create list of account. If you want to store five integers, so you can create list of integer like that. So, if interviewer asked like, can you quote some examples? So, here you can see some examples are available. So, list of string name is my list and we are initializing it. So, here in this list, we can store string values. Then second is list of account which can store collection of account records, then list of integer. And if you want to add some values into list, you can use add method. If you want to get particular value that is stored at particular index, so you can use get method and you need to pass the index value. If you want to add some value at particular index, you can use add method. And with two parameters, first represents index and second represents value. And if you apply SQL like you are querying some data from S object and you want to store that into a list, so this way you can write it. Next collection is set. So, set is basically unordered collection of elements. So, list is ordered whereas set is unordered. It doesn't contain duplicate elements where in case of list you can store duplicates as well. Sets can contain collections that can be nested within one another. So, we can create nested sets. It is similar to list like in list also we can create nested list. Elements of set can be of any type like primitive, collection, S object, user defined types or built in types. So, here user defined types and built in types means apex classes, right? So, here you can see some examples of set. So, first is set of string, second is set of ID. So, generally we prefer set of ID because if you create list of ID, so that list can store duplicate IDs. But in case of set, IDs will be unique. So, generally for ID we prefer set then set of integer. So, here you can see we are adding values through add method. We are removing value through remove method. Then contains method will check whether particular value is available in set or not. So, it returns true or false that you can store in a Boolean variable. Then if you want to check the size, so we have size method. If you want to check whether set is empty or not, so we have is empty method. So, this way various methods you can apply with set as well as with list. Next collection is map. So, what is map in apex? So, map is also a collection of but it is key value pair. So, keys are always unique having a value associated. So, one pair will be having both like one is key and one is value. Values can be duplicate. Adding a map entry with an existing key overrides the existing entry with new. So, if particular key is already available if you store same key. So, previous keys value will be replaced with the new one. Map key and values can contain any collection and can contain nested collections. Keys and values can be of any data type. So, it can be of primitive collection, S object, user defined type or built in type. So, here you can see some examples. So, in first example, map of string comma string is created. So, first string will be key and second string will be value. Then second example is for integer and string. So, integer will be key and string will be values and how we can put. So, throughput method we can store the values like one comma first. So, one will become the key and first will become the value. Then two comma second, then here we have contains key. So, contains key check whether particular key is available or not. So, it returns Boolean value which we can store in a Boolean variable. Then if you want to get value associated with a key. So, if I am passing to in get method, so it will return the value associated with it. So, in our example, it will return second and that will be stored in value variable. Then if you want to fetch all the keys, those are available in the map. So, you can use key set. Then we have map of ID comma count. So, keys will be IDs and value will be a count record. Then we have list of account where we are querying some data. And in number five point you can see I am passing that list into map. So, that list will be converted into map. So, the list will be containing account records. So, account records ID will be the key and account record will become the value automatically. And last point you can see we are having map of account comma list of content. So, keys are account records and values will be list of content. So, this is also possible. Now, what is the difference between for loop and for each loop? So, for loop, like we can write it like this. We need to initialize then we need to put condition. We need to put increment and decrement. And inside braces we can write some code. Whereas for each loop basically is implemented for iterating collections. So, if you have to add some data from your S object and it is stored in a list. And if you want to iterate on that list. So, in that case you can use for each loop like this. So, we are iterating on ACS list in this example. And one by one each account record will be stored in ACC variable. And inside the braces you can write some piece of code. Now, this is interesting question. What is SOQL? So, SOQL stands for Salesforce object query language. It reads records from Salesforce. Similar to the standard SQL, but it is customized for the lighting platform. SOQL can be embedded in Apex port. So, if you're writing any Apex port, so inside that you can write SOQL. So, here is the example. Select ID from account limit five. So, select is for querying some data. Then we need to put the field APIs. If you have multiple field APIs, so you can put them comma separated. Then from and then S object API. And then you can apply some clauses like limit, where, etc. And the return result you can store in a collection. So, here we have collection of type list of account. Then similar to SOQL, next we have SOSL. So, Salesforce object search language. So, this is the full form of SOSL. It is used to perform text search and records. Use SOSL to search fields across multiple S object records. Use SOQL to retrieve records for a single object. Whereas use SOSL to search fields across multiple objects. So, here's the syntax. So, you need to write find and search query. Then you can write search group and then returning objects and fields. Now, what is developer console? So, developer console is an integrated development environment. Here you can create and edit Apex classes, trigger, Aura components, VF pages, etc. You can generate logs for debugging and analyzing them. You can create test classes. So, you can test Apex code to ensure that it is error free. So, after implementing test classes, you can run those as well and you can see the code coverage for your Apex code. Then write or execute SOQL and SOSL queries to find, create, and update the records in the org. So, these are the things you can do with the help of developer console. What is query editor in developer console? So, it helps to execute SOQL and SOSL. What is execute anonymous window? So, it helps to execute Apex code without creating any class. You can execute all lines of code altogether or you can select particular lines for execution as well. You also get an option to open logs just after completion of execution. You cannot save code through anonymous window. So, this is an option which is available in developer console that you can use to run Apex code. What is system.debug? So, display results in the log. Use system.debug in the code for debugging purpose. Once your code is tested and error free, then remove all the system.debug as part of the aspectus. So, here is the example. So, whatever you write in the system debug, so, it will be available in the logs. So, sometimes for debugging purpose, we use system.debug. What is DML? So, DML is used to insert, update, delete and undelete records. You can use absurd to either insert or update a record. So, if you are inserting some record and you are updating some records together. So, instead of writing two separate DMLs, like one is insert or another is update, you can club them and you can use absurd DML. Use merge when duplicate leads, context and accounts are there into one record. Others are deleted and related records are reparented. And always perform DML in bulk. So, this is important because we have governed a limit for DML that is 150. So, in a single transaction, if you have more than 150 DMLs, so, that will be an error and your code will break. So, instead of applying DML on separate records, you can store those records in a collection and then you can apply that DML. What is governor limit? So, as Apex runs in a multi-tenant environment. So, Apex runtime engine strictly enforces limits. So, multi-tenant environment means like more than one or users are using shared resources, right? So, this is because runaway Apex code or processes don't monopolize shared resources. If some Apex code exceeds a limit, then associated governor issues a runtime exception that cannot be handled. These limits count for each Apex transaction. For batch Apex, these limits are reset for each execution of a batch of records in the execute method. So, these are some points that you need to remember about governor limits. And here you can see we have some governor limits that you can remember for interview preparation purpose because an interview like some interviewer asked this question. So, randomly, they can ask like, what is like total number of SOQL queries limit? So, for synchronous, that is 100. For asynchronous, that is 200. Then total number of records retrieved by SOQL queries. So, for synchronous, 50,000 and same for async. Then total number of SOSL query issued is also same for both 20. Total number of records retrieved by a single SOQL query is also same, 2000. Total number of DML statements should 150, 150, same for both. Total stack depth for any Apex invocation that recursively fires triggered due to insert, update or delete statements, 16 each. Total number of callouts can be at HTTP request or web services calls in a transaction. So, both have 100. Then we have some more limits like maximum cumulative timeout for all callouts. It can be HTTP request or web service calls in a transaction. So, 120 seconds. Then maximum number of methods with a future annotation allowed per Apex invocation. So, in sync, it is 50. In async, it is zero in batch and future context and one in queueable context. Maximum number of Apex jobs added to the queue with system.encu job. So, sync, it is 50. For async, it is 1. Total heap size, 6 MB, 12 MB, respectively. Maximum CPU time on the Salesforce servers. So, for sync, it is 10,000 milliseconds. For async, it is 60,000 milliseconds. Maximum execution time for each Apex transaction, 10 minutes each. And total number of send email methods allowed 10 each. Now, next question is what is database class? So, Apex contains the build-in database class. Database class provides methods which can perform DML operation. So, now we have two things here like DML, insert, update, delete, absurd, like that. And along with that, we have a separate class which is known as database class. So, if you want to use it. So, here you can see the list of methods available. So, these methods are static and you can call them through the name of the class. So, database.insert, update, absurd, delete, undelete, merge. And if you want to learn like how we can implement these. So, I already recorded some detailed videos. So, you can visit studiesalesforce.com and there you will find detailed videos having explanation on each topic. More on database class. So, database class methods have an optional all or none parameter. This parameter allows you to specify whether the operation should partially succeed. When this parameter is set to false, if error occurs on a partial set of reports, then the successful reports will be committed and errors will be returned for the failed statement. So, this is important. So, in DML, it doesn't happen. In DML, like if one record is failing, so all other records will also fail. No exceptions are thrown with the partial success option. This feature is not allowed with DML statement. The database methods return result objects containing success or failure information of each record. So, here is the example like if you have a list, let's suppose record list and optional parameter, it is second, which is false, which is like all or none parameter. So, we are passing false here. So, now from context, sorry, from record list, if some records are successful and some are failing. So, those which are successful, they will be inserted because we are using database.insert and failure will be failed only and all success and failure information will be available in this results array, which is of type database.save result. Now, if you want to compare, so this is the question like if you are giving an interview as a fresher, then also this question can be asked and if you are giving interview as like two, three years experience candidate, so like interviewer can ask like what is the difference between DML versus database class. So, you can use DML statements if you want to throw error through Apex exception and handle them with try and catch plot. Then use database methods if you want to allow partial success of a bulk operation. In this way, successful records will be committed and errors will be returned for the failed records. Database methods can also throw exception similar to DML statements. So, it depends on that all or none parameter. What is exception? So, exception is basically runtime error. So, examples like DML statement exceptions. So, if a DML operation fails, it returns an exception of type DML exception. So, how you can control that? So, we can catch exceptions in our port to handle error condition. So, wherever you see like exceptions can be generated. So, that piece of code you can enclose in try block like this. So, we are trying to create account record and we are inserting it. So, here like on account creation, name field is required and we are not populating a value in it. So, when this code will execute, so it will fail. So, this piece of code, these two lines are enclosed in try and followed by catch block which is catching DML exception. So, here we are showing some debug statement like a DML exception has occurred and through e.getMessage, we are getting the error message. So, if this code breaks, your rest of the code will run finally and this piece of code will be handled. So, if you don't want to convert your whole code as a fault or failure. So, you can apply try and catch block in between the code. Now, what is parent to child SQL? So, this is also important for fresher as well as for experience candidate. So, it is a query which is applied on parent record which queries related child records as well. So, if you want to query parent and child records together so that SQL we can see like it is parent to child SQL. Example is, so we are trying to query accounts along with related context. So, select ID comma name comma and then we have inner query. Select ID comma first name from context and this context is a child relationship name. Then inner query closed here then from account and limit is five. So, this way parent to child SQL is applied. So, here context is a child relationship name which you can find on the lookup field or master detail field that is created on the child object. So, if relationship field is a custom field with which two objects are linked then we have to append underscore underscore r as well with the child relationship name. So, these things you need to remember if you want to answer this question. Next, we have child to parent SQL. So, in this query is applied on child object which queries parent details as well. So, example says like select ID first name. So, ID first name these fields are related to contact then account dot name. So, this contact is related to account. So, account is parent and contact is child. So, account dot name and maybe this contact is related to employee object as well. So, we are fetching employee underscore underscore r dot salary underscore underscore c. So, this is a custom relationship field that is why we are appending underscore underscore r and it is having a custom field. So, we are having underscore underscore c. So, here account is representing standard relationship whereas employee is representing representing custom relationship. So, in custom relationship append underscore underscore r. Now, what is debug look? So, a debug log can record operations system processes and errors that occur when executing a transaction or running unit tests. Debug logs can contain information about database changes, STTP call outs, Apex errors, resources used by Apex, automated workflow processes. So, if you want to store these information so you can use debug logs. So, this is all about like Apex questions and answers. If you want to learn these concepts in details so you can visit study salesforce.com there you will find all the detailed videos based on Apex programming language. So, I hope these questions will help you to clear your interview based on Salesforce.