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
Inbox OutboxInbox Outbox 

code help please


public  class queuables implements  queueable{
   
    public void execute(QueueableContext q) {
        Account a= new Account(Name= 'queuable account');
        insert a;
    }
}


trigger QueueableTrgr on Account (before insert) {
if(trigger.isBefore){
    if(trigger.isInsert){
        system.enqueueJob( new queuables(trigger.new));
   
    }
}
}

Error: 
Constructor not defined: [queuables].<Constructor>(List<Account>)
Constructor not defined: [quclass].<Constructor>(List<Account>)

I have questions kind of related to this code as below
Thank you guys for the reply. 

1. I was wondering why we use conditional boolean context variables like isInsert, isbefore when we already mention the DML events (before insert) in the trigger parameter!
2. Can we track the queuable job progress from whithin the same trigger which invokes?
Best Answer chosen by Inbox Outbox
Suraj Tripathi 47Suraj Tripathi 47
Usually, For salesforce best practices, we have to write a code that works for single records as well as multiple records.
So we have to write a bulkify code that works for the single record as well as multiple records.
Because when you insert a record from User Interface, you can insert only one record.
But when you insert many records using a data loader, then your code should be working for all records which will be inserted by the data loader.

Thank you!

Regards,
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi, 
We use isBefore, after when there is more than 1 parameter in the trigger. 
Suppose there are 3 parameters in the trigger code. Ex:-
trigger QueTrgr on Account (before insert, after update, before update) {
and now we need to call some apex when the trigger is working in insert and need to solve other conditions when we trigger fire's on update. Then how we will be able to differentiate the work. In these scenarios, we use this condition i.e isBefore, isInsert, isUpdate.
These condition helps trigger to specifically work on the process that needs to be done depending upon the situation the trigger is fired.

Now coming to your 2nd query i.e can we track the queuable job from the same trigger which invoked them.
No, We cannot track them as they are queued and when the processor is free the queuable jobs are executed.
And, hence their logs are also separately generated, i.e different log is generated when the queuable job is executed other than the trigger log.

Hope this will help you.
If this helped you please mark it as the best answer.
Thank you!

Regards 
Suraj Tripathi
Inbox OutboxInbox Outbox
Suraj Tripathi 47,

Thank you for the reply. It clears so many things for me. 

Can you help me with the above code? I am getting the error mentioned above (constructor), I can't seem to figure that out. 
 
Suraj Tripathi 47Suraj Tripathi 47
public  class queuables implements  queueable{
   private List<account> accList;
     public queuables(List<Account> a) {
           this.accList = a;
      }
    public void execute(QueueableContext q) {
     List<Account> insList = new List<Account>();
       for(Account a : this.accList){
         Account acc = new Account(Name= a.Name+'queuable account');
        insList.add(acc);
      }
       insert insList;
    }
}
trigger 
trigger QueueableTrgr on Account (before insert) {
if(trigger.isBefore){
    if(trigger.isInsert){
        system.enqueueJob( new queuables(trigger.new));
   
    }
}
}
If you are trying to pass the parameter in the queuable class you should make a constructor of queueable class that will save the value in the variable then that variable can be used in the execute method.
Here is the link for your reference:-
https://salesforce.stackexchange.com/questions/246580/when-passing-in-a-collection-of-sobjects-to-a-queueable-what-values-are-used

If there is any confusion please mention.
If this helped you please mark it as a best answer.

Thank you!
Regards 
Suraj Tripathi.
Inbox OutboxInbox Outbox
Suraj Tripathi 47, 

Do we usually pass collection of sobjects as opposed to just one record?
I understand asynchronous apex is for bulk operations among many but can't I insert just one record as opposed to a collection? I am confused. 
Suraj Tripathi 47Suraj Tripathi 47
Usually, For salesforce best practices, we have to write a code that works for single records as well as multiple records.
So we have to write a bulkify code that works for the single record as well as multiple records.
Because when you insert a record from User Interface, you can insert only one record.
But when you insert many records using a data loader, then your code should be working for all records which will be inserted by the data loader.

Thank you!

Regards,
Suraj Tripathi
This was selected as the best answer