Thursday, November 26, 2015

Interview questions on triggers In salesforce

1.What is trigger ?
Ans: Trigger is piece of code that is executes before and after a record is Inserted/Updated/Deleted from the force.com database.

2.What are different types of triggers in sfdc?
Ans: 1.Before Triggers-These triggers are fired before the data is saved into the database.
2.After Triggers-These triggers are fired after the data is saved into the database.

3.What are trigger context variables?
Ans:
Trigger.isInsert: Returns true if the trigger was fired due to insert operation.
Trigger.isUpdate: Returns true if the trigger was fired due to update operation.
Trigger.isDelete: Returns true if the trigger was fired due to delete operation.
Trigger.isBefore: Returns true if the trigger was fired before record is saved.
Trigger.isAfter: Returns true if the trigger was fired after record is saved.
Trigger.New: Returns a list of new version of sObject records.
Trigger.Old: Returns a list of old version of sObject records.
Trigger.NewMap: Returns a map of new version of sObject records. (map is stored in the form of map)
Trigger.OldMap: Returns a map of old version of sObject records. (map is stored in the form of map)
Trigger.Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
Trigger.isExecuting: Returns true if the current apex code is a trigger.


4.What is the difference between Trigger.New and Trigger.NewMap?

Ans:
Trigger.New is Returns a list of new version of sObject records but Trigger.NewMap is Returns a map of new version of sObject records.

5.What is the difference between Trigger.New and Trigger.Old?

Ans:
Trigger.New is Returns a list of new version of sObject records and Trigger.Old is Returns a list of old version of sObject records.

6.What is the difference between Trigger.New and Trigger.Old in update triggers?

Ans:

7.Can we call batch apex from the Trigger?

Ans: A batch apex can be called from a class as well as from trigger code.
In your Trigger code something like below :-
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);


8.What are the problems you have encountered when calling batch apex from the trigger?
Ans:

9.Can we call the callouts from trigger?

Ans: yes we can. It is same as usual class method calling from trigger. The only difference being the method should always be asynchronous with @future

10.What are the problems you have encountered when calling apex the callouts in trigger?

Ans:

11.What is the recursive trigger?

Ans: Recursion occurs in trigger if your trigger has a same DML statement and the same dml condition is used in trigger firing condition on the same object(on which trigger has been written)

12.What is the bulkifying triggers?

Ans: By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.

13.What is the use of future methods in triggers?

Ans: Using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.

14.What is the order of executing the trigger apex?

Ans:
1. Executes all before triggers.
2. Validation rules.
3. Executes all after triggers.
4. Executes assignment rules.
5. Executes auto-response rules.
6. Executes workflow rules.
7. If there are workflow field updates, updates the record again.
8. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.
9. Executes escalation rules.
10. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
11. If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
12. Executes Criteria Based Sharing evaluation.
13. Commits all DML operations to the database.
14. Executes post-commit logic. Ex: Sending email.


15.
What is the trigger handler?
Ans:

16.How do we avoid recursive triggers?

Ans: Use a static variable in an Apex class to avoid an infinite loop. Static variables are local to the context of a Web request.

17.
How many triggers we can define on a object?
Ans: We can write more than one trigger But it is not recommended .Best practice is One trigger On One object. 

18.Can we define triggers with same event on single object?

Ans:

19.Tell me more scenarios where you have written triggers?

Ans:

20.What is the best scenario that you have developed on triggers?

Ans:

21.How many time workflow filed update will be called in triggers?

Ans:

Wednesday, November 25, 2015

How to delete all records from custom object in salesforce

Go to Developer Console>Debug> Open execute anonymous window

Write the below code

List<sobject__c> objDelList= [SELECT Id FROM sobject__c];
delete objDelList;

Click on the button execute button

Note: Replace  sobject__c with original custom object API name 

How to do Avalara tax API Integration With Salesforce?

Special thanks to Ashish Agarwal

Step 1: go to below link and fill the form to get your token and secret

               http://developer.avalara.com/api-get-started/

after submitting the form you will receive an email to your email address. Go there and click on the developer area link provided in email. In this email you will get your login ID and password also.

On developer page you will prompted to give your Id and password . Please fill provide them and login.

Now reset your new password and save.

Step 2: Now go to setting tab and click on Reset Licence Key and then click on generate button.

Step 3: Now check your email .you will find an email from avalara support .open it and get your account number and Licence Key.

Step 4: Setup Avatax Select the Country and set Jurisdictions see below image


Step 5: Now go to SETUP | ADMINISTER | SECURITY CONTROLS | REMOTE SITE SETTINGS and create a new Remote site.

Give remote site name avalara and Remote site url as “https://development.avalara.net “ .

Step 6 : Now create a apex class namely “avalaraController” and copy the code from below link and paste it .

Apex Class:
public class avalaraController
 {

//string variable to show api result
    public string result{get;set;}
//string variable to hold amount of line item
    public String amount { get; set; }
//string varriable to hold quantity of line item
    public String count { get; set; }
//string varriable to hold postal code of address
    public String PostalCode { get; set; }
//line 1 of address
    public String Line1 { get; set; }
//line 2 of address
    public String Line2 { get; set; }

//method to get tex value from avalara tex api    
public String RestRequest(String body,String endPoint,String method){
        
         Blob headerValue = Blob.valueOf("acount no"+':'+"Licence key");
         String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
                
         //accessing end point for avalara request type
        
         Http h=new Http();
         HttpRequest req = new HttpRequest();
         req.setMethod(method);
  
         req.setHeader('Authorization', authorizationHeader);
         req.setEndPoint(endPoint);
         req.setBody(body);
         HttpResponse res = h.send(req);
         result = res.getBody();
         return result;



}

//method to get Text
       public void getTex(){
//calling method to create json body for request body from user provided data
        
         String texString = createTexRequestBody();
         RestRequest(texString ,"https://development.avalara.net/1.0/tax/get" ,'POST');
               }
  //method to validate address             
      public void validateAddress() {
     
        endpoint = 'https://development.avalara.net/1.0/address/validate?Line1='+EncodingUtil.urlEncode(Line1+Line2,'UTF-8')+'&PostalCode='+EncodingUtil.urlEncode(PostalCode,'UTF-8');
        RestRequest('' ,endPoint ,'GET');
    }
 //method to create json string for rest request body        
      public String createTexRequestBody(){
      
      
       JSONGenerator gen= JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeStringField('DocDate', '2013-06-19');
        gen.writeStringField('CustomerCode', 'CUST1');
        gen.writeFieldName('Addresses');
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('AddressCode', '1');
        gen.writeStringField('Line1',Line1 );
        gen.writeStringField('Line2', Line2 );
        
        gen.writeStringField('PostalCode',PostalCode);
        gen.writeEndObject();
        gen.writeEndArray();
        gen.writeFieldName('Lines');
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('LineNo', '1');
        gen.writeStringField('DestinationCode', '1');
        gen.writeStringField('OriginCode', '1');
        gen.writeNumberField('Qty',Integer.valueOf(count));
        gen.writeNumberField('Amount',Integer.valueOf(amount));
        
        gen.writeEndObject();
        gen.writeEndArray();
        gen.writeEndObject();
        return gen.getAsString();
      
      
      }

}

Now in header section of the code replace account Number with your account no and licence key with your licence key provided in email and DocDate should be the date given in the AVATAX Starting date.

Step 7: Now create a visualforce page and give it name as “ avalara” and copy the code from following link and paste it in page
Visualforce Page:

    
        
 
                
                    HP Printer
                

                    Quatity
                
                
                    amount
                
                        
                            
                
                    Address Line1 
                       
                

                    Address  Line2  
                    
                

                    PostalCode 
                    
                
{!tax}
            
            
            
                
                    
Loading...
{!result}


Run your visualforce page and fill the form with quantity ,amount(price) and any right address with valid postal code(required) ( only america and canada address).

And click on get Tex.

And you will find the response.

Check the response string and you will find tax for amount you entered.


Output: