• Cameron Seitz
  • NEWBIE
  • 25 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 6
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 10
    Replies
I currently am only able to get 50% coverage on a test class that I am writing for some Batch Apex that I have written. The issue is that there are a number of lookups/relationships between objects when attempting to create the test records. Specifically  I am in a time crunch and the batch class does not edit anything particularly important or detrimental to the record. It only queries some relevant records/stipulations and changes a checkbox field on the jstcl__TG_Timesheet__c object that is there purely for tracking. How would I go about getting full coverage? Whether that's a mock or not I don't mind, I just have to get this pushed to production quickly. Thank you so much in advance. Here is my Batch Apex:
 
global class placementTimecardAuditFlag implements Database.Batchable<sObject>, Database.Stateful {

  List<jstcl__TG_Timesheet__c> query = [SELECT jstcl__Placement__r.Name FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c ='Active' AND jstcl__Placement__r.jstcl__Timesheet_Period__c != 'Weekly Split' AND jstcl__Week_Ending__c = LAST_N_DAYS:15 AND jstcl__Status__c = 'Pending'];
    Set<String> encounteredNames = new Set<String>();
    Set<String> duplicateNames = new Set<String>();

global Database.QueryLocator start(Database.BatchableContext bc) {
        for(jstcl__TG_Timesheet__c item : query){
    if(encounteredNames.contains(item.jstcl__Placement__r.Name)){
        duplicateNames.add(item.jstcl__Placement__r.Name);
    }
    else{encounteredNames.add(item.jstcl__Placement__r.Name);}
    }
      return Database.getQueryLocator('SELECT Id,Checkbox1__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.Name IN :duplicateNames');}
      

 global void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> a){
for(jstcl__TG_Timesheet__c b : a) {
           b.Checkbox1__c = True;
        }
update a;

    }
global void finish(Database.BatchableContext BC){
    
    
}

}

Here is my test that gets 50% and an error on the method that states:
"System.QueryException: Use query() for non-count queries | Class.placementTimecardAuditFlagTest2.TimecardTestMethod: line 15, column 1" 
 
@istest 
class placementTimecardAuditFlagTest {
 
static testmethod void TimecardTestMethod(){
    jstcl__TG_Timesheet__c ts = new jstcl__TG_Timesheet__c();
    ts.Checkbox1__c = True;

Test.startTest();
	placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
    DataBase.executeBatch(obj); 
           
Test.stopTest();
System.AssertEquals(database.countquery('SELECT TCEDaudit__c FROM ts2__Placement__c'),1);
    }
   
}


 
I'm currently trying to write a SOQL Query to pull all Accounts that have > 5 related Contact objects. How do I go about counting related objects? 
I'm receiving an error when attempting to run my test class. jstcl__Placement__c is a lookup who's parent is ts2__Placement__c which is why I construct it initially.

Error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ts2.Placement: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.ts2.Placement: line 14, column 1: []
 
@istest
public class placementTimecardAuditFlagTest{
    
     static testMethod void myTest() {
         
		Contact a= new Contact(Lastname = 'Test');
		insert a;
        
        ts2__Placement__c b= new ts2__Placement__c();
        insert b;
 
// Create common test timesheets
        List<jstcl__TG_Timesheet__c> testTimesheets = new List<jstcl__TG_Timesheet__c>();
        for(Integer i=0 ;i <200;i++) { 			
            testTimesheets.add(new jstcl__TG_Timesheet__c(jstcl__Week_Ending__c = Date.today()-15, jstcl__Consultant__c = a.Id,jstcl__Placement__c=b.Name));
        }
        insert testTimesheets;       

         
// Create common test placements
        List<ts2__Placement__c> testPlacements = new List<ts2__Placement__c>();
        for(Integer i=0 ;i <200;i++) {
            testPlacements.add(new ts2__Placement__c(ts2__Status__c = 'Active'));

        }
        insert testPlacements;        
//Testing
    Test.startTest();  
        
      placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
        System.AssertEquals(database.countquery('SELECT TCEDaudit__c FROM ts2__Placement__c'),1);
    }}




 
I currently am working on creating a Scheduled Apex Batch Class that pulls all jstcl__Placement__r.Name that exist on Timesheet__C that currently exceed 14 days since they have had time entered AND jstcl__Placement__r.ts2__Status__c = 'Active'. These are related objects that have some field relationships that I'm using.
It then takes that list of Placement Names from the Timesheet and the execute portion of the batch class needs to determine if there are duplicates within that list and + 1 to the placement.TimecardEnddateAudit__c field (picklist) of any duplicate record returned. What I have adds +1 to ALL the returned placements which isn't quite there. (or maybe that's entirely wrong too) Here is what I have so far. Any help is much appreciated. Appologies if my explaination is rough. 
 
global class timecardEndDateAudit implements Database.Batchable<sObject>, Database.Stateful {

  global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT jstcl__Placement__r.Name, Primary_Recruiter__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c = \'Active\' AND jstcl__Week_Ending__c = LAST_N_DAYS:14 AND (jstcl__Status__c = \'Pending\')';
        return Database.getQueryLocator(query);
  }
  
  global void execute(Database.BatchableContext BC, List<sObject> placements){
        for (ts2__Placement__c placement : (List<ts2__Placement__c>) placements) {

            placement.TimecardEnddateAudit__c = placement.TimecardEnddateAudit__c + 1;
        }
       
        update placements;
  }
  global void finish(Database.BatchableContext BC){ }
}

 
When I attempt to save my Apex Class I receive the error "Error: Compile Error: Unexpected token 'class timecardEndDateAudit implements Database.Batchable'. at line 3 column 8"
I can't seem to figure out what I'm missing here. 

gobal class timecardEndDateAudit implements Database.Batchable<sObject>,Database.Stateful{

global Database.QueryLocator start(Database.BatchableContext bc){

String query = 'SELECT jstcl__Placement__r.Name , Account_Executive__c , jstcl__Placement__r.Timecard_End_Date_Audit__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Week_Ending__c >= LAST_N_DAYS:14'

return Database.getQueryLocator(query);
global void execute(Database.BatchableContext bc,List<Placement>scope){

for (Placement Placements :scope){
Placements.Timecard_End_Date_Audit__c ='1';
}
update scope;
}
global void finish(Database.BatchableContext bc){}
}
I have a field that has a picklist that offers the options "Y" or "N". I have a separate field that needs to be empty if the answer is Y and required if the answer is N to that previous field. 

This is the validation I have to make it required if "N": 

AND( 
ISPICKVAL(Bank_Full_Deposit_Flag_1__c , "N") 
)



The field that it is applied to is called "Bank_Deposit_Deduction_Amount_1__c"
I just can't seem to figure out the "Must be blank" part for "Y". Appologies if I'm missing something obvious here. I have been going through the guide and can't seem to find it.