You need to sign in to do that
Don't have an account?
Mhlangano Khumalo
Compile Error in HTTP Patch Test Class. 'Variable does not exist: bacctList at line 16 column 46'
Please assist with a compile error in my test class:
If I remove the variable 'bacctList'
Full Class:
If I remove the variable 'bacctList'
Full Class:
/***************************************************************************************************Json String: { "bacctList" : [{ "A_c_Number__c" : "323838492", "Bank_code__c" : "198765" }, { "A_c_Number__c" : "198765", "Bank_code__c" : "250655" }] } * ****************************************************************************************************/ @RestResource(urlMapping='/dosystem/bankaccounts/*') global with sharing class BankAccountWebservice{ @HttpPatch global static BankAccountWrapper doPatch(List<Bank_Account__c> bacctList) { RestRequest req = RestContext.request; RestResponse res = RestContext.response; BankAccountWrapper response = new BankAccountWrapper(); List<Bank_Account__c> bankAccountsToUpdate= new List<Bank_Account__c>(); for(integer i=0; i<bacctList.size(); i++){ List<Bank_Account__c> ac = [select Name,Debit_Order_A_c__c,A_c_Number__c,Bank_code__c from Bank_Account__c where A_c_Number__c =: bacctList[i].A_c_Number__c AND Bank_code__c =: bacctList[i].Bank_code__c]; for(integer j=0; j< ac.size(); j++){ ac[j].Debit_Order_A_c__c = false; bankAccountsToUpdate.add(ac[j]); } } try { update bankAccountsToUpdate; response.bacctList = bankAccountsToUpdate; response.status = 'Success'; response.message = bacctList.size()+' Records Updated successfully'; } catch(Exception exc) { res.StatusCode = 500; response.bacctList = null; response.status = 'Error'; response.message = 'Your request failed with the following error: ' + exc.getMessage(); } return response; } global class BankAccountWrapper { public List<Bank_Account__c> bacctList; public String status; public String message; public BankAccountWrapper(){ bacctList = new List<Bank_Account__c>(); } } }Full Test Class
@isTest private class BankAccountWebserviceTest{ static testMethod void testDoGet_WithoutId() { setupTestData(); Test.startTest(); // Set up the RestContext object System.RestContext.request = new RestRequest(); System.RestContext.response = new RestResponse(); RestContext.request.requestURI = 'https://cs87.salesforce.com/services/apexrest/dosystem/bankaccounts/'; RestContext.request.httpMethod = 'PATCH'; BankAccountWebservice.BankAccountWrapper results = new BankAccountWebservice.BankAccountWrapper(); results = BankAccountWrapper.doPatch(bacctList); Test.stopTest(); system.assertEquals(results.status, 'Success'); system.assertEquals(results.message,'2 Records Updated successfully'); system.assertEquals(System.RestContext.response.StatusCode, 200); } private static void setupTestData() { List<Account> accounts = new List<Account>(); accounts.add(new Account(Name='Account 1', Phone='(111) 222-3344', Website='www.account1.com')); accounts.add(new Account(Name='Account 2', Phone='(222) 333-4455', Website='www.account1.com')); insert accounts; List<Bank_Account__c> baccounts = new List<Bank_Account__c>(); baccounts.add(new Bank_Account__c(Account__c = accounts[0].Id, Bank__c = 'FNB', A_c_Number__c='123456789', Bank_A_C_Type__c='Savings',Debit_Order_A_c__c = true)); baccounts.add(new Bank_Account__c(Account__c = accounts[1].Id, Bank__c = 'FNB', A_c_Number__c='987654321', Bank_A_C_Type__c='Savings', Debit_Order_A_c__c = true)); insert baccounts; } }
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html
You can optimize this way too.