• madhu reddy 93
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
 for(WorkOrderLineItem workLine : [select id, workOrderId from workOrderlineItem where workOrderId IN :WorkOrderIds]){
        mapWorkOrderLineItem.put(workLine.workOrderId, workLine);
    }
    Id ProdId = [select Id, Product2Id, Product2.ProductCode from PriceBookEntry where Name = 'Service Fee' and Id = '01u7F000007Xf5XQAS'].Id;
     system.debug('**********Product2Id*********    '+ProdId);
List<ProductConsumed> prodConsumedList = new List<ProductConsumed>();
    if(Trigger.isupdate && trigger.isafter && firstAfterUpdate)
    {
        
        for(ServiceAppointment svc : [select id, status, Actual_Duration_Hours__c,ParentRecordId from ServiceAppointment where id IN :trigger.new]){
            if(WorkOrderIds.contains(svc.ParentRecordId) && svc.Status =='Completed'){
                system.debug('**********svc1*********    '+svc);
                mapWorkOrderLineItem.get(svc.ParentRecordId).Asset_Title__c='Service Fee';
                    system.debug('**********svc2*********    '+svc);
               ProductConsumed pc = new ProductConsumed();
                pc.QuantityConsumed = svc.Actual_Duration_Hours__c;
                pc.PricebookEntryId = ProdId;
                pc.WorkOrderId=svc.ParentRecordId;
                pc.WorkOrderLineItemId=mapWorkOrderLineItem.get(svc.ParentRecordId).id;
                prodConsumedList.add(pc);
            }
            
        }
         system.debug('**********svc3*********    ');
             update mapWorkOrderLineItem.values(); 
      insert prodConsumedList;
         system.debug('**********svc4*********    ');
        
        
    } 
 for(WorkOrderLineItem workLine : [select id, workOrderId from workOrderlineItem where workOrderId IN :WorkOrderIds]){
        mapWorkOrderLineItem.put(workLine.workOrderId, workLine);
    }
    Id ProdId = [select Id, Product2Id, Product2.ProductCode from PriceBookEntry where Name = 'Service Fee' and Id = '01u7F000007Xf5XQAS'].Id;
     system.debug('**********Product2Id*********    '+ProdId);
List<ProductConsumed> prodConsumedList = new List<ProductConsumed>();
    if(Trigger.isupdate && trigger.isafter && firstAfterUpdate)
    {
        
        for(ServiceAppointment svc : [select id, status, Actual_Duration_Hours__c,ParentRecordId from ServiceAppointment where id IN :trigger.new]){
            if(WorkOrderIds.contains(svc.ParentRecordId) && svc.Status =='Completed'){
                system.debug('**********svc1*********    '+svc);
                mapWorkOrderLineItem.get(svc.ParentRecordId).Asset_Title__c='Service Fee';
                    system.debug('**********svc2*********    '+svc);
               ProductConsumed pc = new ProductConsumed();
                pc.QuantityConsumed = svc.Actual_Duration_Hours__c;
                pc.PricebookEntryId = ProdId;
                pc.WorkOrderId=svc.ParentRecordId;
                pc.WorkOrderLineItemId=mapWorkOrderLineItem.get(svc.ParentRecordId).id;
                prodConsumedList.add(pc);
            }
            
        }
         system.debug('**********svc3*********    ');
             update mapWorkOrderLineItem.values(); 
      insert prodConsumedList;
         system.debug('**********svc4*********    ');
        
        
    } 
Hi,

  I wrote a trigger which is working fine when deployed to production am getting below eror Please suggest me how to fix this issue

Error: System.LimitException: Too many SOQL queries: 101 Trigger.logosince: line 28, column 1
 
trigger logosince on Account (before  update) 
{
  Try
 {
   Set<Id> accountId = new Set<Id>();
   List<Id> chkoneprt = new List<Id>();
   List<Id> chkoneid = new List<Id>();
   for (Account a : Trigger.New) 
   {     
      accountId.add(a.id);
      chkoneid.add(a.id);
      chkoneprt.add(a.parentid);
      
      } 
       
      /* not required for ( account actone : [select id,parentid from account 
                                where id in :accountId 
                               ])
        {
          chkoneid.add(actone.id);  
          chkoneprt.add(actone.parentid);  
        }  */
        
          system.debug('ID' + chkoneid);
         system.debug('Parent ID' + chkoneprt);
 
        List<Id> finalid = new List<Id>();
        for ( account actpar : [select id,parentid from account 
                                where 
                                     ( parentid != '' and
                                       parentid in :chkoneprt ) or 
                                     ( parentid != '' and
                                       parentid in :chkoneid ) or
                                      id in :chkoneid 
                               ])
        {
          finalid.add(actpar.id);  
            
        }
        system.debug('ID' + chkoneid); 
        system.debug('Final ID' + finalid);        
             
     List<AggregateResult> gr = [ 
     SELECT min(closedate) from opportunity 
     WHERE accountid IN :chkoneid or accountid in :finalid];
   
       
     for (AggregateResult ar : gr)  {
        system.debug('Min Opp date' + (Datetime)ar.get('expr0'));
        for(Account act : trigger.new) 
              {
              act.Logo_Since__c = (Datetime)ar.get('expr0') + 1;
              
              }
         }     
     
 }
  catch (System.NullPointerException e) {
    system.debug('Null Exception');
  }    
             
         
}