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
PallavPallav 

how to use a data collection as a result of the query with another query using "In " ??

Hi All,

I am facing a issue with the my apex code trigger. I have the following queries:
 it is giving me some invalid data binding error message.

1: - Compile Error: Incompatible element type Id for collection of SOBJECT:OpportunityLineItem at line 11 column 12 ------that means   // options.add(i.OpportunityId); line.

2:- Compile Error: Invalid bind expression type of SOBJECT:OpportunityLineItem does not match domain of foreign key at line 15 column 116 ------ that means
OpportunityLineItem [] oppIds = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId In : options ];


here is the complete code of it......


if(trigger.isAfter)
 {
   if((trigger.isInsert)||(trigger.isUpdate))
   {
   try{
        OpportunityLineItem [] OpIds = [Select OpportunityId from OpportunityLineItem where Id In : trigger.new];
       
        List<OpportunityLineItem> options= new List<OpportunityLineItem>();
        for(OpportunityLineItem i: OpIds)
         {
           // options.add(i.OpportunityId);
         }


       OpportunityLineItem [] oppIds = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId In : options ];// opportunity id bind exception

       for (OpportunityLineItem c :oppIds)
    {
         OpportunityLineItem [] OLI = [Select PricebookEntry.Product2Id from OpportunityLineItem where Id=: c.Id];
         Set<Id> Product2Ids = new Set<Id>();
         for(OpportunityLineItem o:OLI)
         {
           Product2Ids.add(o.PricebookEntry.Product2Id);
         }
         
         Product2 [] P2 = [Select Welcome_Program__c from Product2 where Id in :Product2Ids];
          for(Product2 pro2: P2)
          {
           if(pro2.Welcome_Program__c==true)
            {
               Opportunity opp=new Opportunity(Id=c.Id);
               opp.Welcome_Program__c=true;
               update opp;
            }
          }
         }         
       }

      catch(System.DmlException e)
      {
      System.assert(e.getDmlMessage(0).indexOf('Cannot Update opportunity') > -1);
      }
    }
   }


Your little help can solve my issue.

Thanks in anticipation of your response.

thanks and regards
Pallav
SuperfellSuperfell
You declared options as a list of OpportunityLineItems, but you're trying to put Ids in it instead.
List options= new List();
should be
List options= new List();