function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harjeet Singh 13Harjeet Singh 13 

How to cover batch class in test class: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.

Hi All,

I am writing a test class for a batch class but my current code coverage is 0% and not able to cover. I am geetting error as:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Account__c]
Batch class is as belows:
global class UpdatePickUpstatusBatch implements 
    Database.Batchable<sObject>, Database.Stateful {
    

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT ID, Pickup_Date__c, Pickup_Status__c, Name FROM Pickup_Request__c Where Pickup_Date__c != null and Pickup_Status__c==\'Waitlisted\'');

    }

    global void execute(Database.BatchableContext bc, List<Pickup_Request__c> PRScope){
        // process each batch of records
        List<Pickup_Request__c> updateprlist = new List<Pickup_Request__c>();
        for (Pickup_Request__c pr : PRScope) {
           if(Date.today()==pr.Pickup_Date__c-Integer.valueof(Label.PickDateDaysforBatch))
           {
               pr.Pickup_Status__c = 'Open';
               updateprlist.add(pr);
                // make http callouts here Start
                
                
                // End              
           }
        }
        if(!updateprlist.IsEmpty()){
            update updateprlist;
        }
    }    

    global void finish(Database.BatchableContext bc){
        
    }    

}

My test class is as belows:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
        insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            AccRec.Parent = AccRecCustomer;
        
           //AccRec.Parent =  AccRecCustomer.id;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
        insert AccRec;
        
        Account AccRecBilling = new Account();
            AccRecBilling.Name = 'Acme';
            AccRecBilling.Parent = AccRecCustomer;
            AccRecBilling.Phone = '1234';
            AccRecBilling.Status__c = 'Active';
            AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
            
        insert AccRecBilling;

               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com';
            
        insert ConRec;
        
         Contact ConRec1 = new Contact();
            ConRec1.FirstName = 'John1';
            ConRec1.LastName = 'Smith1';
            ConRec1.Phone = '12345';
            ConRec1.Email = 'abc1@gmail.com';
            
        insert ConRec1;
        
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            
        Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
         
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
           /** PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Draft';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            //PickReqRec.Address__c = AddRec.id;
            PickReqRec.password__c = 'test data';
            
        Insert PickReqRec;**/
       
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
            //PickReqRec.Customer__c = AccRec.id;
        	PickReqRec.Account__c =AccRec.id;

            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
        System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Below is the look up filter:
Object-Pickup Request
Field label: Site
Field API: Account__c
User-added image

When I removed the filter then my code coverage becomes 72% but that is not a correct approach. I need not have to remove the filter infact I do need to cover the same in my test class which I am not getting.

Kindly help me

Thanks & Regards,
Harjeet
 
Best Answer chosen by Harjeet Singh 13
sravan velamasravan velama
Harjeet,
Insert these

 Contact ConRec1 = new Contact();
ConRec1.FirstName = 'John1';
ConRec1.LastName = 'Smith1';
 ConRec1.Phone = '12345';
ConRec1.Email = 'abc1@gmail.com';
  insert ConRec1;

Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
Account AccRecBilling = new Account();
 AccRecBilling.Name = 'Acme';
 AccRecBilling.Parent = AccRecCustomer;
AccRecBilling.Phone = '1234';
AccRecBilling.Status__c = 'Active';
AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
 insert AccRecBilling;

Same as your previous code
 

All Answers

sravan velamasravan velama
Hi Harjeet,

You have to insert the records according to the filter criteria, then you will get the code coverage and disabling filter is not the right approach because it may lead to many issues, while migrating to production.
So, try follow the filter criteria and according to which insert the records in test class.
Please place a good screen shot of the filter criteria.So, you may get the required help.

Thanks,
Sravan.

 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Thanks for your respose.

I know disabling lookup filter is not right approach. I am trying to insert the record according to lookup filter only but not able to do the same. 
User-added image
sravan velamasravan velama
Hi Harjeet,

After looking into that screen shot, I can find the following errors you are doing in your code.
1) You have said that the API name for site is Account__c but you are inserting standard Account  object.
2)Please insert  Pickup_Request__c  first then insert Account__c according to the filter criteria and it will work.

//Pickup request object

PickReqRec.Pickup_Type__c = 'Pickup';
 PickReqRec.Pickup_Status__c = 'Waitlisted';
PickReqRec.Source__c = 'Email';
 PickReqRec.Third_Party_Contact__c = ConRec1.id;
PickReqRec.Contact__c = ConRec.id;
 PickReqRec.Billing_A_c__c = AccRecBilling.id;
PickReqRec.Ready_Time__c = '12:00';
 PickReqRec.Close_Time__c = '19:00';
PickReqRec.SuburbT__c = 'DOCKLANDS';
PickReqRec.Pickup_Date__c = System.today()+1;
 PickReqRec.State__c = 'test Sate';
 PickReqRec.Service__c ='Priority';
PickReqRec.Total_Wt__c = 3000;
 PickReqRec.Address__c = CustAddRec.id;
 PickReqRec.password__c = 'test data';
 PickReqRec.Destination_SuburbL__c = SuburRec.id;
PickReqRec.H_cm__c = 444;
PickReqRec.Item_Description__c ='Item';
 PickReqRec.L_cm__c = 44;
PickReqRec.No_of_Items__c =4;
 PickReqRec.Customer__c = AccRecCustomer.id;
//PickReqRec.Customer__c = AccRec.id;
 PickReqRec.Account__c =AccRec.id;
 PickReqRec.W_cm__c = 400;
PickReqRec.Food_Stuff__c = 'Yes';


//Site object
Account__c AccRecBilling = new Account__c();
AccRecBilling.Name = 'Acme';
 AccRecBilling.Parent = AccRecCustomer.Id;
AccRecBilling.Phone = '1234';
 AccRecBilling.Status__c = 'Active';
 AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

I am getting error in line no 145(see my test class above).

Object name-Pickup request
Label name : Site
Api name is account__c.
Data type: Lookup(Account)

Below is the lookup filter:
 
Lookup Filter


Filter Criteria 
(Site: Account Record Type: Record Type NameequalsSite) and (Site: Parent Account IDequalsPickup Request: Customer ID) and (Site: Statusnot equal toMarked for Deletion) and (Site: Statusnot equal toRequested Close) and (Site: Statusnot equal toStop Trade) 
  
Filter Type Required. The user-entered value must match filter criteria. 
Error Message Value does not exist or does not match filter criteria. 
Lookup Window Text   
Active Checked

Kindly help me

Thanks & Regards,
Harjeet
sravan velamasravan velama
Hi Harjeet,

The line PickReqRec.Account__c =AccRec.id (Here you are inserting Standard account object not Site).
As, I mentioned previously first insert the Pick Request Object and then insert the Site Object
According to my observation:
//Insertion of Account
Account AccRecCustomer = new Account();
AccRecCustomer.Name = 'Acme';
AccRecCustomer.Phone = '1234';
AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
AccRecCustomer.Password__c = 'test data';
AccRecCustomer.Status__c = 'Active'; insert AccRecCustomer;
insert AccRecCustomer;
//Insertion of Contact
Contact ConRec = new Contact();
ConRec.FirstName = 'John';
ConRec.LastName = 'Smith';
ConRec.Phone = '1234';
ConRec.Email = 'abc@gmail.com';
insert ConRec;
//Insertion of Address
Address__c AddRec = new Address__c();
AddRec.Address1__c = 'G 370 Docklands Dr';
AddRec.Country__c = 'AUSTRALIA';
AddRec.State__c = 'VIC';
AddRec.Suburb__c = 'Docklands';
AddRec.Zipcode__c = 3008;
AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
Insert AddRec;
String AddRecId = AddRec.id; 
//Insertion of Customer Address CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
CustAddRec.Account__c = AccRec.id;
CustAddRec.Address__c = AddRec.id; 
CustAddRec.Type__c = 'Site';
CustAddRec.InActive__c = false;
 Insert CustAddRec;
//Insertion of Sub
Suburbs__c  SuburRec = new Suburbs__c ();
SuburRec.Name = 'DOCKLANDS';
SuburRec.Post_Code__c = '3008';
SuburRec.State__c = 'VIC';
SuburRec.Depot__c = 'MEL';
SuburRec.City__c = '818 Bourke Street';
insert SuburRec; 
//Insertion of Holiday        
Holiday__c  HolRec = new Holiday__c ();
HolRec.Name = 'ADELAIDE CUP DAY';
HolRec.Type__c = 'Normal';
HolRec.State_Name__c = 'VIC';
HolRec.Start_Date__c = System.today();
HolRec.End_Date__c = System.today()+2;
Insert HolRec ;
//Insertion of Pickup Request       
Pickup_Request__c  PickReqRec = new Pickup_Request__c();
PickReqRec.Pickup_Type__c = 'Pickup';
PickReqRec.Pickup_Status__c = 'Draft';
PickReqRec.Source__c = 'Email';
PickReqRec.Third_Party_Contact__c = ConRec1.id;
PickReqRec.Contact__c = ConRec.id;
PickReqRec.Billing_A_c__c = AccRecBilling.id;
PickReqRec.Ready_Time__c = '12:00';
PickReqRec.Close_Time__c = '19:00';
PickReqRec.SuburbT__c = 'DOCKLANDS';
PickReqRec.Pickup_Date__c = System.today()+1;
PickReqRec.State__c = 'test Sate';
PickReqRec.Service__c ='Priority';
PickReqRec.Total_Wt__c = 3000;
PickReqRec.Address__c = AddRec.id;
PickReqRec.Customer__c = AccRecCustomer.id;
PickReqRec.password__c = 'test data';
Insert PickReqRec;
//Insertion of Site
Account__c AccRecBilling = new Account__c();
AccRecBilling.Name = 'Acme';
AccRecBilling.Parent = AccRecCustomer.Id;//Parent Id condition satisfied
AccRecBilling.Phone = '1234';
AccRecBilling.Status__c = 'Active';//Status condition satisfied
AccRecBilling.RecordTypeId= ACCBillingRecTypeid;// Recordtype condition satisfied

     I have one doubt, the pickup request object is required for Batch class then why do you require SITE. Does both have any relationship. Please let me know the relationship if the above approach fails. Then try this below approach

--------------------This approach is when you have site has lookup on pickup request------------------------------
//Insertion of Pickup Request       
Pickup_Request__c  PickReqRec = new Pickup_Request__c();
PickReqRec.Pickup_Type__c = 'Pickup';
PickReqRec.Pickup_Status__c = 'Draft';
PickReqRec.Source__c = 'Email';
PickReqRec.Third_Party_Contact__c = ConRec1.id;
PickReqRec.Contact__c = ConRec.id;
PickReqRec.Billing_A_c__c = AccRecBilling.id;
PickReqRec.Ready_Time__c = '12:00';
PickReqRec.Close_Time__c = '19:00';
PickReqRec.SuburbT__c = 'DOCKLANDS';
PickReqRec.Pickup_Date__c = System.today()+1;
PickReqRec.State__c = 'test Sate';
PickReqRec.Service__c ='Priority';
PickReqRec.Total_Wt__c = 3000;
PickReqRec.Address__c = AddRec.id;
PickReqRec.Customer__c = AccRecCustomer.id;
PickReqRec.password__c = 'test data';
Insert PickReqRec;
//Insertion of Site
Account__c AccRecBilling = new Account__c();
AccRecBilling.Name = 'Acme';
AccRecBilling.Parent = AccRecCustomer.Id;//Parent Id condition satisfied
AccRecBilling.Phone = '1234';
AccRecBilling.Status__c = 'Active';//Status condition satisfied
AccRecBilling.RecordTypeId= ACCBillingRecTypeid;// Recordtype condition satisfied
insert AccRecBilling;
//Updation of Pickup Request
Pickup_Request__c  PickReqRec = new Pickup_Request__c(Id =PickReqRec.Id );
PickReqRec.Account__c =AccRecBilling.id;
update PickReqRec;

 I hope this would help you....!

 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Site is not any object. 
Site is field label name having lookup with Account
Object-Pickup request is having a field 'Site'(API Name- Account__c) datatype Lookup(Account)

How can I insert records for Site when Site is not an object rather its just a field.

I am getting error for this field only in my test class

Kindly help me

Thanks & Regards,
Harjeet
sravan velamasravan velama
Harjeet can you uncomment line 35 in your test class code and check it
 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

When i uncomment line no 35
 
AccRec.Parent =  AccRecCustomer.id;
Then I need to comment line no 34 as shown below:
 
//AccRec.Parent = AccRecCustomer;

Now when I am trying to save test class it shows error as:
Illegal assignment from Id to Account

Below is my updated test class after uncommenting line no 35 as belows:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
        insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            //AccRec.Parent = AccRecCustomer;
        
           AccRec.Parent =  AccRecCustomer.id;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
        insert AccRec;
        
        Account AccRecBilling = new Account();
            AccRecBilling.Name = 'Acme';
            AccRecBilling.Parent = AccRecCustomer;
            AccRecBilling.Phone = '1234';
            AccRecBilling.Status__c = 'Active';
            AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
            
        insert AccRecBilling;

               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com';
            
        insert ConRec;
        
         Contact ConRec1 = new Contact();
            ConRec1.FirstName = 'John1';
            ConRec1.LastName = 'Smith1';
            ConRec1.Phone = '12345';
            ConRec1.Email = 'abc1@gmail.com';
            
        insert ConRec1;
        
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            
        Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
         
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
           /** PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Draft';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            //PickReqRec.Address__c = AddRec.id;
            PickReqRec.password__c = 'test data';
            
        Insert PickReqRec;**/
       
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
            //PickReqRec.Customer__c = AccRec.id;
        	PickReqRec.Account__c =AccRec.id;

            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
        System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Total code coverage is 27%(3 out of 11 lines covered).
Below is screenshot of developer console:

User-added image

Kindly help me

Thanks & Regards,
Harjeet
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Now I have used try and catch block just to pass the filter condition but still 27% code coverage.

Kindly help me

Thanks & Regards,
Harjeet

 
Harjeet Singh 13Harjeet Singh 13
Sravan- If I remove try catch block as shown below:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
        insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            //AccRec.Parent = AccRecCustomer;
        
           AccRec.Parent =  AccRecCustomer.id;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
        insert AccRec;
        
        Account AccRecBilling = new Account();
            AccRecBilling.Name = 'Acme';
            AccRecBilling.Parent = AccRecCustomer;
            AccRecBilling.Phone = '1234';
            AccRecBilling.Status__c = 'Active';
            AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
            
        insert AccRecBilling;

               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com';
            
        insert ConRec;
        
         Contact ConRec1 = new Contact();
            ConRec1.FirstName = 'John1';
            ConRec1.LastName = 'Smith1';
            ConRec1.Phone = '12345';
            ConRec1.Email = 'abc1@gmail.com';
            
        insert ConRec1;
        
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            
        Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
         
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
           /** PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Draft';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            //PickReqRec.Address__c = AddRec.id;
            PickReqRec.password__c = 'test data';
            
        Insert PickReqRec;**/
        
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            //PickReqRec.Customer__c = AccRecCustomer.id;
            PickReqRec.Customer__c = AccRec.id;
        	PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
            System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Then I am getting error while saving test class as:
Illegal assignment from Id to Account

And then upon running class my test class is getting failed with same error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Account__c]

Kindly help me

Thanks & Regards,
Harjeet
sravan velamasravan velama
Harjeet,

Is Parent field a Lookup to Account and I can see that you are doing self Loop----Is it Right?

And using Try/Catch block in test class is used in the most worst conditions.----NOTE

Try this code:(HERE I ASSUMED THE CUSTOMER FIELD ON PICKUP OBJECT AS LOOKUP AND THERE IS SELF LOOP ON ACCOUNT OBJECT)
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
            insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            AccRec.Parent =  AccRecCustomer;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
            insert AccRec;
               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com'; 
            insert ConRec;
            
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
            PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
            System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Try this code:(HERE I ASSUMED THE CUSTOMER FIELD ON PICKUP OBJECT AS STRING AND PARENT FIELD ON ACCOUNT AS STRING)
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
            insert AccRecCustomer;

        String aVal = AccRecCustomer.Id;
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            AccRec.Parent =  aVal ;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
            insert AccRec;
               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com'; 
            insert ConRec;
            
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        String sVal = AccRecCustomer.id;
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = sVal ;
            PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
            System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}


Note:- IF YOU STILL GET THIS ERROR ---->Illegal assignment from Id to Account
Then do type casting your problem will be resolved.


 
sravan velamasravan velama
Hi Harjeet,

According your screen shot, The execute method is not getting covered because no pickup objects records are retrieved
Try to see whether you have exact record which matches the filter criteria in your SOQL query.
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

I copied pasted your code but I ma getting 2 errors while saving:
line no 84:Variable does not exist: ConRec1
line no 86: Variable does not exist: AccRecBilling

Below is the test class suggested by you:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
            insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            AccRec.Parent =  AccRecCustomer;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
            insert AccRec;
               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com'; 
            insert ConRec;
            
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
            PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
            System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Kindly help me

Thanks & Regards,
Harjeet
sravan velamasravan velama
Harjeet,
Insert these

 Contact ConRec1 = new Contact();
ConRec1.FirstName = 'John1';
ConRec1.LastName = 'Smith1';
 ConRec1.Phone = '12345';
ConRec1.Email = 'abc1@gmail.com';
  insert ConRec1;

Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
Account AccRecBilling = new Account();
 AccRecBilling.Name = 'Acme';
 AccRecBilling.Parent = AccRecCustomer;
AccRecBilling.Phone = '1234';
AccRecBilling.Status__c = 'Active';
AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
 insert AccRecBilling;

Same as your previous code
 
This was selected as the best answer
sravan velamasravan velama
Please let me know any further issue
 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Thanks for all of your kind help.

I have tweeked code a bit and now I am getting 72% code coverage.

I did this without using try catch and removing lookup filter.

Below is my test class:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
        insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            //AccRec.Parent = AccRecCustomer;
            AccRec.Parentid =  AccRecCustomer.id;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
        insert AccRec;
        
        Account AccRecBilling = new Account();
            AccRecBilling.Name = 'Acme';
            AccRecBilling.Parentid = AccRecCustomer.id;
            AccRecBilling.Phone = '1234';
            AccRecBilling.Status__c = 'Active';
            AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
            
        insert AccRecBilling;

               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com';
            
        insert ConRec;
        
         Contact ConRec1 = new Contact();
            ConRec1.FirstName = 'John1';
            ConRec1.LastName = 'Smith1';
            ConRec1.Phone = '12345';
            ConRec1.Email = 'abc1@gmail.com';
            
        insert ConRec1;
        
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            
        Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
         
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
           /** PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Draft';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            //PickReqRec.Address__c = AddRec.id;
            PickReqRec.password__c = 'test data';
            
        Insert PickReqRec;**/
       
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
        	PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
        System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

Below are the lines 25,26 and 30  from main class which is not getting covered:
/**    
Class Name        : UpdatePickUpstatusBatch
Purpose/Overview  : Batch class for status update based on pickupdate, which is tomorrow's date
Author            : Ravi Kiran
Test Class        : UpdatePickUpstatusBatch_Test
Change History    : Date Modified : Developer Name     : Method/Section Modified/Added : Purpose/Overview of Change
 
 */

global class UpdatePickUpstatusBatch implements 
    Database.Batchable<sObject>, Database.Stateful {
    

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT ID, Pickup_Date__c, Pickup_Status__c, Name FROM Pickup_Request__c Where Pickup_Date__c != null and Pickup_Status__c =\'Waitlisted\'');

    }

    global void execute(Database.BatchableContext bc, List<Pickup_Request__c> PRScope){
        // process each batch of records
        List<Pickup_Request__c> updateprlist = new List<Pickup_Request__c>();
        for (Pickup_Request__c pr : PRScope) {
           if(Date.today()==pr.Pickup_Date__c-Integer.valueof(Label.PickDateDaysforBatch))
           {
               pr.Pickup_Status__c = 'Open';
               updateprlist.add(pr);
	   }
        }       
            if(!updateprlist.IsEmpty()){
                update updateprlist;
            }        
    }    

    global void finish(Database.BatchableContext bc){
        
    }    

}

Kindly help me 

Thanks & Regards,
Harjeet

 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Kindly help me to achieve code coverage. My test class having 72% code coverage and lines no 25,26 and 30(see above main class) is not getting covered.

Kindly help me

Thanks & Regards,
Harjeet
sravan velamasravan velama
Harjeet,
Change Line  PickReqRec.State__c = 'Open';
You will able get it covered
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Thanks for your quick response.
Will it be PickReqRec.State__c = 'Open' or Will it be PickReqRec.Pickup_Status__c='Open'.

Please refer my main class(batch class).

If its PickReqRec.Pickup_Status__c='Open'. then I have tried the same changing status from Waitlisted to Open but still code coverage is 72%.

Kindly help me

Thanks & Regards,
Harjeet


 
Harjeet Singh 13Harjeet Singh 13
Hi Sravan,

Thanks for all the help you gave me.

I have covered 100% code coverage.

Below is my test class:
 
@isTest
public class UpdatePickUpstatusBatch_Test {
    static testMethod void UpdatePickUpstatusBatchtest1(){
    
    Schema.DescribeSObjectResult ACCobj1 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = ACCobj1 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName = rtMapByName.get('Site');
    ID ACCSiteRecTypeid = rtByName.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj2 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName2 = ACCobj2 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName2 = rtMapByName2.get('Customer');
    ID ACCCustRecTypeid = rtByName2.getRecordTypeId(); 
    
    Schema.DescribeSObjectResult ACCobj3 = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName3 = ACCobj3 .getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName3 = rtMapByName3.get('Billing');
    ID ACCBillingRecTypeid = rtByName3.getRecordTypeId(); 
    
        
        
        Account AccRecCustomer = new Account();
            AccRecCustomer.Name = 'Acme';
            AccRecCustomer.Phone = '1234';            
            AccRecCustomer.RecordTypeId= ACCCustRecTypeid;
            AccRecCustomer.Password__c = 'test data';
            AccRecCustomer.Status__c = 'Active';
        insert AccRecCustomer;
        
        Account AccRec = new Account();
            AccRec.Name = 'Acme';
            AccRec.Phone = '1234';
            //AccRec.Parent = AccRecCustomer;
            AccRec.Parentid =  AccRecCustomer.id;
            AccRec.Status__c = 'Active';
            AccRec.RecordTypeId= ACCSiteRecTypeid;
            AccRec.Password__c = 'test data';
        insert AccRec;
        
        Account AccRecBilling = new Account();
            AccRecBilling.Name = 'Acme';
            AccRecBilling.Parentid = AccRecCustomer.id;
            AccRecBilling.Phone = '1234';
            AccRecBilling.Status__c = 'Active';
            AccRecBilling.RecordTypeId= ACCBillingRecTypeid;
            
        insert AccRecBilling;

               
        Contact ConRec = new Contact();
            ConRec.FirstName = 'John';
            ConRec.LastName = 'Smith';
            ConRec.Phone = '1234';
            ConRec.Email = 'abc@gmail.com';
            
        insert ConRec;
        
         Contact ConRec1 = new Contact();
            ConRec1.FirstName = 'John1';
            ConRec1.LastName = 'Smith1';
            ConRec1.Phone = '12345';
            ConRec1.Email = 'abc1@gmail.com';
            
        insert ConRec1;
        
        Address__c AddRec = new Address__c();
            AddRec.Address1__c = 'G  370 Docklands Dr';
            AddRec.Country__c = 'AUSTRALIA';
            AddRec.State__c = 'VIC';
            AddRec.Suburb__c = 'Docklands';
            AddRec.Zipcode__c = 3008;
            AddRec.Shipping_Address_Validation_Status__c = 'Verified by Experian';
            AddRec.Shipping_Address_Validation_Timestamp__c = system.today();
            
        Insert AddRec;
        String AddRecId = AddRec.id; 
        CustAddrAssoc__c  CustAddRec = new CustAddrAssoc__c ();
            CustAddRec.Account__c = AccRec.id;
            CustAddRec.Address__c = AddRec.id; 
            CustAddRec.Type__c = 'Site';
            CustAddRec.InActive__c = false;
        
        Insert CustAddRec;
        
        Suburbs__c  SuburRec = new Suburbs__c ();
            SuburRec.Name = 'DOCKLANDS';
            SuburRec.Post_Code__c = '3008';
            SuburRec.State__c = 'VIC';
            SuburRec.Depot__c = 'MEL';
            SuburRec.City__c = '818 Bourke Street';
         
        insert SuburRec; 
        
        Holiday__c  HolRec = new Holiday__c ();
            HolRec.Name = 'ADELAIDE CUP DAY';
            HolRec.Type__c = 'Normal';
            HolRec.State_Name__c = 'VIC';
            HolRec.Start_Date__c = System.today();
            HolRec.End_Date__c = System.today()+2;
        Insert HolRec ;
        
        Pickup_Request__c  PickReqRec = new Pickup_Request__c();
        
           /** PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Draft';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today()+1;
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            //PickReqRec.Address__c = AddRec.id;
            PickReqRec.password__c = 'test data';
            
        Insert PickReqRec;**/
       
            PickReqRec.Pickup_Type__c = 'Pickup';
            PickReqRec.Pickup_Status__c = 'Waitlisted';
            PickReqRec.Source__c = 'Email';
            PickReqRec.Third_Party_Contact__c = ConRec1.id;
            PickReqRec.Contact__c = ConRec.id;
            PickReqRec.Billing_A_c__c = AccRecBilling.id;
            PickReqRec.Ready_Time__c = '12:00';
            PickReqRec.Close_Time__c = '19:00';
            PickReqRec.SuburbT__c = 'DOCKLANDS';
            PickReqRec.Pickup_Date__c = System.today() + Integer.valueof(Label.PickDateDaysforBatch);
            PickReqRec.State__c = 'test Sate';
            PickReqRec.Service__c ='Priority';
            PickReqRec.Total_Wt__c = 3000;
            PickReqRec.Address__c = CustAddRec.id;
            PickReqRec.password__c = 'test data';
            PickReqRec.Destination_SuburbL__c = SuburRec.id;
            PickReqRec.H_cm__c = 444;
            PickReqRec.Item_Description__c ='Item';
            PickReqRec.L_cm__c = 44;
            PickReqRec.No_of_Items__c =4;
            PickReqRec.Customer__c = AccRecCustomer.id;
        	PickReqRec.Account__c =AccRec.id;
            PickReqRec.W_cm__c = 400;
            PickReqRec.Food_Stuff__c  = 'Yes';
            
        Insert PickReqRec;
        
        
       
        Pickup_Request__c PickReqRecObj = [Select id ,Pickup_Status__c ,Pickup_Date__c from Pickup_Request__c  where id =:PickReqRec.id LIMIT 1];
        
        System.debug('----->'+PickReqRecObj );

        //PickReqRec.Pickup_Status__c = 'Open';
        //update PickReqRec;
        
        Test.startTest();

        UpdatePickUpstatusBatch UpdateBatch = new UpdatePickUpstatusBatch();
          //DataBase.executeBatch(UpdateBatch);
        ID batchprocessid = Database.executeBatch(UpdateBatch);
        Test.stopTest();
    }
}

I will mark your last reply as best answer because that helped me to get 100% code coverage.

Many thanks Sravan

Thanks & Regards,
Harjeet
sravan velamasravan velama
Harjeet,

Sorry, Actually it was PickReqRec.Pickup_Status__c='Waitlisted'.
You have to satisfy this condition  if(Date.today()==pr.Pickup_Date__c-Integer.valueof(Label.PickDateDaysforBatch)
For example: If PickDateDaysforBatch = 1 and pr.Pickup_Date__c = 30/08/2017, then it will match the condition and it will be equla to Today() date.
So check what is the value of PickDateDaysforBatch and give the  pr.Pickup_Date__c according to which it would be equal to Current date.

Thanks,
Sravan Velama

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Harjeet Singh 13,

This is look like duplicate issue. You issue is resolved in below post.
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000BilwQAC

Let us know if you need more help.

Issue i found in your test class are below
1) Add Parent Id.
AccRec.Parentid =  AccRecCustomer.id;
2) Set Pickup date like below
PickReqRec.Pickup_Date__c = System.today() + Integer.valueof(Label.PickDateDaysforBatch);

Let us know if this will help you

 
John Deo 10John Deo 10
Hi All,

As i want to migrate content from Orchestra Cms to AEM ,is there any Endpoint Urls provided by orchestra that i can get all conent in any XML Or Json form Please help on this part.

Thanks! in Advance
Akshay MhetreAkshay Mhetre

FACING THE SAME ERROR   .... PLEASE HELP ME TO ACHIEVE 85% code coverage.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Fi_Agent__c]

Class.SharingUtilityTest.testSetup: line 87, column 1   ==== 

 FI_Agent_Mapping__C fi= new FI_Agent_Mapping__c();
            fi.Fi_Agent__c=usr.Id;
            fi.Agency_Name__c=grp.Name ;
            insert fi;  // (Line no 87)

 

Main Class==   


public without sharing class SharingUtility {
       
    public static void recalculateSharing(String ObjectAPIName, Map<Id,String> sObjectIdVsStage, Map<Id,sObject> sObjectMap) {
       // try {//sObjectMap should be passed as map<loanId, LoanApplication_actors>
            List<sObject> sharingList = new List<sObject>();
            List<Record_Access__mdt> recordAccessMeta = new List<Record_Access__mdt>();
            map<Id, Sobject> shareMap = new Map<Id, Sobject>();
            //removeAccess(sObjectIdVsStage.keyset());
            recordAccessMeta = [SELECT MasterLabel, rowCause__c, userField__c, access__c FROM Record_Access__mdt WHERE MasterLabel IN: sObjectIdVsStage.values() AND Object_API_Name__c =:ObjectAPIName];
            
            for(Id loanid : sObjectIdVsStage.keyset()) {
                for(Record_Access__mdt mdt: recordAccessMeta) {
                    
                    if(mdt.MasterLabel.equalsIgnoreCase(sObjectIdVsStage.get(loanid)) && sObjectMap.get(loanid).get(mdt.userField__c)!=null ){
                        System.debug('in If');
                        String userid = mdt.userField__c;
                        sharingList.add(getLoanShareRecord(loanid,string.valueof(sObjectMap.get(loanid).get(userid)),mdt.access__c,mdt.rowCause__c));
                        shareMap.putall(sharingList); //when same cpa agent do D1 and D2  duplicate record is getting added in list so make it unique adding in map
                    }
                }
            }
            
           //insert sharingList;
            List<Database.upsertResult> srList = Database.upsert(shareMap.values(), true);
            // Iterate through each returned result
            for(Database.upsertResult dr : srList) {
                if (dr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    
                }
                else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : dr.getErrors()) {
                        
                    }
                }
            }
        /*}
        catch(Exception ex) {
            system.debug('Exception : '+ex);
            UTIL_Logging.ExceptionLog singleExceptionLog = new UTIL_Logging.ExceptionLog(ex, 'common_RecordAccess', 'recalculateSharing', '', UTIL_Logging.DEBUG_lEVEL_WARN);
            UTIL_Logging.logException(singleExceptionLog);
        }*/
    }
      
    
    
    public static void shareCaseRecordwithAgent (String sobj,Map<String,String> usercaseMap,String access, String reason){
         caseShareRecordBulk(sobj, usercaseMap, access, reason);
    }
    /**
        * @Description : This method is responsible to get Loan share record 
        */
    public static sObject getLoanShareRecord (String parentId, String userId, String access, String reason){
        String sObjectName = Id.valueOf(parentId).getSObjectType().getDescribe().getName();
        System.debug('sObjectName:: '+sObjectName);
        if(sObjectName.endsWithIgnoreCase('__c')) { 
            system.debug('sObjectName:'+sObjectName);
            sObjectName = sObjectName.removeEnd('__c')+'__Share';
            sObject obj = Schema.getGlobalDescribe().get(sObjectName).newSObject();
            obj.put('AccessLevel', access != null ?access : 'Read');
            obj.put('ParentId', parentId);
            obj.put('UserOrGroupId', userId);
            obj.put('rowCause', 'reason');//
            return obj;    
        }
        else if(sObjectName.equalsIgnorecase('Account')) {
            sObjectName = 'AccountShare';
              
            sObject obj = accountShareRecord(sObjectName,parentId,userId,access,reason);
                 return obj;    
        }else if(sObjectName.equalsIgnorecase('DocumentChecklistItem')) {
            sObjectName = 'DocumentChecklistItemShare';
            
            sObject obj = docCheckItemShareRecord(sObjectName,parentId,userId,access,reason);
                 return obj;    
        } else if(sObjectName.equalsIgnorecase('Case')) {
              sObjectName = 'CaseShare';
              

              sObject obj =caseShareRecord(sObjectName,parentId,userId,access,reason);
                 return obj;    
                } else if(sObjectName.equalsIgnorecase('Opportunity')) {
                    sObject obj = opportunityTeamRecord(sObjectName,parentId,userId,access,reason);
                    
                 return obj;    
                }else{
                  
                  return null; 
                }
     
    }
    
    @testvisible
    private static sObject opportunityTeamRecord(String sobj,String parentId, String userId, String access, String reason){
        
        if(userId.startsWith('005')){
        OpportunityTeamMember oldTeamMemberRec = getPrevCreatedOppShare(parentId, userId);
      
        OpportunityTeamMember oppTeamMember = new OpportunityTeamMember();
        if(oldTeamMemberRec != null){
            //If old Team record found then update access only.
            oppTeamMember.Id = oldTeamMemberRec.Id;
            oppTeamMember.OpportunityAccessLevel =  access;
        }else{
            oppTeamMember.OpportunityId = parentId;
            oppTeamMember.UserId = userId;
            oppTeamMember.OpportunityAccessLevel =  access;
            oppTeamMember.TeamMemberRole ='Team Member'; 
        }
            
            return oppTeamMember;
        }else{
            return opportunityShareRecord(sobj, parentId, userId, access, reason);
        }
        /*
          sObject obj = Schema.getGlobalDescribe().get(sobj).newSObject();
         obj.put('OpportunityAccessLevel',access);
          //obj.put('AccountAccessLevel', access != null ?access : 'Read');
        obj.put('OpportunityId', parentId);
        obj.put('UserOrGroupId', userId);
       // obj.put('rowCause', reason);
      
        */
     }
    

    @testvisible
    private static OpportunityTeamMember getPrevCreatedOppShare(string parentId, string userId){
        try{
        OpportunityTeamMember oppTeamMember = [SELECT Id FROM OpportunityTeamMember WHERE OpportunityId=:parentId AND UserId=:userId Limit 1];
        if(oppTeamMember != null){
            return oppTeamMember;
        }else{
            return null;
        }
        }catch(Exception e){   
            return null;
        }
        
    }
    
    
    @testvisible
    private static sObject opportunityShareRecord(String sobj,String parentId, String userId, String access, String reason){
       OpportunityShare oppShare = new OpportunityShare(OpportunityAccessLevel=access, OpportunityId = parentId,  UserOrGroupId = userId);
        return oppShare;
    }
    @testvisible
    private static sObject accountShareRecord (String sobj,String parentId, String userId, String access, String reason){
          sObject obj = Schema.getGlobalDescribe().get(sobj).newSObject();
          obj.put('AccountAccessLevel', access != null ?access : 'Read');
          obj.put('OpportunityAccessLevel','Read');
          //obj.put('AccountAccessLevel', access != null ?access : 'Read');
          obj.put('AccountId', parentId);
          obj.put('UserOrGroupId', userId);
          // obj.put('rowCause', reason);
          return obj;
     }
         /**
        * @Description : This method is responsible to delete Loan share record 
        */
    @testvisible
    private static void removeAccess (Set<Id> parentId){
        // delete all the existing sharing of these loan applications 
        
        list<OpportunityShare> loanShareRecords = new list<OpportunityShare>();
        loanShareRecords = [SELECT Id FROM OpportunityShare WHERE OpportunityId in :parentId];
        Database.DeleteResult[] drList = Database.delete(loanShareRecords, false);
    }
    //Added changes related to DocumentChecklistItem  - Soma
    @testvisible
    private static sObject docCheckItemShareRecord (String sobj,String parentId, String userId, String access, String reason){
        sObject obj = Schema.getGlobalDescribe().get(sobj).newSObject();
        obj.put('AccessLevel', access != null ?access : 'Read');
        obj.put('ParentId', parentId);
        obj.put('UserOrGroupId', userId);
     //   obj.put('RowCause',  Schema.DocumentChecklistItemShare.RowCause.Manual);
        return obj;
     }
     //Added changes related to Case record sharing - Piyush
    @testvisible
    private static sObject caseShareRecord (String sobj,String parentId, String userId, String access, String reason){
        String query = 'select id from CaseShare where UserOrGroupId =: userId and CaseId =: parentId';
        List<SObject> targetObjRecords = Database.query(query);
        delete targetObjRecords;
        CaseShare obj = new CaseShare(CaseAccessLevel = access ,CaseId = parentId, UserOrGroupId = userId, RowCause = reason);
        return obj;
     }
    
    @testvisible
    private static void caseShareRecordBulk (String sobj,Map<String,String> parentId,String access, String reason){
        List<Sobject> caseshareList= new List<Sobject>();
        List<String> userIdList=parentId.values();
        Set<String> CaseIdList=parentId.keySet();
        String query = 'select id from CaseShare where UserOrGroupId in: userIdList and CaseId in: CaseIdList';
        List<SObject> targetObjRecords = Database.query(query);
        if(targetObjRecords.size() > 0){
        delete targetObjRecords;    
        }else{
        sendNotification(parentId);    
        }
        for (String key : parentId.keySet()) {
            Sobject sObjec = Schema.getGlobalDescribe().get('CaseShare').newSobject();
            sObjec.put('CaseAccessLevel',access);
            sObjec.put('CaseId',key);
            sObjec.put('UserOrGroupId',parentId.get(key));
            sObjec.put('RowCause',reason);
            caseshareList.add(sObjec); 
        }        
        insert caseshareList;
        
     }
    @testvisible
    public static void agentMappingShareRecord(Map<String,String> parentId,String access, String reason){
        List<Sobject> caseshareList= new List<Sobject>();
        Set<String> userIdList=parentId.keySet();
        List<String> CaseIdList=parentId.values();
        String query = 'select id from FI_Agent_Mapping__Share where UserOrGroupId in: userIdList and ParentId in: CaseIdList';
        List<SObject> targetObjRecords = Database.query(query);
        delete targetObjRecords;
        for (String key : parentId.keySet()) {
            Sobject sObjec = Schema.getGlobalDescribe().get('FI_Agent_Mapping__Share').newSobject();
            sObjec.put('AccessLevel',access);
            sObjec.put('ParentId',key);
            sObjec.put('UserOrGroupId',parentId.get(key));
            sObjec.put('RowCause',reason);
            caseshareList.add(sObjec); 
        }        
        insert caseshareList;
     }
    public static void setDefaultOwner(List<Loan_Application_Actors__c> loanApplicationActorsList){
        insert loanApplicationActorsList;
    }
    
    @testvisible
    public static void removeAccess(Map<String,String> parentId){
        List<String> userIdList=parentId.values();
        Set<String> CaseIdList=parentId.keySet();
        String query = 'select id,UserOrGroupId,CaseAccessLevel,CaseId from CaseShare where UserOrGroupId in: userIdList and CaseId in: CaseIdList';
        List<sObject> targetObjRecords = Database.query(query);
        if(targetObjRecords.size() > 0){
            delete targetObjRecords;    
        }
        
    }
    
    public static void sendNotification(Map<String,String> caseuser){
        Id typeId = [SELECT Id FROM CUstomNotificationType WHERE DeveloperName = 'FI_Agent_Notification'].Id;
        for (String key : caseuser.keySet()) {
            Messaging.CustomNotification notification = new Messaging.CustomNotification();
            notification.setBody('FI Case Has Been Assigned To You');
            notification.setTitle('FI Case Has Been Assigned To You');
            notification.setSenderId(Userinfo.getUserId());
            notification.setNotificationTypeId(typeId );
            notification.setTargetId(key); // target object id
            notification.send(new Set<String> { caseuser.get(key) }); // target user id.
        } 
    }    
}
 
My Test Class==(37%)
@isTest
public class SharingUtilityTest {
   
     @testSetup
    public static void testSetup() 
    {
         Profile profileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        UserRole role = [Select id,Name from UserRole where Name='Sales' LIMIT 1];
        
        User usr = new User(LastName = 'test name',
                           FirstName='sample test',
                           Alias = 'jl test',
                           Email = 'test.sample@asdfcome.com',
                           Username = 'test.sample@abcsdfcome.com',
                           ProfileId = profileId.id,
                           LanguageLocaleKey = 'en_US',
                           timezonesidkey='Asia/Kolkata',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US',
                          // UserRoleID = role.id,
                            IsActive = TRUE
                           );
    
           insert usr ;
        system.runAs(usr) {
             Account acc = new Account();
              acc.Pan_Form_No_60__c = 'Pan';
              acc.First_Name__c = 'test';
              acc.Last_Name__c = 'test';
              acc.AADHAAR__c = '2134 5678 9012';
              acc.Name = 'Central Team';
              acc.Mobile_Number_1_Primary__c = '9090909090';
               acc.Gender__c = 'Male';
               acc.Personal_Email_For_Commn__c = 'test@gmail.com';
               acc.PAN_Number__c = 'BAKPJ4825M';
              acc.AADHAAR_TOKEN__c = 1234567890;
             insert acc;
            
        Case cas = new Case();
          // cas.Loan_Application__c = oppRec.id;
            cas.PinCode__c = 123456;
             cas.CPV_Decision__c = 'Waived';
             cas.FICO_Status__c = 'Green';
            // cas.FI_Agency_Account__c = acc.id; 
            // cas.FI_Agent__c =  usrcomm.id;
           insert cas;
        }
    }
    public static testmethod void shareCaseRecordwithAgentTest(){
        
        case cas= [select id from case limit 1];
        user usr = [select id from user where IsActive = TRUE and Profile.name = 'System Administrator' limit 1];
        Account acc=[Select id from Account limit 1];
        system.runAs(usr){
         string soobj = '';
        Map<String,String> usercaseMap = new Map<String,String>();
           usercaseMap.put(String.valueOf(cas.id),String.valueOf(usr.id));
        string access = 'Read';
        string reason = 'Manual';
        
         SharingUtility share = new SharingUtility();
         SharingUtility.shareCaseRecordwithAgent(soobj,usercaseMap,access,reason);
            
          string sobj ='Case';
            String parentId = String.valueOf(cas.id);
            String userId = String.valueOf(usr.id);
         SharingUtility.caseShareRecord(soobj,parentId,userId,access,reason);
             string sobjAcc ='AccountShare';
            string accaccess = 'Edit';
             String accparentId = String.valueOf(acc.id);
        SharingUtility.accountShareRecord(sobjAcc,accparentId,userId,accaccess,reason);
            string sobjitem ='DocumentChecklistItemShare';
         //  SharingUtility.docCheckItemShareRecord(sobjitem,accparentId,userId,accaccess,reason);  
            
    }
    }
}