• Kondal Kontham
  • NEWBIE
  • -1 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
My trigger is too agressive and I am hoping that you can help.

Goal:  Update the boolean Move_to_ProductionP__c (on Project__c) if the following criteria are true:  o.Move_to_Production__c = True and o.RecordTypeId == '012a0000001FqTn'
Problem:  The trigger updates the boolean field Move_to_Production__c on the custom Project object (this is desired)  but it also updates the boolean field on  the Opportunity field Move_to_Production__c as True upon creation of an opportunity, which is not a desired outcome.

Here is my code.  I would appreciate any help from the experts!  Thanks in advance for looking.

trigger MoveToProduction on Opportunity (before insert, before update) {   

List<ID> ProjIds = New List<ID>();   

for(Opportunity o : Trigger.new){
 if(o.Move_to_Production__c = true && o.RecordTypeId == '012a0000001FqTn')
{       ProjIds.add(o.Project__c);    
 }
  }   
List<Project__c> ProjList = [SELECT id, Move_to_ProductionP__c FROM Project__c WHERE id in :ProjIds];   for(integer i = 0 ; i < ProjList.size(); i++){     
ProjList[i].Move_to_ProductionP__c = true;     
  }
update ProjList;
}
 
This may seem like an obvious question, but when adding profiles to a changes set, will it only bring over those changes in the profile pertaining to what is in my change set?
 I'm afraid that if I add Profiles to my change set, the entire profile will be overwritten with changes in sandbox that have nothing to do with what I'm trying to deploy to production. 
 
Hi
It makes sense to me that, Too many dml rows:10001 because of more than 10000 records being inserted,
Note: I have a soql query in my batch apex which is currently not limited by (Limit keyword in the query).i dont think i can do this because there is a possibility of some millions of records to be processed.
Currently there is no batch size set,I am not sure if limiting or decreasing my batch size is going to solve this problem o.
Help will be appreciated.

Here is my batch apex code and scheduler,
 
global class batchLogBookManagementV2 implements Database.Batchable<sObject>, Database.Stateful {

global List<Log_Book__c> logToBeContinued = new List<Log_Book__c> ();
global List<Log_Book__c> logToBeInserted = new List<Log_Book__c> ();    
global List<Subject_Registered__c> sbsr = new List<Subject_Registered__c> ();
    
    global List<Weeks__c> weekList = Weeks__c.getAll().values();
    global Map<Integer, Weeks__c> weekMap = new Map<Integer, Weeks__c>();
    global Map<String, Integer> weekMapGetIntWithWeek = new Map<String, Integer>();    
    global Map<String, Subjects_and_Levels__c> subjectLevelMap =  Subjects_and_Levels__c.getAll();
    global List<Subjects_and_Levels__c> subjectLevelList = subjectLevelMap.values();
    global Map<String, Subjects_and_Levels__c> levelMap = new Map<String, Subjects_and_Levels__c>();
    

        
global final String query = 'SELECT id,Last_Schedule__c, Level__c, Student__c, Subject__c, Week__c, (SELECT Student__c, Subjects_Registered__c, Subject__c, Class_Scheduled_Date__c, Level__c, Week__c FROM Log_Books__r ORDER By Class_Scheduled_Date__c DESC NULLS FIRST LIMIT 1 ) FROM Subject_Registered__c WHERE Deactivated__c = false AND Last_Schedule__c != null';
 
 
global Iterable<sObject> start(Database.BatchableContext BC){
     sbsr = Database.query(query);
     for(Subject_Registered__c s: sbsr)
     {
         logToBeContinued.add(s.log_Books__r[0]);         
     }
    
    return logToBeContinued;
    }
 
global void execute(Database.BatchableContext BC,List<Log_Book__c> scope){
     for(Subjects_and_Levels__c level: subjectLevelList)
     {
     system.debug('level : '+level);
         if(level !=null)
         levelMap.put(level.subject__c+Integer.valueof(level.Order__c), level);
         else
             system.debug('error as level null');
     }
    for(Weeks__c week : weekList)
    {
    system.debug('week : '+week);
        if(week != null){
            weekMap.put((Integer)week.Order__c, week);
            weekMapGetIntWithWeek.put(week.Name, (Integer)week.Order__c);
        }
    }
     System.debug('++++++++++++++++++++++++   : ' + scope);
     for(Log_Book__c s: scope)
     {
         
         System.debug('logToBeInserted--------------------------------   : ' + logToBeInserted.size());
         System.debug('--------------------------------   : ' + s);
         Integer totalLevels = 0;
          if(s.Subject__c == 'English')
            totalLevels = 15;
          else if(s.Subject__c == 'Abacus')
            totalLevels = 11;
          else if(s.Subject__c == 'Math')
            totalLevels = 14;
          else if(s.Subject__c == 'G.K')
            totalLevels = 4;
         System.debug('xxxxxxxxxxxxxxxx  : ' + subjectLevelMap);
          
         System.debug('@@Subject@@@@@@@'+subjectLevelMap.get(s.Subject__c+s.level__c) );
          
            if(subjectLevelMap.containsKey(s.Subject__c+':'+s.Level__c) && (s.Class_Scheduled_Date__c != null))
            {
                
            Integer levelOrder = Integer.valueof(subjectLevelMap.get(s.Subject__c +':'+s.Level__c).Order__c);
             system.debug('level order: '+levelOrder);
            Integer weekOrder = Integer.ValueOf(weekMapGetIntWithWeek.get(s.Week__c));
             system.debug('week order: '+weekOrder);
             Log_Book__c l = new Log_Book__c (); 
         l.Student__c = s.Student__c;
         l.Subject__c = s.Subject__c;
         l.Subjects_Registered__c = s.Subjects_Registered__c;
         l.Class_Scheduled_Date__c = s.Class_Scheduled_Date__c.addDays(7);
                System.debug('class schedule date is--->'+l.Class_Scheduled_Date__c);
           
         if(s.Week__c == '26-Z')
         {
             if(levelOrder != null && levelOrder < totalLevels)
             {
                 levelOrder = levelOrder +1;
                 System.Debug('lb.Subject__c+levelOrder : '+ l.Subject__c+levelOrder);
                 l.level__c = levelMap.get(l.Subject__c+levelOrder).Level__c;
                 l.Week__c = '1-A';
                 logToBeInserted.add(l);
             }
             
         }
         else
          if(s.Subject__c == 'Math' && s.Level__c == 'Level 5B' && s.Week__c == '17-Q')
            {
              if(levelOrder != null && levelOrder < totalLevels)
             {
                 levelOrder = levelOrder +1;
                 System.Debug('lb.Subject__c+levelOrder : '+ l.Subject__c+levelOrder);
                 l.level__c = levelMap.get(l.Subject__c+levelOrder).Level__c;
                 l.Week__c = '1-A';
                 logToBeInserted.add(l);
             }
       
            }
         else
         {
                weekOrder = weekOrder+1;
                l.Week__c = weekMap.get(weekOrder).Name;
                l.Level__c = s.Level__c;
                logToBeInserted.add(l);
                    
         }
    }  
     }//for
    if(logToBeInserted.size()>0)
    {
               
        Database.UpsertResult[] srList = Database.upsert(logToBeInserted, false);
        
        // Iterate through each returned result
        for (Database.UpsertResult  sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted LogBook. LogBook ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Account fields that affected this error: ' + err.getFields());
                }
            }    
        
        }
        
    }
     //System.debug('###############################  :' + ((Subject_Registered__c)scope.get(0)).log_Book__r.Class_Scheduled_Date__c);
     System.debug('Scope of the record is'+scope.size());
    // scope variable now has the records received from QueryLocator.
  //  delete scope;    
   // Database.emptyRecycleBin(scope);
    }
 
global void finish(Database.BatchableContext BC){
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id =:BC.getJobId()];
 
    // Send an email to the Apex job's submitter
    // notifying of job completion.
  
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 String[] toAddresses = new String[] {a.CreatedBy.Email};
 mail.setToAddresses(toAddresses);
 mail.setSubject('BAtch: ' + a.Status);
 //mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures.');
 mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures and Status'+a.status);
 
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }
}

//////////////////////////////////////////

global class scheduledLogBookBatch implements Schedulable
{
   global void execute(SchedulableContext SC) {
   
       batchLogBookManagementV2 p = new batchLogBookManagementV2(); 
       if(!Test.isRunningTest())
       Database.executeBatch(p);
       
   }
}