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
AsaktiAsakti 

batch class for below trigger please help,

trigger opportunityProduct on OpportunityLineItem (after insert) {
   
   
   Set<Id> oppId = new Set<Id>();
   
   for(OpportunityLineItem opp : Trigger.new)
   {
       oppId.add(opp.OpportunityId);
       
   }
   
   List<Opportunity> oppList = new List<Opportunity>();
   List<String> products = new List<String>();
   
   
   for(Opportunity opp : [Select Name,(Select Name,product2Id,product2.Name_Details__c from OpportunityLineItems) from Opportunity where Id in:oppId ])
   {
       for(OpportunityLineItem oppLine : opp.OpportunityLineItems)
       {
           if(OpportunityLineItem.Name != null)
           {
               products.add(oppLine.product2.Name_Details__c);
               
           }
       }
       opp.Product_Details__c = String.join(products,', ');
       oppList.add(opp);
       
   }
      update oppList;
}




batchclass i have written


global class productNameOnOpportunity implements Database.Batchable<sObject>{
   global Database.QueryLocator start(Database.BatchableContext BC){
       String query = 'Select Name,Product_Details__c (Select Name,product2Id,product2.Name_Details__c from OpportunityLineItems) from Opportunity';
       return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext info, List<Opportunity> Opp)
   {
       List<String> names = new List<String>();
       List<Opportunity> oppList = new List<Opportunity>();
       for(OpportunityLineItem oppLine : Opp)
       {
           if(OpportunityLineItem.Name != null)
           {          
               names.add(OpportunityLineItem.product2.Name_Details__c);
               Opp.Product_Details__c = String.join(names,',');
               oppList.add(Opp);
           }
       }
       if(oppList.size()>0)
       {
           update oppList;
       }  
   }    
   global void finish(Database.BatchableContext info){
       
   }
}
Best Answer chosen by Asakti
AbhinavAbhinav (Salesforce Developers) 
On linle 13 you are using opp.OpportunityLineItems.  as per your code use o1.OpportunityLineItems

All Answers

AbhinavAbhinav (Salesforce Developers) 
your issue?
 
AsaktiAsakti
getting below error
Variable does not exist: Name_Details__c
Variable does not exist: Product_Details__c
line 17 Method does not exist or incorrect signature: void add(List<Opportunity>) from the type List<Opportunity>
AbhinavAbhinav (Salesforce Developers) 
Try to execute this query from query editor first to check whether you query is right or not, I can see a comma is missing after Product_Details__c for sure. To be sure about child relationships  names first check your query from query editor.

Select Name,Product_Details__c (Select Name,product2Id,product2.Name_Details__c from OpportunityLineItems) from Opportunity
AsaktiAsakti
i checked added one comma query is working fine
 
AsaktiAsakti
can you help me with the batch class?
 
AbhinavAbhinav (Salesforce Developers) 
Isssue 2:

You are using Code

for(OpportunityLineItem oppLine : Opp)
       {
           if(OpportunityLineItem.Name != null)
           {          
               names.add(OpportunityLineItem.product2.Name_Details__c);
               Opp.Product_Details__c = String.join(names,',');
               oppList.add(Opp);
           }
       }

Here Opp is not list of OpportunityLineItem Its a list of Opportunity  which you are getting in scope;

Try this:

for(Opportunity op : Opp)
   {
       for(OpportunityLineItem oppLine : op.OpportunityLineItems)
       {
             //Your Logic   
           }
    }
       
 
AbhinavAbhinav (Salesforce Developers) 
On linle 13 you are using opp.OpportunityLineItems.  as per your code use o1.OpportunityLineItems
This was selected as the best answer
AsaktiAsakti
Thanks a lot.
AbhinavAbhinav (Salesforce Developers) 
If it worked, please close the query by selecting a best answer.