0

I'm attempting to run a query within a loop to fetch a record and place the value into a newly created record, but am getting an error when attempting the 2nd query. I only included a small portion of the class so it's less confusing.For some reason I'm getting an "Unexpected token 'bankAcc.id' error for the query starting with "Onboarding_Bank_Account__c Onbankacc".Anybody have any ideas on this ?

 for(Bank_Account__c bankAcc : [Select Id,Status__c,Program_Onboarding__c,Program_Supplier__c,Organization__c FROM Bank_Account__c WHERE Id IN: setProgramBakAccountUpdated]) {

       Onboarding_Bank_Account__c Onbankacc =  [SELECT Program_Bank_Account__c FROM Onboarding_Bank_Account__c WHERE 
Program_Bank_Account__c = bankAcc.id ORDER BY LastModifiedDate ASC NULLS FIRST LIMIT 1] ;

            // Create new Milestone for each Bank Account in the List
            Milestone__c newMilestone  = new Milestone__c();

            //Set Milestone Field Values
            newMilestone.Program_Bank_Account__c = bankAcc.Id;
            newMilestone.Program_Supplier__c = bankAcc.Program_Supplier__c;
            newMilestone.Account_Record__c = bankAcc.Organization__c;
            newMilestone.Organization_for_UI__c = bankAcc.Organization__c;
            newMilestone.Master_Record_Updated__c = true;
            newMilestone.Onboarding__c = Onbankacc.id;
2
  • 1
    You know LastModifiedDate can't be null, right? Also query in a loop = BAD.
    – Adrian Larson
    Mar 6, 2020 at 17:14
  • ah yes. I originally was sorting on another custom field. I forgot about that in there.
    – Zoom_v
    Mar 6, 2020 at 17:54

1 Answer 1

3

You're just missing a binding colon:

Onboarding_Bank_Account__c Onbankacc = [
    SELECT Program_Bank_Account__c 
    FROM Onboarding_Bank_Account__c 
    WHERE Program_Bank_Account__c = :bankAcc.id 
    ORDER BY LastModifiedDate ASC NULLS FIRST LIMIT 1
] ;

bankAcc.id --> :bankAcc.id

However, this looks like it can be and should be bulkified by making this query a subquery of your primary for query.

1
  • you da man ! Thanks David.
    – Zoom_v
    Mar 6, 2020 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .