+ Start a Discussion
SFDC Apex DevSFDC Apex Dev 

Could any one help to write test class for this apex class...

APEX CLASS

public class Mynewclass{
    static List<St__c> lstSO = new List<St__c>();
    static List<St__c> lstSOnew = new List<St__c>();
    static Map<String,NewItem> fisMap  = new Map<String,NewItem>();
    static Map<String,NewItem> fiMap  = new Map<String,NewItem>();
    
public Static void CreateStObjRec(List<SC__Order__c> SoList){
    List<NewItem> lstNew = new List<NewItem>();
    Map<String,SC__Order__c> woMap  = new Map<String,SC__Order__c>();
    Map<String,St__c> soMap = new Map<String,St__c>();
    integer lstctr =0;
    
    for(SC__Order__c SO : SoList){
        lstctr=lstctr+1;
        system.debug('SO.Send_Trigger_Status__c '+SO.Send_Trigger_Status__c);
        system.debug('SO.SC__Order_Status__c '+SO.SC__Order_Status__c);
        system.debug('SO.SC__Order_Type__c '+SO.SC__Order_Type__c);
        system.debug('SO.Follow_Up__c '+SO.Follow_Up__c);
        system.debug('SO.Pro_Services_Inc__c '+SO.Pro_Services_Inc__c);
        system.debug('SO.Opportunity_ID__c '+SO.Opportunity_ID__c);
        
        if (SO.Send_Trigger_Status__c == 'Not Requested' && SO.SC__Order_Status__c != 'DONE' && SO.SC__Order_Type__c.containsIgnoreCase('install') && SO.Follow_Up__c == false && !String.isBlank(String.valueOf(SO.Pro_Services_Inc__c)) && !String.isBlank(SO.Opportunity_ID__c) && (SO.Opportunity_ID__c.length()==18 || SO.Opportunity_ID__c.length()==15 )){
             system.debug(' system.debug at 29');
             
               if ((!String.isBlank(SO.SC__Country__c)) && (!String.isBlank(String.ValueOf(SM_Route_Settings__c.getValues(SO.SC__Country__c)))) && SM_Route_Settings__c.getValues(SO.SC__Country__c).Valid_for_Proj__c == true){             
                     
                     system.debug(' Inside loop ');
                     SO.Send_Trigger_Status__c = 'Send Requested';
                     
                     NewItem post = new NewItem();
                     post.ParentId = SO.ID;
                     post.Body = 'Staging Object Record added for Wrk '+ SO.Name +' for interfacing to CM' +'\n';
                     post.Body = post.Body + 'CM Send Trigger Status is ' + SO.Send_Trigger_Status__c +'\n';  
                     post.Body = post.Body + 'Order status is ' + SO.SC__Order_Status__c +'\n';  
                     post.Body = post.Body + 'Order Type is ' + SO.SC__Order_Type__c +'\n';
                     post.Body = post.Body + 'Pro Services Inc is ' + SO.Pro_Services_Inc__c +'\n';
                     post.Body = post.Body + 'Opportunity ID is  ' + SO.Opportunity_ID__c +'\n';
                     post.Body = post.Body + 'Country is  ' + SO.SC__Country__c ;
                                 
                     if (!String.isBlank(SO.ID))
                     {
                         lstNew.add(post);
                     }
                     system.debug(' SO DETAILS ============== : '+SO);
                     fiMap.put(SO.OwnerId+lstctr.format(),post);
                     system.debug('fismap details >>>>>>>>'+fiMap);
                     system.debug('SO.name+lstctr.format() === '+SO.OwnerId+lstctr.format());
                     
                     St__c SO = new St__c();
                     SO.RecordTypeId = getRecordType('Wrk');
                     SO.Opportunity_ID__c = SO.Opportunity_ID__c;
                     SO.Order_ID__c = SO.ID;
                     SO.Order_Number__c = SO.ID;
                     lstSO.add(SO);
                                          
                     soMap.put(wo.OwnerId+lstctr.format(),SO);
                     woMap.put(wo.OwnerId+lstctr.format(),SO);            
             }
               else if ((String.isBlank(SO.SC__Country__c)) || (String.isBlank(String.ValueOf(SM_Route_Settings__c.getValues(SO.SC__Country__c)))) || SM_Route_Settings__c.getValues(SO.SC__Country__c).Valid_for_Proj__c == false){             
                     system.debug('Inside not meet criteria at 73===========================');
                     SO.Send_Trigger_Status__c = 'Does Not Meet Criteria';
                     
                     NewItem post = new NewItem();
                     post.ParentId = SO.ID;
                     post.Body = 'Wrk '+ SO.Name +' does not meet criteria for interfacing to CM'+'\n';
                     post.Body = post.Body + 'CM Send Trigger Status is ' + SO.Send_Trigger_Status__c +'\n';  
                     post.Body = post.Body + 'Order status is ' + SO.SC__Order_Status__c +'\n';  
                     post.Body = post.Body + 'Order Type is ' + SO.SC__Order_Type__c +'\n';
                     post.Body = post.Body + 'Pro Services Inc is ' + SO.Pro_Services_Inc__c +'\n';
                     post.Body = post.Body + 'Opportunity ID is  ' + SO.Opportunity_ID__c +'\n';
                     post.Body = post.Body + 'Country is  ' + SO.SC__Country__c;
            
                     if (!String.isBlank(SO.ID))
                     {
                         lstNew.add(post);
                     }
                     fiMap.put(wo.OwnerId+lstctr.format(),post);  
                 }        
        }  
        
    if (!lstNew.isEmpty()){
        upsert lstNew;   
    }
       
    if (!soMap.isEmpty()){
            Insert soMap.values();
        }
        system.debug('soMap Values : '+ soMap);
        system.debug('fiMap Values == : '+ fiMap);
        for(String mapKey: soMap.keySet()){
            SC__Order__c temp = SOMap.get(mapKey);
            St__c sso = soMap.get(mapKey);
            temp.Obj_Name__c = sso.ID;
            SOMap.put(mapkey,temp);
            NewItem fi = fiMap.get(mapKey);
            if (fi != null){
                system.debug('New Item '+fi);
                system.debug('sso.id == '+ sso.id);
                if (!String.isBlank(sso.id)){
                      if (String.isBlank(fi.Parentid)){
                        system.debug('debug at 191 line ==');
                        fi.Parentid = sso.id;
                        fisMap.put(sso.id,fi);
                    }
                }
            }
        }        
        system.debug('fisMap values ================'+fisMap.values());    
   } 
}
v varaprasadv varaprasad
Hi Allen,

Check once below sample code : 
@isTest 
private class MynewclassTestClass {
    @testSetup
	static void TestData() {
  
     list<SC__Order__c> lstOrders = new list<SC__Order__c>();
  	 
      for(integer i=0; i<=10; i++){
	  SC__Order__c b = new SC__Order__c();
		   b.name = 'Test'+i;
		   // Add reamining all fields here..
			 lstOrders.add(b);
       } 

      insert lstOrders;
	  
       
     static testMethod void CreateStObjRecTest(){
		// Retrieve the orders
        Test.startTest();
			   list<SC__Order__c> lstOrders = [SELECT id,name FROM SC__Order__c];	 
			   Mynewclass.CreateStObjRec(lstOrders);	
       Test.stopTest();
      }	 
	  
    }
}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
Raj VakatiRaj Vakati
Try this
 
@isTest 
private class MynewclassTestClass {
    @testSetup
	static void TestData() {
  
     list<SC__Order__c> lstOrders = new list<SC__Order__c>();
	 
	  Account a = new Account(Name = 'Test');
        
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		
		SM_Route_Settings__c rounte = new SM_Route_Settings__c() ;
	rounte.Name ='USA ' ;
	rounte.SC__Country__c ='USA';
	rounte.Valid_for_Proj__c =true ; 
	insert rounte ;
	
		
		
		SM_Route_Settings__c rounte = new SM_Route_Settings__c() ;
	rounte.Name ='UK ' ;
	rounte.SC__Country__c ='UK';
	rounte.Valid_for_Proj__c =false ; 
	insert rounte ;
	
	
  	 
      for(integer i=0; i<=5; i++){
	  SC__Order__c b = new SC__Order__c();
		   b.name = 'Test'+i;
		  b.Send_Trigger_Status__c ='Not Requested' ;
		  b.SC__Order_Status__c  ='Not DONE';
		  b.SC__Order_Type__c='installation' ;
		  b.Opportunity_ID__c = o.Id ;
		  b.Follow_Up__c  = false ;
		  b.Pro_Services_Inc__c ='Testing';
		  b.SC__Country__c='USA';
		  
		  
		  SC__Order__c b1 = new SC__Order__c();
		   b1.name = 'Test'+i;
		  b1.Send_Trigger_Status__c ='Not Requested' ;
		  b1.SC__Order_Status__c  ='Not DONE';
		  b1.SC__Order_Type__c='installation' ;
		  b1.Opportunity_ID__c = o.Id ;
		  b1.Follow_Up__c  = false ;
		  b1.Pro_Services_Inc__c ='Testing';
		  b1.SC__Country__c='UK';
		  
		  
		 lstOrders.add(b1);
        
			 lstOrders.add(b);
       } 

      insert lstOrders;
	  
       
     static testMethod void CreateStObjRecTest(){
		// Retrieve the orders
        Test.startTest();
			   list<SC__Order__c> lstOrders = [SELECT id,name FROM SC__Order__c];	 
			   Mynewclass.CreateStObjRec(lstOrders);	
       Test.stopTest();
      }	 
	  
    }
}