1

I am attempting to distribute Leads from a list on a weekly basis. I have this code which somebody was kind enough to help me with :

global class WeeklyPreOppProcessor {
{
    global void execute (SchedulableContext scx)
    {

        Group group = [Select Id, Name FROM Group 
                       WHERE Type = 'Queue' 
                       AND Name = 'Distributed_Queue'];

        List<Lead> lstLead = [SELECT Id, Name, OwnerId 
                              FROM Lead 
                              WHERE Distributed__c = false LIMIT 50];
        for(Lead leadObj:lstLead)
        {
            leadObj.Distributed__c = true;
            leadObj.OwnerId = group.Id;
        }
        update lstLead;
    }

}

But this is producing an error : "Global variable must be contained in a global class...". I don't understand this since I have the class labeled as global.

Any ideas ?

3 Answers 3

2

You must declare variables as global. Earlier I have given this code. Just made small changes.

global class WeeklyPreOppProcessor implements Schedulable
{
    global Group grp;
    global List<Lead> lstLead;
    global void execute (SchedulableContext scx)
    {

        grp = [Select Id, Name FROM Group 
                       WHERE Type = 'Queue' 
                       AND Name = 'Distributed_Queue'];

        lstLead = [SELECT Id, Name, OwnerId 
                              FROM Lead 
                              WHERE Distributed__c = false LIMIT 50];
        for(Lead leadObj:lstLead)
        {
            leadObj.Distributed__c = true;
            leadObj.OwnerId = grp.Id;
        }
        update lstLead;
    }

}
9
  • I"m trying your code and am still getting a "global variable must be contained in a global class" error.
    – Zoom_v
    Commented Mar 23, 2017 at 21:28
  • There was an extra { on line 2. I was then getting a "Identifier name is reserved: group at line 6 column 15" error. Not sure what that is. I haven't gotten to the test script yet.
    – Zoom_v
    Commented Mar 23, 2017 at 21:37
  • Yha, that's what I did. And the group identifier apparently had to be changed to something else because it was a reserved name - so I changed it to group1 and it worked.
    – Zoom_v
    Commented Mar 23, 2017 at 21:43
  • 1
    Declaring the variables is completely unnecessary, as is the global accessibility modifier (public should suffice).
    – Adrian Larson
    Commented Mar 23, 2017 at 21:48
  • Yes, thats true, actually his issue is Group variable name is also group, that's why the issue was Commented Mar 23, 2017 at 21:50
1

You have an extra opening curly bracket ({). That's forcing everything inside of it to be considered an initializer block and really confusing the poor compiler.

global class MyClass {
    {
        // stateful initializer code
    }
}

Once you remove the extra curly bracket, you'll get a new compile fail:

Identifier name is reserved: group

Once you rename that (to something like distributedQueue), your code will be good to go. Note also in your class declaration that you forgot to implement the Schedulable interface. Also note that you don't need to use global and should almost never use it unless you're working on package development. So your proper class should look like:

public class Demo implements Schedulable
{
    public void execute (SchedulableContext scx)
    {

        Group distributedQueue = [
            SELECT Name FROM Group 
            WHERE Type = 'Queue' 
            AND Name = 'Distributed_Queue'
        ];
        List<Lead> records = [
            SELECT Name, OwnerId FROM Lead
            WHERE Distributed__c = false LIMIT 50
        ];
        for(Lead record : records)
        {
            leadObj.distributed = true;
            leadObj.OwnerId = distributedQueue.Id;
        }
        update records;
    }
}
4
  • I have been setting up scheduled jobs for this. It runs but it doesn't make the changes. I am using it on a custom object which I don't think should be a problem. I even took out the part of assigning the Owner in case that was a problem. It still won't make any changes. Any ideas ?
    – Zoom_v
    Commented Mar 24, 2017 at 1:17
  • @Zoom_v Sounds like a separate question!
    – Adrian Larson
    Commented Mar 24, 2017 at 1:17
  • ok - I didn't know if it would have been considered wrong to start a new thread. Thanks.
    – Zoom_v
    Commented Mar 24, 2017 at 1:42
  • @Zoom_v The reason this code failed to compile is very different than the reason the code doesn't update your records and requires different investigative/reproduction measures.
    – Adrian Larson
    Commented Mar 24, 2017 at 1:50
0

Let's take an example

global class updateData implements Schedulable {   
global void execute (SchedulableContext SC) {       
    DataContact dContact = new DataContact();       
    dContact.UpdateContact();       
}       

}

It looks like you are trying to execute the similar code (code mentioned above)inside the Dev Console. The Developer Console does not support creating global classes. If you are just trying to anonymously call UpdateContact, then you can simply type this into the Developer Console:

DataContact dc = new DataContact();
dc.updateData();

If you are trying to create a schedulable object to schedule calls to DataContact then you should create an Apex class with the code that you posted and then schedule it using the 'Schedule Apex' button on the Apex Classes page.

1
  • J - I am not using the Dev Console. I'm just trying to create that class in the Apex Class section. I would like to be able to just schedule that Santanu's class with the Schedule Apex button if I could just get it to save. But I'm getting that global variable error.
    – Zoom_v
    Commented Mar 23, 2017 at 21:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .