Apex Interview Question – Salesforce – Part 16

151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ?
Ans : It can be done with help of External Id.

Date dt = Date.today().addDays(7);
Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting', CloseDate = dt);

/*
Create the parent reference. Used only for foreign key reference  and doesn't contain any other fields. If we provide any other value it will give following error

System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: []
*/

Account accountReference = new Account(MyExtID__c = 'SHIVA1234567');
newOpportunity.Account = accountReference;

//  Create the Account object to insert.  Same as above but has Name field.  Used for the insert.
Account parentAccount = new Account(Name = 'Shiva', MyExtID__c = 'SHIVA1234567');

Database.SaveResult[]
    results = Database.insert(new SObject[] {  parentAccount, newOpportunity });


152 . Which SOQL statement can be used to get all records even from recycle bin or Achieved Activities?
Ans : We will need “ALL Rows” clause of SOQL.
Sample :

SELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS

153. How can you lock record using SOQL so that it cannot be modified by other user.
Ans : we will need “FOR UPDATE” clause of SOQL.
Sample :

 Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

154. If you set more than one savepoint, then roll back to a savepoint that is not the last savepoint you generated, What will happen to later savepoint variables ?
Ans : if you generated savepoint SP1 first, savepoint SP2 after that, and then you rolled back to SP1, the variable SP2 would no longer be valid. You will receive a runtime error if you try to use it.


155. What are few limitations (points to remember) of Savepoint or Transaction Control in Apex ?
Ans :

  • Each savepoint you set counts against the governor limit for DML statements.
  • Static variables are not reverted during a rollback. If you try to run the trigger again, the static variables retain the values from the first run.
  • Each rollback counts against the governor limit for DML statements. You will receive a Runtime error if you try to rollback the database additional times.
  • The ID on an sObject inserted after setting a savepoint is not cleared after a rollback.

156. What are few Considerations about Trigger ?
Ans :

  • upsert triggers fire both before and after insert or before and after update triggers as appropriate.
  • merge triggers fire both before and after delete triggers for the losing records and before update triggers for the winning record only.
  • Triggers that execute after a record has been undeleted only work with specific objects.
  • Field history is not recorded until the end of a trigger. If you query field history in a trigger, you will not see any history for the current transaction.
  • You can only use the webService keyword in a trigger when it is in a method defined as asynchronous; that is, when the method is defined with the @future keyword.
  • A trigger invoked by an insert, delete, or update of a recurring event or recurring task results in a runtime error when the trigger is called in bulk from the Force.com API.
  • Merge trigger doesn’t fire there own trigger instead they fire delete and update of loosing and winning records respectively.

157. How to execute Apex from Custom button or Javascript ? Give Example.
Ans :

It is possible using Ajax toolkiit.

global class myClass {
	webService static Id makeContact (String lastName, Account a) {
		Contact c = new Contact(LastName = lastName, AccountId = a.Id);
		return c.id;
	}
}

we can execute above method from javascript like :

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
var account = sforce.sObject("Account");
var id = sforce.apex.execute("myClass" , "makeContact",
{lastName:"Smith", a:account});

To call a webService method with no parameters, use {} as the third parameter for sforce.apex.execute .

Also, you can use the following line to display a popup window with debugging information:
sforce.debug.trace=true;


158.  What is difference between public and global class in Apex ?
Ans :

  • Public class can be accessed within application or namespace. This is not exactly like public modifier in Java.
  • Global class visible everywhere , any application or namespace. WebService must be declared as Global and which can be accessed inside Javascript also. It is like public modifier in Java.

159. Explain Considerations for Static keyword in Apex.

Ans :

  • Apex classes cannot be static.
  • Static allowed only in outer class.
  • Static variables not transferred as a part of View State.
  • Static variables and static block runs in order in which they are written in class.
  • Static variables are static only in scope of request.

160. Explain few considerations for @Future annotation in Apex.

Ans :

  • Method must be static
  • Cannot return anything ( Only Void )
  • To test @future methods, you should use startTest and stopTest to make it synchromouse inside Test class.
  • Parameter to @future method can only be primitive or collection of primitive data type.
  • Cannot be used inside VF in Constructor, Set or Get methods.
  • @future method cannot call other @future method.

Posted

in

,

by


Related Posts

Comments

8 responses to “Apex Interview Question – Salesforce – Part 16”

  1. Naved Avatar
    Naved

    Hello Jitendra,

    Can we stop creating an instance while their is already an instanceis running of apex class?

    Thanks & Regards
    Naved

    1. JitendraZaa Avatar
      JitendraZaa

      We can follow Singleton Design Pattern to achieve this.

  2. Dilip Avatar
    Dilip

    Hello,
    I am fresher and i am learning salesforce right now.Will companies consider freshers in this field.(In india)

    1. vikky Avatar
      vikky

      Apparenly no, but you can try in small companies

  3. Suresh Avatar
    Suresh

    Hi
    Can anyone send me the Latest salesforce 401 dumps to my mail-id sureshkumar58094@gmail.com Am taking the exam this september

  4. Munis Avatar
    Munis

    Thanks for the very comprehensive technical questions and answers . Good stuff , helped me revise SF stuff before an interview

  5. Pratyush Kumar Avatar
    Pratyush Kumar

    Jitendra sir,

    We are using ExternalId, why?

    Date dt = Date.today().addDays(7);
    Opportunity newOpportunity = new Opportunity(Name = ‘shivasoft’, StageName = ‘Prospecting’, CloseDate = dt);

    /*
    Create the parent reference. Used only for foreign key reference and doesn’t contain any other fields. If we provide any other value it will give following error

    System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: []
    */

    Account accountReference = new Account(MyExtID__c = ‘SHIVA1234567’);
    newOpportunity.Account = accountReference;

    // Create the Account object to insert. Same as above but has Name field. Used for the insert.
    Account parentAccount = new Account(Name = ‘Shiva’, MyExtID__c = ‘SHIVA1234567’);

    Database.SaveResult[]
    results = Database.insert(new SObject[] { parentAccount, newOpportunity });

  6. swathi Avatar
    swathi

    HI
    I am fresher, I want to get project soon.so how much level I should known about salesforce like apex and all.

    Regards,
    swathi

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading