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
fofofofo 

before insert trigger: apex variable in soql causes error?

Here is my code:

for (Orderc__c o : trigger.new){
if(o.Has_Promotion_Code__c == true && o.Promotion_Code__c !=null){
	Opportunity[] findOpp = [SELECT Name FROM Opportunity WHERE Voucher_Code__c = :o.Promotion_Code__c limit 1];
}
if(findOpp.size()==1){
	o.Ready_To_Print__c = false;
	o.Pay_Status__c 	= 'INVOICE';	
}
}

 error message on line 6: method does not exist or incorrect signature:findOpp.size()

Starz26Starz26
Opportunity findOpp[];

for (Orderc__c o : trigger.new){ if(o.Has_Promotion_Code__c == true && o.Promotion_Code__c !=null){ findOpp = [SELECT Name FROM Opportunity WHERE Voucher_Code__c = :o.Promotion_Code__c limit 1]; } if(findOpp.size()==1){ o.Ready_To_Print__c = false; o.Pay_Status__c = 'INVOICE'; } }
fofofofo

ok, Thanks.

why do I have to seperate the define and the soql?

 

bob_buzzardbob_buzzard

Scoping - when you define it inside the if statement, it is only scoped for that block of code.  Once you finish the if statement, the variable no longer exists.  By defining it at the outer level, its available until that outer level block finishes.