0

I wrote this class that will trigger after an opportunity gets created but the class doesn't let me save as it comes up with a following error:

Loop must iterate over a collection type: Opportunity

The class goes as follows:

public with sharing class ClassSetPricebook {

public static void pbSet(Opportunity listOpportunity){


    ID ehpb = '01s20000000D23DF';
    ID euyp = '01s20000000dsfga';
    ID sp = '01s2000000051234DA';
    ID eu = '01s12000000U21fUIS';
    List<Opportunity> opps = [SELECT Id, Name, Quote_Bill_To__c FROM Opportunity Where id =: ApexPages.currentPage().getParameters().get('id') ]; 
    Id billid = null;
    Id qubito = opps[0].Quote_Bill_To__c;
    CreateQuoteBillTo__c[] quotedet = [SELECT Id, BillToCode__c  FROM CreateQuoteBillTo__c where Id = :qubito LIMIT 1];          
    System.debug('Opportunity Quote Bill to iD: ' + ' ' + opps[0].Quote_Bill_To__c );
    System.debug('Bill to code number: ' + ' ' + quotedet.get(0).BillToCode__c );

     if(!quotedet.isEmpty() && quotedet.size()>0){
        billid = quotedet.get(0).BillToCode__c;
        }


     for(Opportunity o : listOpportunity) {
        if (billid == '822109') {
            o.Pricebook2Id = ehpb;
        }
        else if (billid == '57124') {
            o.Pricebook2Id = euyp;
        } 
    }    

}
   }


the error is at line 22 where the for loop starts, does anyone know how to solve this issue?

1
  • Could you please show the line no. ? Please make sure listOpportunity is not null. And in for loop you're not making the use of o for iteration ?
    – user46657
    Commented Jul 11, 2017 at 11:17

1 Answer 1

1

In the third line you pass a variable into your method called listOpportunity. That variable isn't currently of the list type. Please try this code:

public with sharing class ClassSetPricebook {

public static void pbSet(List<Opportunity> listOpportunity){


    ID ehpb = '01s20000000D23DF';
    ID euyp = '01s20000000dsfga';
    ID sp = '01s2000000051234DA';
    ID eu = '01s12000000U21fUIS';
    List<Opportunity> opps = [SELECT Id, Name, Quote_Bill_To__c FROM Opportunity Where id =: ApexPages.currentPage().getParameters().get('id') ]; 
    Id billid = null;
    Id qubito = opps[0].Quote_Bill_To__c;
    CreateQuoteBillTo__c[] quotedet = [SELECT Id, BillToCode__c  FROM CreateQuoteBillTo__c where Id = :qubito LIMIT 1];          
    System.debug('Opportunity Quote Bill to iD: ' + ' ' + opps[0].Quote_Bill_To__c );
    System.debug('Bill to code number: ' + ' ' + quotedet.get(0).BillToCode__c );

     if(!quotedet.isEmpty() && quotedet.size()>0){
        billid = quotedet.get(0).BillToCode__c;
        }


     for(Opportunity o : listOpportunity) {
        if (billid == '822109') {
            o.Pricebook2Id = ehpb;
        }
        else if (billid == '57124') {
            o.Pricebook2Id = euyp;
        } 
    }    

}
   }
0

You must log in to answer this question.