You need to sign in to do that
Don't have an account?
trigger while loop formula field Update
Any efficient way other than this?
trigger AutoAssignOpportunityNumber_AutoNoObj on Opportunity (before insert) {
for (Opportunity o : Trigger.new)
{
// Iterate over each sObject
AutoNo__c au = [select NextNo__c, Iterator__c from AutoNo__c where Category__c = 'Opportunity Number' LIMIT 1 FOR UPDATE];
if (o.Opportunity_Number__c == '' || o.Opportunity_Number__c == NULL)
{
while ([select count() from Opportunity where Opportunity_Number__c =:au.NextNo__c] > 0) {
au.Iterator__c = au.Iterator__c + 1;
// reflush the formula field
update au;
au = [select NextNo__c, Iterator__c from AutoNo__c where Category__c = 'Opportunity Number' LIMIT 1 FOR UPDATE]; <--- MUST RELOAD???
}
o.Opportunity_Number__c = au.NextNo__c;
au.Iterator__c = au.Iterator__c + 1;
update au;
}
}
}