Pages

Tuesday, March 7, 2017

Apex Basics & Database "Get Started with Apex"

Create an Apex class that returns an array (or list) of strings.

Create an Apex class that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.
  • The Apex class must be called 'StringArrayTest' and be in the public scope.
  • The Apex class must have a public static method called 'generateStringArray'.
  • The 'generateStringArray' method must return an array (or list) of strings. Each string must have a value in the format 'Test n' where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
Solution:

StringArrayTest Class:

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n){
        List<String> myArray = new List<String>();
        for(integer i = 0; i < n; i++){
            myArray.add('Test '+i);
        }
        return myArray;
    }

}

Note: Please be careful with the space in 'Test '+i when you add into the List.

Sunday, January 15, 2017

Asynchronous Apex "Using Batch Apex"

Create an Apex class that uses Batch Apex to update Lead records.

Create an Apex class that implements the Database.Batchable interface to update all Lead records in the org with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class.
  • Create an Apex class called 'LeadProcessor' that uses the Database.Batchable interface.
  • Use a QueryLocator in the start method to collect all Lead records in the org.
  • The execute method must update all Lead records in the org with the LeadSource value of 'Dreamforce'.
  • Create an Apex test class called 'LeadProcessorTest'.
  • In the test class, insert 200 Lead records, execute the 'LeadProcessor' Batch class and test that all Lead records were updated correctly.
  • The unit tests must cover all lines of code included in the LeadProcessor class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
LeadProcessor Class:

global class LeadProcessor implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([SELECT LeadSource from Lead]);
    }
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead Leads : scope){
            Leads.LeadSource = 'Dreamforce';
        }
        update scope;
    }
    
    global void finish(Database.BatchableContext bc){
        
    }
}

LeadProcessorTest Class:

@isTest
public class LeadProcessorTest {
    static testMethod void leadProcessorTrailTest(){
        List<Lead> leadList = new List<Lead>();
        for(integer i = 0; i<200; i++){
            Lead leads = new Lead();
            leads.FirstName = 'Ankit';
            leads.LastName = 'Avula'+i;
            leads.Company = 'ARG';
            leadList.add(leads);           
        }
        insert leadList;
        Test.startTest();
        LeadProcessor L = new LeadProcessor();
        Database.executeBatch(L);
        Test.stopTest();
    }

}

Thursday, January 12, 2017

Asynchronous Apex "Using Future Methods"

Create an Apex class that uses the @future annotation to update Account records.

Create an Apex class with a method using the @future annotation that accepts a List of Account IDs and updates a custom field on the Account object with the number of contacts associated to the Account. Write unit tests that achieve 100% code coverage for the class.
  • Create a field on the Account object called 'Number_of_Contacts__c' of type Number. This field will hold the total number of Contacts for the Account.
  • Create an Apex class called 'AccountProcessor' that contains a 'countContacts' method that accepts a List of Account IDs. This method must use the @future annotation.
  • For each Account ID passed to the method, count the number of Contact records associated to it and update the 'Number_of_Contacts__c' field with this value.
  • Create an Apex test class called 'AccountProcessorTest'.
  • The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
AccountProcessor class:

public class AccountProcessor {
    @future
    public static void countContacts(List<ID> accIds){
        List<Account> listacc = [SELECT id,Number_of_Contacts__c,(SELECT id from contacts) from account where id in : accIds];
        for (account acc : listacc){
            List<Contact> con = acc.contacts;
            acc.Number_of_Contacts__c = con.size();
        }
        update listacc;
    }

}

AccountProcessTest class:

@isTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest(){
     Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
        system.debug('account a :'+a.id);
        
        Contact cont = New Contact();
        cont.FirstName ='Ankit';
        cont.LastName ='Avula';
        cont.AccountId = a.Id;
        Insert cont;
        system.debug('contact cont :' +cont.id);
        
        List<Id> accIds = new List<Id>();
        accIds.add(a.Id);
        
        Test.startTest();
        AccountProcessor.countContacts(accIds);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals(ACC.Number_of_Contacts__c, 1);
    }
}

Apex Integration Services "Apex Web Services"

Create an Apex REST service that returns an account and it's contacts.

To pass this challenge, create an Apex REST class that is accessible at '/Accounts/<Account_ID>/contacts'. The service will return the account's ID and Name plus the ID and Name of all contacts associated with the account. Write unit tests that achieve 100% code coverage for the class and run your Apex tests.
  • The Apex class must be called 'AccountManager'.
  • The Apex class must have a method called 'getAccount' that is annotated with @HttpGet
  • The method must return the ID and Name for the requested record and all associated contacts with their ID and Name.
  • The unit tests must be in a separate Apex class called 'AccountManagerTest'.
  • The unit tests must cover all lines of code included in the AccountManager class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
AccountManager Class:


@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount(){
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substringBetween('Accounts/','/contacts');
        Account Result = [SELECT Id, Name, (SELECT Id, Name From Contacts) From Account Where Id =: accountId];
        system.debug('Result is :' +Result);
        Return Result;
    }
}

AccountManagerTest Class:

@isTest
private class AccountManagerTest {
    private static testMethod void testGetAccount(){
        Id recordId = createTestRecord();
        RestRequest request = new RestRequest();
        request.requestUri = 'https://na35.salesforce.com/services/apexrest/Accounts/'+recordId+'/Contacts';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account thisAccount = AccountManager.getAccount();
        System.assert(thisAccount != null);
        System.assertEquals('Test record', thisAccount.Name);
    }
    static Id createTestRecord(){
         Account TestAcc = new Account(
          Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
        LastName='Test', 
        AccountId = TestAcc.id);
        return TestAcc.Id;
    }
}

Thursday, January 5, 2017

Apex Integration Services "Apex SOAP Callouts"

Generate an Apex class using WSDL2Apex and write a test class.

Generate an Apex class using WSDL2Apex for a SOAP web service, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.
  • Use WSDL2Apex to generate a class called 'ParkService' in public scope using this WSDL file. After you click the 'Parse WSDL' button don't forget to change the name of the Apex Class Name from 'parksServices' to 'ParkService'.
  • Create a class called 'ParkLocator' that has a 'country' method that uses the 'ParkService' class and returns an array of available park names for a particular country passed to the web service. Possible country names that can be passed to the web service include Germany, India, Japan and United States.
  • Create a test class named ParkLocatorTest that uses a mock class called ParkServiceMock to mock the callout response.
  • The unit tests must cover all lines of code included in the ParkLocator class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
ParkService Class generated from WSDL:

//Generated by wsdl2apex

public class ParkService {
    public class byCountryResponse {
        public String[] return_x;
        private String[] return_x_type_info = new String[]{'return','http://parks.services/',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class byCountry {
        public String arg0;
        private String[] arg0_type_info = new String[]{'arg0','http://parks.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0'};
    }
    public class ParksImplPort {
        public String endpoint_x = 'https://th-apex-soap-service.herokuapp.com/service/parks';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://parks.services/', 'ParkService'};
        public String[] byCountry(String arg0) {
            ParkService.byCountry request_x = new ParkService.byCountry();
            request_x.arg0 = arg0;
            ParkService.byCountryResponse response_x;
            Map<String, ParkService.byCountryResponse> response_map_x = new Map<String, ParkService.byCountryResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://parks.services/',
              'byCountry',
              'http://parks.services/',
              'byCountryResponse',
              'ParkService.byCountryResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}

ParkLocator Class:

public class ParkLocator { public static String[] country(String country){ ParkService.ParksImplPort Locator = new ParkService.ParksImplPort(); return Locator.byCountry(country); } }

ParkServiceMock class:

@isTest global class ParkServiceMock implements WebServiceMock{ global void doInvoke( Object stub, Object request, Map<String,Object> response, String endpoint, String soapAction, String requestName, String responseNS, String responseName, String responseType) { ParkService.byCountryResponse response_x = new ParkService.byCountryResponse(); response_x.return_x = new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}; response.put('response_x',response_x); } }

ParkLocatorTest class:

@isTest private class ParkLocatorTest { testMethod static void testCallout(){ Test.setMock(WebServiceMock.class, new ParkServiceMock()); String country = 'United States'; String[] result = ParkLocator.country(country); System.assertEquals(new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}, result); } }