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
Karna ShivaKarna Shiva 

Rebate_Amount__c object having - 92255 rows and suppose to insert 92255 rows on gross profit, but it is inserting more than 1 laks rows

Please see the below code:

Rebate_Amount__c object having - 92255 rows and suppose to insert 92255 rows on gross profit, but it is inserting more than 1 laks rows

global class RebateGPBatch implements Database.Batchable<SObject>
{

    global Database.QueryLocator start(Database.BatchableContext bc)

  {
       string query = '';
       query += ' select Opportunity_Name__r.id  from Rebate_Amount__c ';
    
       if(Test.isRunningTest())
       {
          query  = ' select id from Rebate_Amount__c  ';
          query += ' where company__c = \'SDS\' ';
          query += ' limit 20';
       }

       return Database.getQueryLocator(query);
  }

  global void execute(Database.BatchableContext bc, List<SObject> scope)
  {

     List<Id> optyWithQuoteSet = new List<Id>();
    
  
      for (sObject obj :scope)
      {
         Rebate_Amount__c   op = (Rebate_Amount__c)obj;
          
         optyWithQuoteSet.add(op.Opportunity_Name__r.id);
      }


    
list<Shipped__c>  listSch =[select cost__c,Material__c  ,Opportunity2__c
                                                  from Shipped__c
                                                     where Quote__c!=null and Opportunity2__r.StageName='S' and Opportunity2__c = :optyWithQuoteSet];

map<string,Shipped__c> mapShipped= new map<string,Shipped__c>();

for(Shipped__c sch : listSch)
{
mapShipped.put(sch.Material__c + sch.Opportunity2__c,sch);
}

  //GP insert - start
   List<Gross_Profit__c> GPList = new List<Gross_Profit__c>();

Rebate_Amount__c object having - 92255 rows and suppose to insert 92255 rows on gross profit, but it is inserting more than 1 laks rows

List<Rebate_Amount__c> Gp_data = [Select Material__c,Quote_Number__c,Company_Code__c,Rebate_by_Amount__c,Item_Number__c,Active_Flag__c,Net_Sales_Amount__c from Rebate_Amount__c where Opportunity_Name__r.id=:optyWithQuoteSet];


for(Rebate_Amount__c oli : Gp_data)
{

   Gross_Profit__c  gp= new Gross_Profit__c();
   gp.Quote_No__c= oli.Quote_Number__c;
   gp.Company__c=oli.Company_Code__c;
   gp.Item_Number__c = oli.Item_Number__c;
   gp.Rebate__c = oli.Rebate_by_Amount__c;
   gp.Active_Flag__c = oli.Active_Flag__c;
   gp.Net_Sales__c = oli.Net_Sales_Amount__c;
   string keyMap_Cost=oli.Material__c + oli.Quote_Number__c;

   if (mapShipped.containskey(keyMap_Cost))
    {
    gp.Cost__c = mapShipped.get(keyMap_Cost).cost__c;
    }
    GPList.add(gp);
     }
   if(GPList.size()>0)
   {
      system.debug('GP Insert section ');
   insert GPList;
   }


   // GP insert - end

}

   global void finish(Database.BatchableContext bc)
  {

    }



}
MikeGillMikeGill
Hi Karna,

Are you saying the batch apex inserts (1) row and not (92255)

Try limiting the query

query += ' select Id, Opportunity_Name__r.id  from Rebate_Amount__c LIMIT 10000';
M
Karna ShivaKarna Shiva
Rebate_Amount__c object having - 92255 rows

Batch program suppose to insert 92255 rows on gross profit object, but  batch apex is inserting more than 1 laks rows Gross profit object

Rebate_Amount__c count  equals to Gross_Profit__c count

Rebate amount = Gross profit