• balakrishna mandula 8
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
Hi I am new to writing test classes. I am now writing test class for the below schdulable class. 
My Req:

Parent: Incident  
Child: Task

If Status of the Incident is 'RESOLVED' after 5 days this status must be changed to 'CLOSED'. condition is all the status status must also be 'CLOSED'. If task status is other than 'CLOSED' batch apex will change this to 'CLOSED'. 

Below is Batch apex and schedule apex:

global class UpdateStatusToClosed implements Database.Batchable<SObject>, Schedulable{
    
    //Variable declarations
    Date dateBeforeFiveDays;
    String currentStatus;
    Status__c closedStatus;
    List<Task__c> taskBMC;
    Stopping_Manually_Closed_VR_in_Batch_Ape__c stopManuallyClosedVR; //custom setting field
    
    //Schedule execute
    global void execute(SchedulableContext sc){
        Database.executeBatch(new UpdateStatusToClosed(false),10);
    }
    
    global UpdateStatusToClosed(boolean testMode){
        this.testMode = testMode;
    }
    
    global boolean testMode;    
    
    global UpdateStatusToClosed(){
        //Used to update the child records
        taskBMC = new  List<Task__c>();
        //Used to query resolved status incident records
        currentStatus = 'RESOLVED';
        //Retrieving closed status record from status object
        closedStatus = new Status__c();
        closedStatus = [SELECT Id, Name FROM Status__c WHERE Name = 'CLOSED' LIMIT 1];
        stopManuallyClosedVR = [SELECT Id, Bypass_Validation_Rule__c FROM Stopping_Manually_Closed_VR_in_Batch_Ape__c LIMIT 1];        
        system.debug('Closed Status Id '+closedStatus.Id);
        //substracting days from current date
        dateBeforeFiveDays = system.Today().addDays(-5);
        system.debug('Present Date '+system.Today());
        system.debug('Date After Sub Days '+dateBeforeFiveDays);        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        //Quering and retrieving incident records based on current status and dateBeforeFiveDays variables 
        system.debug('HI START');        
        return Database.getQueryLocator([SELECT Id, Name, FKStatus__C,StatusChangeDate__c,
                                         (SELECT Id, Name, FKStatus__r.Name
                                          From Tasks__r 
                                          WHERE FKStatus__r.Name != 'CLOSED') 
                                         FROM Incident__C 
                                         WHERE FKStatus__r.Name =:currentStatus AND DAY_ONLY(StatusChangeDate__c) =:dateBeforeFiveDays]);
        
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        system.debug('HI EXECUTE');
        //Casting sobject type        
        List<Incident__C> incList = (List<Incident__C>)scope;
        system.debug('Retrieved List '+incList+' and size is   '+incList.size());
        //Updating the incident status from RESOLVED to CLOSED by assigning closedStatus.Id to reference field
        for(Incident__C inc : incList){
            if(inc.Tasks__r.size()>0){
                for(Task__c taskList : inc.Tasks__r){
                    taskList.FKStatus__c = closedStatus.Id;
                    taskBMC.add(taskList);
                }   
            }
            system.debug(' Incident Before Update '+inc);
            inc.FKStatus__C = closedStatus.Id;
            system.debug('Incident After Update '+inc);
        }
        //Updating the incidents anf tasks
        if(incList.size()>0){
            stopManuallyClosedVR.Bypass_Validation_Rule__c = TRUE;
      update stopManuallyClosedVR;            
            if(taskBMC.size()>0){
                update taskBMC; 
                system.debug('Updated Tasks '+taskBMC);                
            }
            else{
                system.debug('Nothing to update the Task records');
            }            
            update incList;
            system.debug('Updated Incidents '+incList);            
        }
        else{
            system.debug('Nothing to update the Incident records');
        }
        stopManuallyClosedVR.Bypass_Validation_Rule__c = FALSE;
        update stopManuallyClosedVR;
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
}

Test class I tried writing is:

@isTest
public class UpdateStatusToClosed_TC{
  static testMethod void batchMethod(){
    Incident__c inc = new Incident__C ();
    Action__c act=new Action__c();
    act.Name='Client Note';
    Database.insert(act,false); 
    
    inc.FKStatus__c='a23g0000000HDTO';
    inc.IncidentType__c='Incident';
    inc.FKCategory__c='a17g0000001Uh0k';
    inc.incidentDescription__c='Test';
    inc.FKImpact__c='a1Jg00000013MkH';
    inc.FKUrgency__c='a2Ag0000001GUr0'; 
    inc.StatusChangeDate__c = Date.today();
    
    inc.Zone__c='APAC';       
        
    Database.insert(inc,false);
    
    Task__c task = new Task__c();
    task.FKIncident__c = inc.Id;
    task.Client_Name__c = 'Test Task';
    task.FKStatus__c = 'a23g0000000HDTO';
    
    
    Database.Insert(task,false);
    
    //Schedule the test job
    
    Test.startTest();
      BMCRemedyforce_UpdateStatusToClosed incSt = new BMCRemedyforce_UpdateStatusToClosed();
      Database.executeBatch(incSt);
    Test.stopTest();
    

  }
}

I know I have to write much code here. Pls help me write this

Thanks in advance
 
Hi 

Could you please help me to write test class for after update trigger? I know this is simple test class but I am new to coding.

Scinario:

Parent object: Incident
Child Object: Task

there is checkbox field call Task_Updated__c on both parent and child. if any of the child's checkbox field is true then parent checkbox field Task_Updated__c to be TRUE

trigger UpdateInc_On_update_Task on Task__c (after update) {
   set<Id> incId = new set<Id>();
  
   List<Incident__c> incList = new List<Incident__c>();
   List<Incident__c> incheckUpdateList = new List<Incident__c>();
   List<Incident__c> inUncheckUpdateList = new List<Incident__c>();
   
   
  
  
   
   for(Task__c tsk : trigger.new){
      incId.add(tsk.Incident__c);
   }
   
   List<Incident__c> incd = [Select Id, Name, Task_Updated__c, (Select Id, Name, Task_Updated__c from Tasks__r where Task_Updated__c = true) from Incident__c where Id =:incId];
   for(Incident__c ind : incd){
     if(ind.Tasks__r.size()>0){
       ind.Task_Updated__c = true;
       incheckUpdateList.add(ind); 
     } 
     else{
       ind.Task_Updated__c = false;
       incheckUpdateList.add(ind);
     }
      
   }
   if(incheckUpdateList != null)
   Database.SaveResult[] srlist = Database.update(incheckUpdateList,false);
  
    
    
}
Hi

I have created one approval process which updates the picklist field to "Published" and after changing it's value to "pubished" one more checkbox field to be selectable if picklist value is other than "Published" then checkbox field can't be checked or unchecked.

one more requirement in this:

picklist field value "published" can't be moved manually. with only approval process it should be possible.

Thanks in advance
Hi,
 I am trying to imlement validation rule on one check box field say "Yes" that will only be selectable if another field say "Status" is "Published"
Please help me on this

Thanks in advance

 
Hi 

Could you please help me to write test class for after update trigger? I know this is simple test class but I am new to coding.

Scinario:

Parent object: Incident
Child Object: Task

there is checkbox field call Task_Updated__c on both parent and child. if any of the child's checkbox field is true then parent checkbox field Task_Updated__c to be TRUE

trigger UpdateInc_On_update_Task on Task__c (after update) {
   set<Id> incId = new set<Id>();
  
   List<Incident__c> incList = new List<Incident__c>();
   List<Incident__c> incheckUpdateList = new List<Incident__c>();
   List<Incident__c> inUncheckUpdateList = new List<Incident__c>();
   
   
  
  
   
   for(Task__c tsk : trigger.new){
      incId.add(tsk.Incident__c);
   }
   
   List<Incident__c> incd = [Select Id, Name, Task_Updated__c, (Select Id, Name, Task_Updated__c from Tasks__r where Task_Updated__c = true) from Incident__c where Id =:incId];
   for(Incident__c ind : incd){
     if(ind.Tasks__r.size()>0){
       ind.Task_Updated__c = true;
       incheckUpdateList.add(ind); 
     } 
     else{
       ind.Task_Updated__c = false;
       incheckUpdateList.add(ind);
     }
      
   }
   if(incheckUpdateList != null)
   Database.SaveResult[] srlist = Database.update(incheckUpdateList,false);
  
    
    
}
Hi,
 I am trying to imlement validation rule on one check box field say "Yes" that will only be selectable if another field say "Status" is "Published"
Please help me on this

Thanks in advance