Sunday 18 December 2016

Salesforce Advanced Interview Questions

           Salesforce Advanced Interview Questions 


1.write sample trigger to update Account phone on its related contacts.


TriggerName: AccountHandlerTrigger


trigger AccountTriggerHandler on Account (after update) {

AccountHandlerClass.updateContacts2(Trigger.newMap);
}



/*******************************************************************
Class Name: AccountHandlerClass

Purpose:    1. This is the Trigger Handler class for Account object




public class AccountHandlerClass {

/*******  Sample method 1 *********/

 public static void updateContacts2(map<id,account> newAccountMap){
   
list<contact> lstCons =[select id,name,phone,Accountid from contact where Accountid in :newAccountMap.keySet()];
system.debug('lstCons :'+lstCons);
list<contact> updCons = new list<contact>();
if(!lstCons.isEmpty()){
for(contact c : lstCons){
if(c.Phone != newAccountMap.get(c.AccountId).phone){
c.Phone = newAccountMap.get(c.AccountId).phone;
updCons.add(c);
}
}
}
   update updCons;
}

}


2.write Soql query to Accounts with contact. 

list<account> acc = [select Id,name from Account where Id In (select AccountId from Contact)];

system.debug('acc : '+acc.size());



3.if a controller has a series of extensions then which extension will execute first.




If both extension having same method, whichever methods are defined in the “leftmost” extension, or, the extension that is first in the comma-separated list will execute first.


====================================================================
<apex:page standardController="Account" extensions="Extension1,Extension2">
    <apex:form>
        Displaying name from Extensions : "{!Name}"
    
    </apex:form>
</apex:page>
====================================================================
public class Extension1 {    
    public Extension1(ApexPages.StandardController controller){}
    public static string getName(){      
        return 'Prasad';
    }

}



public class Extension2 {      
    public Extension2(ApexPages.StandardController controller){}
  
    public static string getName(){       
        return 'Kalyan';
    }
    
}


=======================================================================
4.Transaction Control using SavePoint and Rollback in Salesforce.

SavePoint and Rollback will help us to maintain transaction for DML statement.
Suppose you  have written multiple lines of DML statements in a try block, If any error occurs during DML Operations, the operation will be rolled back to the most recent save point and the entire transaction will not be aborted.

   Savepoint sp;
try{
   sp = Database.setSavepoint();
  
   Account a = new Account();
   a.Name = 'Test Account';
   insert a;
  
   Contact c = new Contact(Account = a.Id);
   c.FirstName = 'Biswajeet';
   c.LastName = 'Samal';
   insert c;
  }
catch(Exception e){
    Database.RollBack(sp);
}

More Info : 


5.can we do a Callout from with in a Trigger ?



6.How to get Custom Settings in Apex Test code?


@isTest
public class BIIB_testClassHelper{

/*
 * Creating custom settings data in creation in test class .
 *  Avoid @isTest(SeeAllData = True) in test classes..
 */

public static testMethod void customsettingcreation(){
  
Id SampleRecordtypeID=Schema.SObjectType.Case.getRecordtypeinfosbyname().get('Recordtype Name').getRecordtypeId();
string Country = 'USA';

Custom_Settings__c cusutomsetting = new Custom_Settings__c();
cusutomsetting.RecordType_ID__c=SampleRecordtypeID;
cusutomsetting.CountryName__c=Country;

insert cusutomsetting;

   }
}



8.Trigger to update All contact names in Account Field.

===================================================================
trigger ContactTrigger_Handler on Contact (after insert,after delete) {
    
    if(trigger.isinsert && trigger.isAfter){
        updaetcontactnamefield.updateaccount(Trigger.new);
    }
    
     if(trigger.isdelete && trigger.isAfter){
        updaetcontactnamefield.updateaccount(Trigger.old);
    }

}

====================================================================
public class updaetcontactnamefield{
    public static void updateaccount(list<contact> contacts){
        set<id> accountIds = new set<id>();
        
        if(!contacts.isEmpty()){
            for(contact c:contacts){
                accountIds.add(c.accountid);
            }
        }
        
        list<account> lstAcc = new list<account>();
        for(Account acc : [Select id, name, description, 
                           (Select Id, name, LastName From Contacts) 
                           From Account Where Id In : accountIds])
        {
            List<String> lstSrting = new List<String>();
            for(Contact con : acc.contacts)
            {
                lstSrting.add(con.lastname);
            }
            acc.description = String.join(lstSrting, ',');
            lstAcc.add(acc);
        }
      update lstAcc;
    }
    
}

====================================================================

9.How to setup Field Level Security (FLS) for Person Account Fields.

OR
Why I am not able to find list of Person Account fields in Field Level Security (FLS) settings when navigated to fields on Account Object.
Ans :


Field Level Security (FLS) of Person Account fields ar controlled by Contact Fields. So, if you want to setup FLS of Person Account Fields navigate to fields of Contact and it will be reflected on Person Account.

10.In below code snippet , What is your observation and what is going wrong ?


trigger TestBeforeDelete on Lead (before Delete) {

for(Lead l : Trigger.Old)
{
l.addError('error');
}

String msgBody = 'Test Email';
String Subject = 'Test from Cogni Force on Lead';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'abc@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('abc@gmail.com');
mail.setSenderDisplayName('Cogniforce Test Simulator');
mail.setSubject(Subject);
mail.setPlainTextBody(msgBody);
mail.setHTMLBody(msgBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Ans :
It will not send any email. Because “adderror” prevents all transactions from committing including emails.

10.How To Start Loading Data From 101 Row Of The CSV File In Data Loader?


In Dataloader Settings we need to specify Start Row no:

11.Calling future method from batch class?


Future method cant be called from future or Batch.


recommend the use of System.Queueable call instead of @Future. You can create a class that is constructed with any kind of argument that you want to hand in, like so:
public class DeferredHandler implements System.Queueable{
    public void execute(System.QueueableContext objContext)    {
        //do your @Future stuff here 
    }
}
And invoke him from inside your batch like this:
//defer stuff
System.Queueable job = new DeferredHandler(key2val);
System.enqueueJob(job);


12.How we can display serial-nos in standardlistcontroller.

<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
  <apex:pageBlock >
      <apex:variable var="rowcount" value="{!0}" />      
    <apex:pageBlockTable value="{!accounts}" var="a" >
       <apex:column>
        {!rowcount} <apex:variable var="rowcount" value="{!rowcount+1}" />
    </apex:column>
      <apex:column value="{!a.name}"/>
      <apex:column value="{!a.phone}"/>
    </apex:pageBlockTable>
  </apex:pageBlock>

</apex:page>

No comments:

Post a Comment

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...