1

I am trying to Schedule Class that will add a new contact under All Account every 2 min and also there should be a validation check that you can not have more than 05 Contacts under a single Account.

Updated

The Code I've written is working and creating a new Contact for every Account but now i want a Validation Check that will check weather Account has less than 5 Contact or not

I've tried with accList.Contacts.Size()<=5 but this is giving me Error FATAL_ERROR System.AsyncException: The Apex job named "ScheduleContactOnAccountEveryTenMin 2019-08-07 15:24:07" is already scheduled for execution.

Thanks

global class ScheduleContactOnAccountEveryTenMin implements schedulable
{
    global void execute(SchedulableContext scx)
    {
        List<Account> accList = [SELECT Id, Name FROM Account WHERE CreatedDate > 2019-07-26T13:10:24.000+0000];
        List<Contact> conList2 = new List<Contact>();

        for(Account accSobj2 : accList)
        {
            Contact conS = new Contact();
            conS.FirstName = 'Schedule Class';
            conS.LastName = ' Test';
            conS.AccountId = accSobj2.Id;
            conList2.add(conS);
        }

        // if(conList2.size()<=5){

        Insert conList2;
        ScheduleContactOnAccountEveryTenMin.start();
        // abort Job and start again
        System.abortJob( scx.getTriggerId() );

        /*}
        else
        {
            System.abortJob( scx.getTriggerId() );
        }*/

    }
    public static void start()
    {
        // start keepalive again in 5 mins
        Datetime sysTime = System.now().addSeconds( 120 );  
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        System.schedule( 'ScheduleContactOnAccountEveryTenMin ' + sysTime, chronExpression, new ScheduleContactOnAccountEveryTenMin() );
    }
}
  • What is the error you are getting? Can you please mention that. – maniac coder Aug 7 '19 at 9:48
  • I am not getting any Error, but Job is also not getting created – Dave Aug 7 '19 at 9:52
  • 2
    Is there anything on debug log? Please note that scheduler class fails if the record size is more than 50k. I would suggest to use Batch class and use Schedule class to schedule the batch job. – maniac coder Aug 7 '19 at 10:13
  • What is the business need leading to this implementation? – David Reed Aug 7 '19 at 12:32
  • I am implementing this to get more knowledge about Asynchronous Apex, I've recently started Asynchronous Apex – Dave Aug 7 '19 at 12:41
2

In my opinion, this is not an ideal test of your Anonymous Apex skills because it does not represent the kind of business functionality you're likely to build using those features. That said:

    // if(conList2.size()<=5){

This conditional is in the wrong place. If you don't want to add more than five total Contacts on any Account, you need to query for the Contacts associated with each Account and then check the size of that list before you add more. You do this using a subquery:

    List<Account> accList = [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE CreatedDate > 2019-07-26T13:10:24.000+0000];
    List<Contact> conList2 = new List<Contact>();

    for(Account accSobj2 : accList)
    {
        if (accSobj2.Contacts.size() < 5) {

Next, you're aborting your current scheduled job after you try to schedule a new one, which is what produces the error

System.AsyncException: The Apex job named "ScheduleContactOnAccountEveryTenMin 2019-08-07 15:24:07" is already scheduled for execution

You need to do the abort before you call start().

Lastly, this type of functionality - which runs broadly across an sObject query likely to return a large number of results - in production should be implemented as Batch Apex. The inherent asynchrony of Batch Apex and its dependency on platform load and queue behavior would likely not permit you to run it every two minutes, something the platform generally doesn't support with that level of precision anyway.

  • Thanks @David Reed: I was doing if (accSobj2.Contacts.size() < 5) after Inserting the Contact. -- My bad And Thanks for clarifying Error System.AsyncException: The Apex job named " " is already scheduled for execution and for last comment - as I was getting in more depth of Schedule Apex(What can we do with Schedule Apex). I will keep in mind that in these type of scenario i've to implement Batch Apex I will also try this to implement with Batch Apex also to understand Batch Apex At last Thanks Again for clearing my doubts Have a great day!! – Dave Aug 7 '19 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.