1

I'm trying to implement Andy in the Cloud's Unit of Work pattern within a queueable class so that I can handle the DML for the Milestones that I'm inserting & relating to the Projects, which are also being created by this class, more easily. The Milestones are the children of Projects.

I'm hitting several errors:

unexpected token: 'private'
unexpected token: 'static'
unexpected token: 'List'

for the line that begins private static List MY_SOBJECTS

&

Invalid type: SObjectUnitOfWork

for the line SObjectUnitOfWork uow = new SObjectUnitOfWork(MY_SOBJECTS);.

public class CreateImplementationProjects implements Queueable {

  private Set<Id> stage4PlusOpportunities = new Set<Id>();

  // constructor
  public CreateImplementationProjects (Set<Id> stg4OppIds) {
    this.stage4PlusOpportunities = stg4OppIds;
  }

  public void execute (QueueableContext context) {

    List<Opportunity> changedOpportunities;

    changedOpportunities = [SELECT Id,OwnerId,AccountId
                              FROM Opportunity
                             WHERE Id IN :stage4PlusOpportunities];

    public static List MY_SOBJECTS =
    new Schema.SObjectType[] {
      MPM4_BASE__Milestone1_Project__c.SObjectType,
      MPM4_BASE__Milestone1_Milestone__c.SObjectType
    };

    SObjectUnitOfWork uow = new SObjectUnitOfWork(MY_SOBJECTS);
    for(Opportunity o : changedOpportunities) {
      MPM4_BASE__Milestone1_Project__c project = new MPM4_BASE__Milestone1_Project__c();
        project.Name = o.Name;
        project.OwnerId = o.OwnerId;
    }
  }
}

Is it possible to use private & static collections in the queueable context?

And why is the compiler complaining about the SObjectUnitOfWork?


Edit To be clear, I have spent quite a while looking for the answers to those questions but can't find any information that's related, I'm not just posting this out of laziness!

3

You changed it to public in your code sample, but the principle is the same. When you are within a method definition, your syntax for declaring variables is different than at the class level.

At the class level, the syntax is for variable declaration is:

 access_modifier static_modifier variable_type variable_name;

You can optionally include assignment syntax at the end. This syntax is what you use for stage4PlusOpportunities, for example.

  • access_modifier is private
  • static_modifier is omitted, meaning it is an instance variable
  • variable_type is Set<Id>
  • variable_name is stage4PlusOpportunities

However, within a method declaration, neither access_modifier nor static_modifier are valid. So the syntax simply becomes:

variable_type variable_name

So proper declaration would be simply:

List<SObjectType> MY_SOBJECTS = new List<SObjectType> { ... };

Note that every List<T> must be specifically typed, in this case List<SObjectType>.


Since the information you are working with does not appear to be specific to the Queueable instance nor execution context, you should probably just move it to the class level.

public with sharing class MyClass implements Queueable
{
    public Set<Id> myOtherVariable = new Set<Id>();
    static final List<SObjectType> MY_SOBJECTS = new List<SObjectType> { ... }

    public void execute(QueueableContext context)
    {
        // implementation
    }
}

As for why the compiler complains that the SObjectUnitOfWork type is not defined, make sure the SObjectUnitOfWork class is actually pulled into your org.

  • Thanks again. I think utilising the Unit Of Work is probably overkill for my use case here. But the notes on syntax declaration will be really useful to refer back to & I will move my information to the class level. Not my greatest question.. – Alex S Aug 22 '17 at 13:20
2

You have to install FinancialForce Apex Common library and also ApexMocks that it dependant on.

Please reference the documentation from the above links to familiarise yourself with the functionality.

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.