+ Start a Discussion
Mhlangano KhumaloMhlangano 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:
User-added image

If I remove the variable 'bacctList'

User-added image
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;        
    }
    
      
}


 
Nayana KNayana K
Please follow best practices in main class. SOQL inside for loop is not recommended.
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html 
@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';
        
        /* decalre and intilialize bacctList before adding below line*/
        BankAccountWebservice.BankAccountWrapper results = BankAccountWebservice.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;        
    }
}


 
Mhlangano KhumaloMhlangano Khumalo
Thanks @Nayana K I've made changes to my class to bulkify it.
/ *
   { "bacctList" : 
     [{
       "Id" : "a0g8E00000119zY",
       "A_c_Number__c" : "111",
       "Bank_code__c" : "250655"
      },
      {
       "Id" : "a0g8E00000119zd",
       "A_c_Number__c" : "2222",
       "Bank_code__c" : "051001"
      }]
   }
 *    
 ****************************************************************************************************/
@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>();
        integer updatedRecordCount=0;
        
        List<Bank_Account__c> ac= [select Name,Debit_Order_A_c__c,A_c_Number__c,Bank_code__c From Bank_Account__c where Id IN : bacctList];
         
        for(integer i=0; i<ac.size(); i++){                                    
            ac[i].Debit_Order_A_c__c = false;       
            bankAccountsToUpdate.add(ac[i]);
            updatedRecordCount++;                 
        }
              
        try {
            update bankAccountsToUpdate;

            response.bacctList = bankAccountsToUpdate;
            response.status = 'Success';
            response.message = updatedRecordCount+' 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>();
        }
    }     
}



 
Nayana KNayana K
/ *
   { "bacctList" : 
     [{
       "Id" : "a0g8E00000119zY",
       "A_c_Number__c" : "111",
       "Bank_code__c" : "250655"
      },
      {
       "Id" : "a0g8E00000119zd",
       "A_c_Number__c" : "2222",
       "Bank_code__c" : "051001"
      }]
   }
 *    
 ****************************************************************************************************/
@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>();
        integer updatedRecordCount=0;

        for(Bank_Account__c objBA :[select Name,Debit_Order_A_c__c,A_c_Number__c,Bank_code__c From Bank_Account__c where Id IN : bacctList]){                                    
            objBA.Debit_Order_A_c__c = false;       
            bankAccountsToUpdate.add(objBA);               
        }
		updatedRecordCount = bankAccountsToUpdate.size();
              
        try {
            update bankAccountsToUpdate;

            response.bacctList = bankAccountsToUpdate;
            response.status = 'Success';
            response.message = updatedRecordCount+' 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>();
        }
    }     
}

You can optimize this way too.