Friday 27 January 2017

Test Utility Classes | TestDataFactory | Util Test in salesforce


Common test utility classes are public test classes that contain reusable code for test data creation.The TestDataFactory/ testutility  class is a special type of class — It is a public class that is annotated with @isTest and and as such, are excluded from the organization code size limit and execute in test context and can be accessed only from a running test.

Test utility classes/TestDataFactory contain methods that can be called by test methods to perform useful tasks, such as setting up test data.


Step 1:- Create TestDataFactory / Test utility
@isTest
public with sharing class TestDataFactory 
{
    /** 
    * ********************************************************
    * This method is test data for create Lead
    * ********************************************************
    */

    public static Lead createLead(Boolean doInsert)
    {
        Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
        if(doInsert){
            insert newLead;
        }
        return newLead;
    }

    public static Void convertLead(Lead newLead )
    {
        database.leadConvert lc = new database.leadConvert();
        lc.setLeadId(newLead.id);
        leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        lc.setOpportunityName('Cole Swain');
        
    }
        
    /** 
    * ******************************************************
    * This method is test data for create Account
    * ******************************************************
    */
    
    public static Account createAccount(Boolean doInsert)
    {
        Account acc = new Account();
        acc.Name = 'Test Account';
        if(doInsert){
            insert acc;
        }
        return acc;
    }
       
     /**
     * *******************************************************
     * This method is test data for create contact object
     * *******************************************************
     */
    public static Contact createContact(Boolean doInsert)
    {
        return createContact(doInsert, createAccount(true).Id);
    }
    
    public static Contact createContact(Boolean doInsert, Id accId)
    {
        Contact con = new Contact();
        con.AccountId = accId;
        con.FirstName = 'FirstName';
        con.LastName = 'LastName';
        con.Email = 'FirstName@test.com' + Math.floor(Math.random() * 1000);
        if(doInsert)
        {
            insert con;
        }
        return con;
    }

    /**
    * ***********************************************************
    * This method is test data for create Opportunity object
    * ***********************************************************
    */
    
    public static Opportunity createOpportunity(Boolean doInsert, Id accId)
    {
        Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = accId,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today()
                            );
        if(doInsert)
        {
            insert oppt;
        }
        return oppt;
    }   
    
    /**
    * ************************************************************
    * This method is test data for create Case object
    * ************************************************************
    */
        
    public static Case  createCase(Boolean doInsert )
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        if(doInsert)
        {
            insert cas ;
        }
        return cas ;
    }    
    
}



Step 2:- How to Use TestDataFactory / Test utility

@isTest
private class MyTestClass 
{
    static testmethod void myUnitTest() 
    {
        Lead leadObj = TestDataFactory.createLead(true);
        Account accObj = TestDataFactory.createAccount(true);
        Contact contObj = TestDataFactory.createContact(true,accObj.id);
        Opportunity oppObj = TestDataFactory.createOpportunity(true,accObj.id);
        Case caseObj = TestDataFactory.createCase(true);
    }
    // If you want to edit data according to apex class then try like below
    static testmethod void myUnitTest1() 
    {
        Lead leadObj = TestDataFactory.createLead(false); // pass false
        leadObj.LastName ='MyName';
        insert leadObj ;
        
        Account accObj = TestDataFactory.createAccount(false);
        accObj.Name ='MyName';
        insert accObj;
        
    }
    
}


Please check below post for Test Class Sample :-



Some Useful link :-

1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm
2) https://trailhead.salesforce.com/en/apex_testing/apex_testing_data?id=apex_testing
3) https://github.com/dhoechst/Salesforce-Test-Factory


Thanks
Amit Chaudhary
@amit_sfdc

Wednesday 11 January 2017

Learn Rest API in salesforce | How to learn Rest API | Rest API in salesforce


REST API
The Salesforce REST API lets you integrate with Salesforce applications using simple HTTP methods, in either JSON or XML formats, making this an ideal API for developing mobile applications or external clients. Salesforce also supports Apex REST, which lets you create Web services on Force.com using Apex


Please check all below post to learn about REST API with example.


1) Apex Integration Services
Now learning in salesforce to much easy as compare to our time. The Salesforce Trailhead now offers a fun way to learn everything related to Salesforce Integration and Rest API

2) Insert | Update | Delete record by Rest API | Workbench | Standard Rest API | Rest Explorer 
In Above post you will learn how to insert, update or delete a record by Rest API without any code 



3) REST Explorer | Using Workbench | Execute API from Workbench 
The best way to test your web-service is workbench. The browser based workbench does not require OAuth as long as your browser session is active. Above post will help you to learn about Workbench. 


4) Access Token Generation | OAuth 2.0 | Connected App | Rest API Access Token 
This post will help how to Generate OAuth token to execute the Rest API 



5) Rest API in Salesforce | Execute Rest API on workbench | Test class for Rest API 
 Above post will help you How to create Rest API code in Apex classes with Test class. In above you can found the sample code and can see how to execute same API in workbench.




 6) REST API | New Resources | Summer 15 | Composite Resources in Salesforce 
In this post we will learn about Batch resource. The Batch resource lets you execute a sequence of independent subrequests. For example, you can update the name on an account and get the account’s field values in a single request



7) Test Salesforce API by Postman Rest Client | Postman and Salesforce | Calling APEX Rest service using Postman| OAuth 2.0

Please check below for how to call REST API from postman and how to do salesforce API end to end testing



8) REST API with XML and JSON | REST API with XML Payload | REST API with Dynamic Response

 REST API which should support JSON and XML both. If we will pass JSON request body then response should come in JSON. But if we will send request body in XML then response should come in XML.

9) Callouts from a Visualforce Page | Continuation | Asynchronous callouts from VF page

How to do Callout from VF page button



Thanks
Amit Chaudhary

Saturday 7 January 2017

Salesforce Apex Hours :- Year End Meetup




 
Farmington Hill Salesforce Developer group organized another successful event on 24th Dec 2016, focusing on How to learn salesforce this time for new user in group.


Agenda :-
• Sharing Knowledge about salesforce,New features etc
• Trailhead badges
• Games & Swag !



Special Thanks to Regina for awesome swag and Salesforce Book :-
1) Force.com Fundamentals
2) Lightning Experience Guidebook
3) Lightning Components Developer Guide 





Facebook Page :- https://www.facebook.com/FarmingtonHillsSfdcdug/
Meetup Link :- http://www.meetup.com/Farmington-Hills-Salesforce-Developer-Meetup/
Twitter Tag :- #FarmingtonHillsSFDCdug


Some Pic of our First Meetup

First Meetup Place :- Starbucks  33199 Grand River Av, Farmington, MI, USA.












Thanks
Amit Chaudhary
@amit_sfdc


Sunday 1 January 2017

Success Story of : - Shivanath Devinarayanan


Welcome to my 4th Salesforce Success Story series. This series is focused on success story and to inspire/encourage new user/Developer. And why we should join our local Salesforce Developer/User group.






1) Your Job Title

CEO/Technical Architect at Dazeworks Technologies Pvt Ltd

2) Success Story

Shivanath:- Ever since my first job in 2010, I fell in love with Salesforce and in spite of less relevance of the platform at that time, I took the risk to invest my career in it. Later on in 2013, I shifted to Gurgaon as Lead Associate at Simplion Technologies to learn more about Salesforce. Being a company that focuses solely on Salesforce, Simplion was just what I needed to realize that there was so much more to the platform. I also started getting involved with Gurgaon Salesforce User Group and organizing events.

I owe a lot of my ‘expert’ status to the Salesforce community as well as the detailed workbooks on Force.com that were available then. These paved the way for expanding my knowledge on the platform and have come to my rescue at the toughest of times. My interaction with the Salesforce community made me realize the huge potential that the platform holds for developers in India. This realization led me to kick-start the Kerala Salesforce Developer User Group, a community that educates like-minded individuals interested in Salesforce and the cloud.

My next role was at Groupon, a top player in the Salesforce community and this was a crucial step in my career. The company being located in Chennai helped me get closer to the Salesforce community in the city. I started the  Chennai Developer User Group as well, another great community of Salesforce lovers. By then, I had also become the proud holder of the elite Salesforce.com and Force.com MVP titles. This honor further exposed me to a much wider Salesforce community – one that consisted of people I had always looked up to.

Events like Dreamforce brought me closer to the community and the platform.
Meeting up with a whole lot of dynamic and inspiring Salesforce MVPs and successful business heads slowly gave wings to my own long-term dream of becoming an entrepreneur. I began to seriously consider the prospect and thus was born Dazeworks – a pure-play Salesforce consulting and development firm based out of India with expert teams also in the  US, Europe, Middle East Asia, South Asia and Africa. Our team consists of certified developers, architects and consultants including awesome MVPs like Deepak Anand and Vinay Chaturvedi. ( Amit:- Shivanath is role model for all NCR Salesforce Developer group )


 3) Why we should join a Salesforce User/Developer Group

Shivanath:- Joining a user or dev group can provide you with some amazing resources. You get to learn new stuff, bring out innovative ideas and the best part - networking! You meet a lot of amazing people from the community with whom you can exchange information and widen your knowledge. What you learn, you will be able to give back to the community too. You get to interact with so many other brains that do the same thing as you with the same level of passion. ( Amit:- I agree i also meet to many awesome person in Meetup only )

4) Advice for new Salesforce Developer

Take the Trail: If you are a newbie, the best way to wade through Salesforce is with Trailhead. Learn the INs and OUTs of the Salesforce platform with Trailhead. Even if you’ve been around Salesforce for some time, Trailhead is the recommended way forward because some of those badges are REALLY tough to win.

Join a nearby User group: Find a user group near you and start attending meetings. Learn all you can, share your knowledge, your passion and your challenges.


5) Trailhead badges: 

8 - Not my best score! I really should try and join a Race for Trailhead to keep up with the numbers :D


Follow Shivanath Devinarayanan on:
Twitter : 
@shivanathd


 If you want to share your Salesforce Success Story, Please feel free to drop me an email :- amit.salesforce21@gmail.com
 

<<PREVIOUS       NEXT>>


Thanks
Amit Chaudhary
@amit_sfdc