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
AlpsPSO CommunityAlpsPSO Community 

Not able to save Apex class scheduled.

Hi Everyone,

 

I ma fcaing a surprising issue while saving my apex class scheduled. The code worked great in my sandbox but when i tried to migrate it to production. It didn,t allowed me to save the code.

 

Below is my simple code :

 

global class NotificationScheduledFor40Hours implements Schedulable {

   
        private final List<Id> contactids = new List<Id>();
        private final List<Id> userIds = new List<Id>();
        private final List<User> userList = new List<User>();
        private final List<String> emailAddressList = new List<String>();   
   
        global void execute(SchedulableContext ctx) {
   
        List<User>  u = [SELECT id,ProfileId,FirstName,Email,Total_hours_of_week__c FROM User WHERE User.Total_hours_of_week__c <: 40.00 limit 1000];
       
        if(! u.isEmpty()){
           
            system.debug('u is :: ' + u);

             for(Integer i=0;i<u.size();i++)
             {
                User u1 = u[i];                
                 
                System.debug('u1::' + u1);

            }

      }

 }

}

 

The code is working fine in Sandbox. But for production it start giving error at line - if(! u.isEmpty()){ } and do not reflect any logical calculation. I mean to say i am not able to use any variable attribute inside this - global void execute(SchedulableContext ctx) {} method.

 

Please help me.

 

kiranmutturukiranmutturu

what is the error that u r getting at that line?

 

 

UVUV

Can you post the error as same code is working fine at my end.

AlpsPSO CommunityAlpsPSO Community

Exactly no specific error i am getting. But i am getting yellow color in my eclipse which is not polling my code to Production.

 



AlpsPSO CommunityAlpsPSO Community

 Oh is it. But Exactly no specific error i am getting. But i am getting yellow color in my eclipse which is not polling my code to Production. Please see below image : . My code is not polled on using code if(! u.isEmpty()){}

 



kiranmutturukiranmutturu

actually this is not an error this is a warning ..

may be this class is already scheduled in the production environment...then you have to stop the scheduled class and have to migrate....

AlpsPSO CommunityAlpsPSO Community

Thanks Kiran. But this is the entire new class i m working wiht. I just checked my Scheduled Jobs but there is no related dependencies for this class.

 

Also, coul you please let me know - do i need a test calss at this time i mean just to check if(! u.isEmpty()){}. I don't feel we require that. Also please let  me know what about cronkit, also that is alos not mandatory. Right? Moreover if you could provide me sample of this code working at your end along with a test class. That will be a real help.

 

Thanks,

Nitasha

UVUV

 

If there would be any batch/schedule class/report running in the production then you won't be able to deploy your code.

You should wait for the job completion and then deploy it.

AlpsPSO CommunityAlpsPSO Community

U r right.. But i m just saving my code in Production. Not yet scheduled. Moreover i catched the point where i m doing worng. Its saying my organization is having 72% code coverage but i need 75% to save my code.  Can you please help me now how can i increase that?

AlpsPSO CommunityAlpsPSO Community

I catched the point. Its saying my organization is having 72% code coverage however i need it 75 % to poll my code to production. Can you please now help me how can i increase it? i tried to write a test case for teh same as below. But its didn't worked.

kiranmutturukiranmutturu

u have to find the what are all the classes that are not having min 75% code coverage and try to acheive minimum of 75% for all classes....

AlpsPSO CommunityAlpsPSO Community

Hi Kiran,

 

I hv my schedueld class and test class as below : But its not working. Cna you help me out with this?

 

/*******************************************************************************************************************************

 

global class NotificationScheduledFor40Hours implements Schedulable {

   
        private final List<Id> contactids = new List<Id>();
        private final List<Id> userIds = new List<Id>();
        private final List<User> userList = new List<User>();
        private final List<String> emailAddressList = new List<String>();   
   
        global void execute(SchedulableContext ctx) {
   
        List<User>  u = [SELECT id,ProfileId,FirstName,Email,Total_hours_of_week__c FROM User WHERE User.Total_hours_of_week__c <: 40.00 limit 1000];
       
        if(! u.isEmpty()){
           
            system.debug('u is :: ' + u);

             for(Integer i=0;i<u.size();i++)
             {
                User u1 = u[i];                
                 
                System.debug('u1::' + u1);
               
                String userFirstName = u1.FirstName;
               
                String conultantProfileID = '00e80000000l0tQAAQ';               
                String projectManagerProfileID = '00e80000000l14jAAA';
               
                String userProfileID = u1.ProfileId;
               
                if ( ( userProfileID != null || userProfileID != '') && ( conultantProfileID.equals(userProfileID) || projectManagerProfileID.equals(userProfileID)) ) {
                userIds.add(u1.id);       
               
                userList.add(u1); 
                String emailString = u1.Email;
                emailAddressList.add(emailString);    
                //Id contactID = u1.ContactId;
                //System.debug('contactID ::' + contactID);
                System.debug('emailString ::' + u1.Email);
       
                
                 String[] toAddressList = new String[]{};
   
                for(Integer j=0;j<emailAddressList.size();j++){
                    String emailID = emailAddressList.get(j);
                    toAddressList.add(emailID);
                }
                
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                //String toaddress = toAddressList.get(i);
                mail.setToAddresses(toAddressList);
                mail.setsubject('Less Than 40 Hours for Last Week.');
                mail.setHtmlBody('You have less than 40 hours entered in Salesforce.com for last week.  Please update your hours or enter PTO.  If you have questions or are unable to enter your hours, please contact your System Administrator.');
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                system.debug('Email sent to :: ' + mail);
               
                u1.put('Total_hours_of_week__c',0.00);
                 update u1;
               }
              }
            }                                  
     }
}

 

/*******************************************************************************************************************************

 

The test class i hv developed is :

 

/*******************************************************************************************************************************

@istest
private class EmailTestClass {

static testmethod void EmailTest(){
Profile profile = [select id from profile where name='Standard User'];


for(User u : [SELECT id,ProfileId,FirstName,Email,Total_hours_of_week__c FROM User WHERE User.Total_hours_of_week__c <: 40.00 limit 1]){
            System.debug('u::' + u);           
            String userFirstName = u.FirstName;           
            String userProfileID = u.ProfileId;  
            String emailString = u.Email;                  

               
                String conultantProfileID = '00e80000000l0tQAAQ';               
                String projectManagerProfileID = '00e80000000l14jAAA';
               
                //String userProfileID = u.ProfileId;
               
                if ( ( userProfileID != null || userProfileID != '') && ( conultantProfileID.equals(userProfileID) || projectManagerProfileID.equals(userProfileID)) ) {
                //userIds.add(u1.id);       
               
                //userList.add(u1); 
                //String emailString = u.Email;
                List<String> emailAddressList = new List<String>();
                emailAddressList.add(emailString);    
                //Id contactID = u1.ContactId;
                //System.debug('contactID ::' + contactID);
                System.debug('emailString ::' + u.Email);
       
                
                 String[] toAddressList = new String[]{};
   
                for(Integer j=0;j<emailAddressList.size();j++){
                    String emailID = emailAddressList.get(j);
                    toAddressList.add(emailID);
                }
                
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                //String toaddress = toAddressList.get(i);
                mail.setToAddresses(toAddressList);
                mail.setsubject('Less Than 40 Hours for Last Week.');
                mail.setHtmlBody('You have less than 40 hours entered in Salesforce.com for last week.  Please update your hours or enter PTO.  If you have questions or are unable to enter your hours, please contact your System Administrator.');
                //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                system.debug('Email sent to :: ' + mail);
               
                u.put('Total_hours_of_week__c',0.00);
                 update u;
     
       
}
}
}
}

/*******************************************************************************************************************************