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
AK_SmithAK_Smith 

How to make an apex class from the appex code

Hello!
I need to make a new apex class from the code below. My goal to execute this code on a monthly basis using "shedule apex" button.
my code is:
list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE];
update AllRecords;

Could anyone help? 
Best Answer chosen by AK_Smith
Lokesh KumarLokesh Kumar
HI smith,

you can use like below code
global class Miko_Scheduled Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            doexecute();
        }

        public void doexecute()
        {
            list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
			AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE];
			update AllRecords;
		}
	}

afterward, you can go to Setup > Build > Develop > Apex Classes and then press on the 'Schedule Apex' to set up your class.

Thanks !

All Answers

Lokesh KumarLokesh Kumar
HI smith,

you can use like below code
global class Miko_Scheduled Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            doexecute();
        }

        public void doexecute()
        {
            list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
			AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE];
			update AllRecords;
		}
	}

afterward, you can go to Setup > Build > Develop > Apex Classes and then press on the 'Schedule Apex' to set up your class.

Thanks !
This was selected as the best answer
Ramssf70Ramssf70
Hi AK_Smith,

try the following code
public class myclass
{

public void getdata()
{

list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE];
update AllRecords;

}
}


if you want to excuete the class  you have to go to developer console and write the following code and click on excuete

steps for going developer console 

myclass obj = new myclass();
obj.getdata();
kindly let me know if you need any help
Thank you
AK_SmithAK_Smith
Thank you. But i got the error 
System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.TriggerHanglerActivity.copyDataToContactProductCategory: line 388, column 1 Class.TriggerHanglerActivity.onAfterUpdate: line 47, column 1 Trigger.ActivityTrigger: line 24, column 1

 
Lokesh KumarLokesh Kumar
HI smith can you please check is there any trigger written on the object PersonalGoals__c if yes can you please share so that i can check it. the error is not from this class it's on update event.

 
AK_SmithAK_Smith
No, only Process builder. 
but an error was  during Running Test: TestTriggerHanglerActivity.copyDataTest3
Could it be because of related objects? 

The PersonalGoals__c  is brand new and related only to cupple other objects
AK_SmithAK_Smith
YES! FIX IT! 

Thank you!
Lokesh KumarLokesh Kumar
HI Smith,

An error is because of class TestTriggerHanglerActivity and in method copyDataTest3.

Thanks !