I have a method that creates an invoice, in addition, I have another object that I use to store whether the invoice has been created. I have an issue when executing my method in the anonymous apex console. The issue is that when executing multiple (clicking multiple) times my method in the console apex will create multiple invoices. This should not happen because this extra object that I have to check whether the invoice is created is updated inside the method.
class MyClass {
public void createInvoice(){
if(!Util.isInvoiceInserted()){
Util.setInvoiceStateToInserted(true);
new Invoice__c invoice = new Invoice__c()
insert invoice;
}
}
}
class Util(){
static Booelan isInvoiceInserted(){
InvoiceLog__c invoiceLog = [SELECT isInvoiceCreated__c FROM InvoiceLog__c LIMIT 1];
return invoiceLog.isInvoiceCreated__c;
}
static void setInvoiceStateToInserted(Booelan value){
InvoiceLog__c invoiceLog = new InvoiceLog__c(isInvoiceCreated__c = value);
update invoiceLog;
}
}
How can I just run my method once even if I execute my method multiple times in the anonymous console?
Util
class?Util.isInvoiceInserted()
. That makes me think this is kind of an x-y problem and you could potentially redesign the whole process, but you'd need to share some more detail on what this is aiming to do.