Common Salesforce Interview Questions Series 1

                                                                    



#1 Triggers and Order of Execution

While save a record with an insertupdate, or upsert statement :

JavaScript validation if the record contains any dependent picklist fields

  1. Loads the original record from the database or initializes the record for an upsert statement.
  2. Loads the new record field values from the request and overwrites the old values.
  3. If the request came from a standard UI edit page, Salesforce runs system validation 
  4. Executes all before triggers
  5. Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules.
  6. Executes duplicate rules. If the duplicate rule identifies the record as a duplicate   the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
  7. Saves the record to the database, but doesn't commit
  8. Executes all after triggers
  9. Executes assignment rules >>  auto-response rules >> Executes processes >> workflow flow triggers, executes the flows
  10. Executes escalation rules
  11. Commits all DML operations to the database.
#2 Difference between LookUp and Master Detail Relationship


Loookup :     

                                                                                  Master

Loosely coupled                                                       Tightly coupled
Max number of lookup is 25                                      Max number of master is 2
Not a mandatory                                                        Mandatory
Once value is assigned can be changed                        Cannot be changed
Partent delete child cannot delete                                 parent delete child also delete

  cannot on lookup                                                        Roll up can be made on master-detail



#3 What is Approval Processing?

An approval process is an automated Business Process that your organization can use to approve records in Salesforce
An approval process specifies the:
– Steps necessary for a record to be approved
– Who must approve it at each step
– The actions to take when a record is approved, rejected, or first submitted for approval
#4 Relation between Account contact and opportunity



You can create a contact without filling account i.e it shows that there is a lookup relationship between 


account and contact.


If 


you have created a contact with account and you delete that account then contact will be deleted, this



 shows that it is in Master-Detail relationship. This is a standard behavior.




So we can say it in both ways, but it documentation it is a lookup relationship.








#5 Difference between Created and 
every time edited to meet the criteria and Created and 
edited to subsequently meet the criteria?

If we select 'Created and everytime edited to meet the criteria' whenever we create a record or edit a record if the criteria of the workflow rule meets then it will trigger every time. If we select 'Created and edited to subsequently meet the criteria' -


While creating the record criteria meets so that workflow will fire and while editing the record again criteria meets workflow won't fire (meeting the criteria to meeting the criteria)
  1. While creating the record criteria doesn't meet so workflow won't fire and while editing the record workflow criteria meets then workflow will fire (not meeting the criteria to meeting the criteria)
Conclusion: Previous state of record should be not meeting criteria and current state of record should be meeting the criteria then only in current state workflow will fire.




#6 Action Tag in Salesforce
Action tags-This tags support for calling action and refresh the field only not visualforce page.
  1. apex:actionFunction-
    Provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.
  1. apex:actionPoller
     A timer that sends an AJAX update request to the server according to a time interval that you specify. Update request can then result in a full or partial page update. You should avoid using this component with enhanced lists.
  1. apex:actionRegion
       This action tag is use AJAX request for particular field or component. Like we have change the value of pick list in visual force then request go to Server and come back through AJAX without Saving the VF page.
  1. apex:actionStatus
          A component that displays the status of an AJAX update request. An AJAX request can either be in progress or complete

  1. apex:actionSupport

     Adds AJAX support to another component, allowing the component to be refreshed asynchronously by server when a particular event occurs, such as a button click or mouse over

# Batch Class in Salesforce

Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be pro grammatically invoked at run time using Apex.

Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.

We have to create an global apex class which extends Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs

Examples:-

global class deleteOpp implements Database.Batchable
{
global final String Query;
global deleteOpp (String q)
{
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List scope)
{
List <Opp> lstOpp = new list<Opp>();
for(Sobject s : scope)
{
 Oppa = (Opp)s;
lstOpp .add(a);
}
Delete lstOpp ;
}

global void finish(Database.BatchableContext BC)
{
                //Send an email to the User after your batch completes
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {‘sforcesfdc@gmail.com’};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done‘);
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

//This is how the batch class is called.
id batchinstanceid = database.executeBatch(new deleteOpp(‘select Id from opp’))



Advantages of using Batch Apex?

  • Higher Governor Limits
  • Can be used to process in batches
  • Can be scheduled to run at different time. 

  • Work around to other governor limits e.g. Send More than 10 E-mails

  • Governor limit difference in using batch
    Area
    Normal Context
    Batch Context
    SOQL queries100 SOQL per cycle200 SOQL per cycle
    records retrieved by SOQL queries50,00050,000,000 (getQueryLocator)
    Heap size6 MB12 MB

    Batch Apex Governor Limits


    • Up to five queued or active batch jobs 
    •  User can have up to 50 query cursors open at a time. 
    • A maximum of 50 million records can be returned in the Database.QueryLocator object. 
    • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200
    • startexecute, and finish methods can implement up to 10 callouts
    • Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn’t cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running

    You could monitor the class by following below steps:
    To monitor or stop the execution of the batch Apex Batch job, go to Setup->Monitoring->Apex Jobs or Jobs-> Apex Jobs.

    Start:

    Syntax:
    global (Database.QueryLocator | Iterable) start(Database.BatchableContext bc) {}

    Execute:

    Syntax:
    global void execute(Database.BatchableContext BC, list<sobject<){}
    where , list<sObject< is returned by the Database.QueryLocator method.

    Finish:

    Syntax:
    global void finish(Database.BatchableContext BC){}

    Scheduling a batch apex class with in that batch class?



    global class AccountUpdateBatchJob implements Database.Batchable<sObject>
    {
        global Database.QueryLocator start(Database.BatchableContext BC)
        {
            String query = 'SELECT Id,Name FROM Account';
          
            return Database.getQueryLocator(query);
        }
        global void execute(Database.BatchableContext BC, List<Account> scope)
        {
            for(Account a : scope)
            {
                a.Name = a.Name + 'Updated by Batch job';
            }
            update scope;
        }
        global void finish(Database.BatchableContext BC) {
            OtherBatchJob obj = new OtherBatchJob();
            DataBase.executeBatch(obj);
        }

    }


    Calling on Batch Apex

                AccountJob obj = new AccountJob ();
                DataBase.executeBatch(obj);

    Scheduler Class For Batch Apex


    global class AccountUpdateBatchJobscheduled implements Schedulable 
    {
        global void execute(SchedulableContext sc) 
        {
            
    AccountJob b = new AccountJob (); 
            database.executebatch(b);
        }
    }
    How to Schedule scheduler class

    There are two option we have schedule the scheduler classes.
    1) By System Scheduler.
    2) By Developer console

    By Developer console 

    Execute below code from developer console :-

    AccountUpdateBatchJobscheduled m = new AccountUpdateBatchJobscheduled();
    String sch = '20 30 8 10 2 ?';
    String jobID = system.schedule('Merge Job', sch, m);



    What is Database.State full interface?
    To maintain variable value inside the Batch class, Database.Stateful is used.

    What is Database.AllowCallouts?

    To use a callout in batch Apex, you must specify Database.AllowsCallouts in the class definition. For example:
    global class SearchAndReplace implements Database.Batchable<sObject>,
       Database.AllowsCallouts{
                  //Business logic you want by implementing Batchable interface methods
    }
    Callouts include HTTP requests as well as methods defined with the webService keyword.

    How to use Database.Stateful in batch Apex?





    we want to add up the sum of all opportunities for each Account and store it in a field on Account called test__c 

    So we build a batch that receives all opportunities withn an Account and Amount, and keeps a running total for each account using an account map, and finally update the accounts in the final part of the batch:

    global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

       global final String query;
       global Map<Id, Account> accountmap;
      
       global SummarizeAccountTotal(){
       accountmap = new Map<Id, Account> ();
       }

       global Database.QueryLocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(query);
       }
       
       global void execute(Database.BatchableContext BC, List<sObject> scope){
       
       List<Opportunity> ops = (List<Opportunity>)scope;
       
       for (Opportunity o : ops) {
       
       if (accountmap.containskey(o.AccountId)) {
       Account a = accountmap.get(o.AccountId);
       a.test__c += o.Amount;
       accountmap.put(o.AccountId, a);
       }
       else{
       accountmap.put(o.AccountId, new Account (Id = o.AccountId, test__c = o.Amount));
       } 
             
       }
       }
       

    global void finish(Database.BatchableContext BC){
       
       try {
       update accountmap.values();
       }
       catch (Exception Ex) {
       system.debug(Ex);
       }
       
       }
    }











    Comments

    Popular posts from this blog

    All about workflow rule , limitation and important points with example

    Custom Setting in Salesforce

    Action tags in VisualForce Salesforce