0

i am getting this error in lin 12, how to pass multiple product code using list, can anyone please help me out.

Error--Error: Compile Error: Invalid bind expression type of OpportunityLineItem for column of type String at line 12 column 92

public class Procuct_Adding_To_Order{

public static void productOrder(List<OpportunityLineItem> oli){ 

   set<Id> setOppId = new set<Id>(); 

   for(OpportunityLineItem obj: oli){ 
        setOppId.add(obj.OpportunityId); 
   } 

   List<OpportunityLineItem> ProdCode = [Select ProductCode from OpportunityLineItem where id IN: oli];
   List<Product2> Prod_Nam = [Select id,Product_Abb__c from Product2 where ProductCode IN: ProdCode];
   List<opportunity> needed = [Select Master_Opportunity__c from opportunity Where Opportunity.id =: setOppId];
   List<OrderItem> orderIt = new List<OrderItem>();

    for(Order ord: [Select id from Order where opportunityId =: needed[0].Master_Opportunity__c])
      {
       for(OpportunityLineItem o : oli)
       {  
        for(Product2 pr : Prod_Nam)
        {
        OrderItem op = new OrderItem(); 
        op.Opportunity_del__c = needed[0].Master_Opportunity__c; 
        op.orderId = ord.id;
        op.Order_Name__c = Pr.Product_Abb__c;
        op.PriceBookEntryId = o.PriceBookEntryId;
        op.Quantity = o.Quantity;
        op.UnitPrice = 100;
        orderIt.add(op);
         }
          } 
        }
    upsert orderIt; 
}
}
  • you are creating self-inflicted wounds by your variable names. prodCode is really oliList; prod_nam is really prodList; needed is something like oliOppoList; that said the first three soqls could be combined as you can fetch Product2 parent fields from the OLI and OLI can be queried for opportunityId. Using maps instead of lists is so much more useful and can avoid the loops within loops – cropredy Feb 11 '16 at 14:47
0

In your first SOQL, thou you are only querying for product codes, you will get back opportunity line item in the list.

List<OpportunityLineItem> ProdCode = [Select ProductCode from OpportunityLineItem where id IN: oli];

So, in your second SOQL you are passing OpportunityLineItem list instead of product codes.

List<Product2> Prod_Nam = [Select id,Product_Abb__c from Product2 where ProductCode IN: ProdCode];

to fix this, after your first query, iterate and store the product codes into a separate collection / array and use it in your second SOQL.

something like

List<String> ProdCodes = new List<String>();
List<OpportunityLineItem> olList = [Select ProductCode from OpportunityLineItem where id IN: oli];
for(OpportunityLineItem ol : olList){
    ProdCodes.add(ol.ProductCode);
}
List<Product2> Prod_Nam = [Select id,Product_Abb__c from Product2 where ProductCode IN: ProdCodes];
  • actually i don't want to pass product id , i just want to pass product code @Vamsi – RAT__ Feb 11 '16 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.