+ Start a Discussion
Hitesh KhannaHitesh Khanna 

how do i write test class for the method that accepts list of record ids.

This is my method :
public static List<String> UpdateStatus(List<String> recordId ){
        //Fetch the current values in sub status multiselect picklist
        GPO_Rebate_Payment__c gpopayment = [SELECT Sub_Status__c,Status__c FROM GPO_Rebate_Payment__c where id IN :recordId LIMIT 1];
         List<String> currentStatusValues = gpopayment.Sub_Status__c.split(';');
        
        If(gpopayment.Status__c == 'Pharma Payment')
        {
            if(!currentStatusValues.Contains('Overdue'))
            {
                 currentStatusValues.add('Overdue');
            }
        }
        else{
            if(currentStatusValues.Contains('Overdue'))
            {     
                for(Integer i = currentStatusValues.size() - 1; i >= 0; i--) {
                if(currentStatusValues[i].equals('Overdue')) 
                {
                currentStatusValues.remove(i);
                }
    }
                 //currentStatusValues.('Overdue');
            }
        /* String newStatusValues;
        newStatusValues = string.join(currentStatusValues ,';');
         gpopayment.Sub_Status__c = newStatusValues;*/
    
    }
        return currentStatusValues;

}

i have written this test method but it is giving me error of illegal assignment from List to List:
@isTest
    public static void UpdateStatusTest(){
        GPO_Rebate_Payment__c gpopayment = new GPO_Rebate_Payment__c();
        gpopayment.name = 'test class';
        gpopayment.Status__c = 'Pharma Payment';
        gpopayment.Sub_Status__c = 'In Progress';
        
        update gpopayment;
        
        test.startTest();
        List<String> recid = [SELECT Id FROM GPO_Rebate_Payment__c WHERE name='test class'];
        GPORebatePaymentSubStatusUpdate.UpdateStatusTest(recid.Id);
        test.stopTest();

 
Best Answer chosen by Hitesh Khanna
mukesh guptamukesh gupta
Hi Hitesh,

Please use below code:-
 
@isTest
    public static void UpdateStatusTest(){
        GPO_Rebate_Payment__c gpopayment = new GPO_Rebate_Payment__c();
        gpopayment.name = 'test class';
        gpopayment.Status__c = 'Pharma Payment';
        gpopayment.Sub_Status__c = 'In Progress';
        
        update gpopayment;

List<String> recList = new List<String>(); 
List<GPO_Rebate_Payment__c> recids = [SELECT Id FROM GPO_Rebate_Payment__c WHERE name='test class']; 
  for(GPO_Rebate_Payment__c rec : recids){ 
    recList.add(rec.Id); 
   }
        test.startTest();
		GPORebatePaymentSubStatusUpdate.UpdateStatus(recList);
        test.stopTest();
		}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

mukesh guptamukesh gupta
Hi Hitesh,

Please use below code:-
 
@isTest
    public static void UpdateStatusTest(){
        GPO_Rebate_Payment__c gpopayment = new GPO_Rebate_Payment__c();
        gpopayment.name = 'test class';
        gpopayment.Status__c = 'Pharma Payment';
        gpopayment.Sub_Status__c = 'In Progress';
        
        update gpopayment;

List<String> recList = new List<String>(); 
List<GPO_Rebate_Payment__c> recids = [SELECT Id FROM GPO_Rebate_Payment__c WHERE name='test class']; 
  for(GPO_Rebate_Payment__c rec : recids){ 
    recList.add(rec.Id); 
   }
        test.startTest();
		GPORebatePaymentSubStatusUpdate.UpdateStatus(recList);
        test.stopTest();
		}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
Hitesh KhannaHitesh Khanna
Thanks Mukesh for helping.
but i am getting the following error while running the test class. can u help ?
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Hitesh,

try with below code.
@isTest
    public static void UpdateStatusTest(){
        GPO_Rebate_Payment__c gpopayment = new GPO_Rebate_Payment__c();
        gpopayment.name = 'test class';
        gpopayment.Status__c = 'Pharma Payment';
        gpopayment.Sub_Status__c = 'In Progress';
        
        insert gpopayment;

List<String> recList = new List<String>(); 
List<GPO_Rebate_Payment__c> recids = [SELECT Id FROM GPO_Rebate_Payment__c WHERE name='test class']; 
  for(GPO_Rebate_Payment__c rec : recids){ 
    recList.add(rec.Id); 
   }
        test.startTest();
		GPORebatePaymentSubStatusUpdate.UpdateStatus(recList);
        test.stopTest();
		}
If this helps, Please mark it as best answer.

Thanks!!
 
Hitesh KhannaHitesh Khanna
How will i cover the ELSE condition logic of the method ? create a new record with the condition that fulfills the else condition ?
AnkaiahAnkaiah (Salesforce Developers) 
Create another Test method which should meet the else condition.
 
@isTest
    public static void UpdateStatusTest1(){
        GPO_Rebate_Payment__c gpopayment = new GPO_Rebate_Payment__c();
        gpopayment.name = 'test class';
        gpopayment.Status__c = 'Overdue';
        gpopayment.Sub_Status__c = 'In Progress';
        
        update gpopayment;

List<String> recList = new List<String>(); 
List<GPO_Rebate_Payment__c> recids = [SELECT Id FROM GPO_Rebate_Payment__c WHERE name='test class']; 
  for(GPO_Rebate_Payment__c rec : recids){ 
    recList.add(rec.Id); 
   }
        test.startTest();
		GPORebatePaymentSubStatusUpdate.UpdateStatus(recList);
        test.stopTest();
		}

If this helps, Please ,mark it as best answer.

Thanks!!