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
Richard DwyerRichard Dwyer 

Apex Trigger is not changing the status from Notice to Inactive on Custom Object

Hi Team,

I have a code written to change the Status of the record from Notice Period to Inactive for custom object 'Enrollments'. The code is https://eu3.salesforce.com/setup/build/viewApexClass.apexp?id=01p20000000HVDd

However since November this is not working as there are lot of Notice period records that are ot changing the status to inactive in the link: https://eu3.salesforce.com/setup/build/viewApexClass.apexp?id=01p20000000HVDd

I tried to debug the code but could not find any reason why its not firing. I would like to know which part of that trigger is causing the issue. Test record t replicate is https://eu3.salesforce.com/0032000000xVJEQ

Please help to find which part of the code is causing the issue in not obtaining the record status.

Provided login access for salesforce.com. Any help would be highly appreciated!!
Roy LuoRoy Luo
The link to your org will go nowhere without login info. It is better to post code snippet here.
Richard DwyerRichard Dwyer
Hi Roy,

Thanks for your insight here. I have given the login access for Salesforce.com support and so thought you might logging in with Blacktab access.

The code is mentioned below:

global class batchSetClassInactiveAfterLastClassDate Implements Database.Batchable<sObject>{

global String qry;

global batchSetClassInactiveAfterLastClassDate(String thisqry){
  qry = thisqry;
}

global Database.QueryLocator start(Database.BatchableContext bc) {  
  Date dt = Date.today();
  return Database.getQueryLocator(qry);  
}
 
global void execute(Database.BatchableContext BC, List<sObject> enrol){
system.debug('############ enrol =' + enrol);
  
  set<ID> contIDs = new set<ID>();
  
  for (Enrollments__c thisenrol : (List<Enrollments__c>)enrol)
        contIDs.add(thisenrol.Contact__c);
           
  system.debug('############ contIDs =' + contIDs);
  
  Map<ID,Contact> thiscontMap = new Map<ID,Contact>([select id, Name,RecordTypeID  from Contact where id In :contIDs ]);
  
  system.debug('############ thiscontMap =' + thiscontMap);
  
  // fianally a map of all enrolments against the contacts
  
  
           
           
    for (Enrollments__c thisenrolment : (List<Enrollments__c>)enrol)
  {
    
    system.debug('############ thisenrolment =' + thisenrolment);
    
    system.debug('############ thisenrolment is after last class date, so should be set to Inactive');
    thisenrolment.Status__c = 'Inactive';
    
    system.debug('############ thisenrolment now=' + thisenrolment);
           
  }
  update enrol;
  
  
  
  // last payment date
  system.debug('############ no look for all enrolments that should be set 2 zero becasue teh last payment date is today');
  list<Enrollments__c> enrollastpaymentdatetoday = [select Id, Status__c, Last_Payment_Date__c, Contact__c From Enrollments__c 
        where Status__c = 'Notice Period' and  Last_Payment_Date__c <= :Date.today()];
  
  system.debug('############ enrollastpaymentdatetoday =' + enrollastpaymentdatetoday);
  update enrollastpaymentdatetoday;
  
  system.debug('############ enrollastpaymentdatetoday after update=' + enrollastpaymentdatetoday);
      
  
  
        
    //set to contact type Ex-Student  
    Map<ID, Set<String>> enrollTypeMap = new Map<ID, Set<String>>();
    
    list<Enrollments__c> erolls = [select Id, Status__c, Last_Class_Date__c, Contact__c From Enrollments__c where Contact__c In :contIDs];
    Map<Id, list<Enrollments__c>> allEnrollToContactMap = new Map<Id, list<Enrollments__c>>();
    list<Enrollments__c> enrollLst;
    for (Enrollments__c eachEnroll : erolls){
    if (allEnrollToContactMap.get(eachenroll.Contact__c) == null)
      enrollLst = new list<Enrollments__c>();
    else
      enrollLst = allEnrollToContactMap.get(eachenroll.Contact__c);
    
    enrollLst.add(eachenroll);
    allEnrollToContactMap.put(eachenroll.Contact__c, enrollLst);      
    }
    system.debug('############ erolls =' + erolls);
    //for (Enrollments__c erolls : [select Id, Status__c, Last_Class_Date__c, Contact__c From Enrollments__c where Contact__c In :contIDs]){
           
      //Set<String> enrollTypeSet;
      //if (enrollTypeMap.get(erolls.Contact__c) != null){
      //  enrollTypeSet = enrollTypeMap.get(erolls.Contact__c);  
      //}
      //else{
      //  enrollTypeSet = new Set<String>();
      //}
      //enrollTypeSet.add(erolls.Status__c);
    //}
    
    
    //system.debug('############ enrollTypeSet =' + enrollTypeSet);
    //Ex-Student
  Utils.convertToExStudent(thiscontMap.Values(), allEnrollToContactMap);    
    
    //update thiscontMap.Values();  
}

global void finish(Database.BatchableContext BC){
}

Please let me know if you have any further questions.


Thanks & Regards,
Richard Dwyer
Roy LuoRoy Luo
How do you run this batchable code? Is it from a scheduled apex job? Likely the issue is in the caller. 
Richard DwyerRichard Dwyer

Hi Roy, I don't know.
Are you able to log into my org to take a look?
I have given the login access for Salesforce.com support and so you are able to log in with Blacktab access.