• Rafael Suarez 14
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
Is it possible to use the batchable class in an anonymous block?
I'm attempting to update to a few hundred accounts from within the Execute Anonymous environment, but because of the existing Apex, Im getting "Too many SOQL queries: 50001".
So I attempted the following code (still within an  anonymous block) and it will come back with 

"Execute Anonymous Error
Line: 13, Column: 14
Global type must be contained inside of a global class"

I think the developer console doesn't like global classes...    Does it ?
Do I absolutely need a global modifier to use the SFDC batchable classes ?
Advise will be appreciated

This is the actual code:  
global class SICS implements Database.Batchable<Account>{
   global final String Query;
   string q = 'Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account' +
               'where Temp_BadCMSIC__c != NULL ';
    
  global UpdateAccountFields(String q){ Query=q;}

  global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}
    
 global void execute(Database.BatchableContext BC, List<Account> scope){
   
LIST<Account> SIC2Up  = [Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account where Temp_BadCMSIC__c != ''];
List <Account> Acc2Up = new List<Account>();
    for(Account acc : scope){
    IF(acc.Temp_BadCMSIC__c != NULL){
       acc.CMSIC8A__c = acc.Temp_BadCMSIC__c;
    }
    Acc2Up.add(acc);
}
Update Acc2Up;
}
}

// SICS obj = new SICS(); 
Id batchJobId = Database.executeBatch(new SICS(),200);
Thanks
RSM
 

I created a simple Apex schedulable class to update a single field in the user object.  Every night at 11 PM, the job is to copy the contents of the Last Login, cast it to date val, and save it to another field (business reasons, Last login is tricky, could not implement in declarartive).

The apex class was scheduled to run at 11 PM.  At 11:05-ish  I saw a debug log entry, stating that the job ran at 11:00:05 PM , and duration was more than 2 million ms.    However, the Apex Scheduled Jobs didn't update  the 'started ' date column until  30 mins later!

Any ideas on how in the world is it possible that
the debug logr declared the job finished right on time
the scheduled jobs did not update it until 15 mins later
still, thats nowhere near the declared duration of +2 million seconds (555 hrs) ?
Apex Log entry
Thanks
Rafa
Hello,

I am having issues running into this 50 future limit.

Currently, we have a portal that we created inhouse for our clients, and we send a lot of information from our salesforce database to our portal database. 

In this particular instance we are sending multiple objects to our portal, and per client we can get up to 100 of these objects, but obviouslly this is a problem because of the 50 max limit on @future.

If anyone has an example of how they get around this, or what is a effective way of getting this done. Is it possible to make a queue that executes in batches of 10, waits for them to return, and executes the next batch of 10? Or something simillar.
Is it possible to use the batchable class in an anonymous block?
I'm attempting to update to a few hundred accounts from within the Execute Anonymous environment, but because of the existing Apex, Im getting "Too many SOQL queries: 50001".
So I attempted the following code (still within an  anonymous block) and it will come back with 

"Execute Anonymous Error
Line: 13, Column: 14
Global type must be contained inside of a global class"

I think the developer console doesn't like global classes...    Does it ?
Do I absolutely need a global modifier to use the SFDC batchable classes ?
Advise will be appreciated

This is the actual code:  
global class SICS implements Database.Batchable<Account>{
   global final String Query;
   string q = 'Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account' +
               'where Temp_BadCMSIC__c != NULL ';
    
  global UpdateAccountFields(String q){ Query=q;}

  global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}
    
 global void execute(Database.BatchableContext BC, List<Account> scope){
   
LIST<Account> SIC2Up  = [Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account where Temp_BadCMSIC__c != ''];
List <Account> Acc2Up = new List<Account>();
    for(Account acc : scope){
    IF(acc.Temp_BadCMSIC__c != NULL){
       acc.CMSIC8A__c = acc.Temp_BadCMSIC__c;
    }
    Acc2Up.add(acc);
}
Update Acc2Up;
}
}

// SICS obj = new SICS(); 
Id batchJobId = Database.executeBatch(new SICS(),200);
Thanks
RSM
 
This issue came up without any new code being deployed. 

All help is greatly appreciated.

Description of Trigger

The Trigger automatically greats a renewal opportunity after a new opportunity is marked as closed one. 

Code
 
trigger RenewalOpptTrigger on Opportunity (after update) {
    Map<Id, Account> accts = new Map<Id, Account>([SELECT Id, Name FROM Account]);
    List<Opportunity> opps = new List<Opportunity>([SELECT Name FROM Opportunity]);
   //Checking paramaters for Renewal Opportunity - Must be Won and Closed, etc.
   
   if (trigger.isUpdate){
   for (Opportunity opp : Trigger.new){
       System.debug('Opportunity: ' + opp.Name + ', Stage: ' + opp.StageName);
              if(opp.StageName == 'Customer - Closed Won'){
                     if(opp.Type == 'Renewal' || opp.Type =='Renewal and Expansion' || opp.Type == 'New - Enterprise' || opp.Type == 'New Business Expansion'){
                      System.debug(LoggingLevel.Error, 'Opp ID: ' + opp.Id);
                    
                      //Renewal Date Calculations - Renewal Start and End Dates
                      Date contractStartDate = opp.Contract_Start_Date__c;
                      integer duration = (integer)opp.Contract_Duration_Months__c;
                      
                      Date renewalStartDate = contractStartDate.addMonths(duration);
                         system.debug(renewalStartDate);
                           Account acct = accts.get(opp.AccountId);
                           String acctName = acct.Name;
                      
                        String stage = 'SAL Demo Scheduled';
                        decimal amount;
                         
                         if(opp.License__c != null && opp.Contract_Increase__c!=null ){
                         amount =  ((opp.Contract_Increase__c/100)+1) * opp.License__c; 
                        }
                         else{amount = opp.License__c;}
                      
                       
                         system.debug(amount);
               
                      String title = String.valueOf(renewalStartDate.year()) + '_Renewal_' + acctName + '_$' + String.valueOf(amount.format());
                  
                      OpportunityContactRole[] oppconroles = [SELECT IsPrimary, ContactId FROM OpportunityContactRole WHERE OpportunityId = :opp.Id];
                    
                    boolean exists = false;
                    for (Opportunity o : opps) {
                       if (o.Name == title) {
                            exists = true;
                            System.debug(LoggingLevel.Error, o.Name + ' already EXISTS');
                       }
                      }
                    
                         
                         if(!exists){  
                      Opportunity ren_opp = opp.clone(false, true, false, false);
                       ren_opp.License__c = amount;
                       ren_opp.Amount = amount;
                       ren_opp.Previous__c = opp.License__c;
                        system.debug(ren_opp.License__c);
                        ren_opp.StageName = stage;
                        ren_opp.Contract_Start_Date__c = renewalStartDate;
                        ren_opp.Contract_Duration_Months__c = duration;
                        ren_opp.CloseDate = renewalStartDate;
                        ren_opp.Type = 'Renewal';
                        ren_opp.Probability = 10;
                        if(ren_opp.Contract_Increase__c>0){
                                ren_opp.Type='Renewal and Expansion';}
                            
                       
                       ren_opp.Name = title;
                  
                        //System Information deletions
                        ren_opp.CampaignId = null;
                        ren_opp.Date_moved_to_Closed_Lost__c = null;
                        ren_opp.Date_moved_to_Closed_Won__c = null;
                        ren_opp.Date_moved_to_Contract_Sent__c = null;
                        ren_opp.Date_moved_to_Demo_Complete__c = null;
                        ren_opp.Date_moved_to_Demo_Scheduled__c = null;
                        ren_opp.Date_moved_to_Verbal_Commit__c = null;
                        ren_opp.Date_moved_to_Proposal_Sent__c = null;
                        ren_opp.Amount_at_Closed_Lost__c = null;
                        ren_opp.Amount_at_Closed_Won__c = null;
                        ren_opp.Amount_at_Contract_Sent__c = null;
                        ren_opp.Amount_at_Demo_Complete__c = null;
                        ren_opp.Amount_at_Demo_Scheduled__c = null;
                        ren_opp.Amount_at_Proposal_Sent__c = null;
                        ren_opp.Amount_at_Verbal_Commit__c = null;

                        //SOW Deletions
                        ren_opp.Unique_Pricing_Scenario__c = null;
                        ren_opp.Department__c = null;
                        ren_opp.Signature_Events__c = null;
                        ren_opp.Number_of_Seats__c = null;
                        ren_opp.Special_Admin_Structure__c = null;
                        ren_opp.Design_Resources__c = null;
                        ren_opp.Needs_Designer_Pro__c = false;
                        ren_opp.Key_Functionality_Landmines__c = null;
                        ren_opp.Custom_Dev_Work__c=null;
                        ren_opp.Splash_User_Experience__c=null;
                        ren_opp.Kickoff_Email_Recipients__c=null;
                        ren_opp.Imminent_Deadline__c=null;
                        ren_opp.Client_Fears_or_Concerns__c=null;
                        ren_opp.Opportunities_for_Growth__c = null;
                        ren_opp.Opportunities_for_Growth_Yes__c = null;
                        ren_opp.Additional_Comments__c = null;
                        ren_opp.Require_Starter_Themes__c = null;
                       // ren_opp.SOW_End_Date__c = renewalStartDate;
                        
                        ren_opp.Primary_Purpose_of_Events__c = null;

                        //Docusign deletions     
                        ren_opp.DocuSign_Status__c = null;
                    
                        //Discovery Questions deletions   
                        ren_opp.Annual_of_Events__c = null;
                        ren_opp.CRM__c=null;
                        ren_opp.CRM_Other__c=null;
                        ren_opp.CRM_Other__c = null;
                        ren_opp.Budget__c = null;
                        ren_opp.Purpose_of_Events__c = null;
                        ren_opp.Purpose_of_Events_Other__c=null;
                        ren_opp.What_are_they_currently_using__c = null;
                        ren_opp.Marketing_Automation__c = null;
                        ren_opp.Marketing_Automation_Other__c = null;
                        ren_opp.Role_of_Primary_Contact__c = null;
                        ren_opp.Role_of_Primary_Contact_other__c = null;
                        ren_opp.Current_process_for_event_program__c = null;
                        ren_opp.Currently_using_other__c = null;
                        ren_opp.Value_Driver_details__c = null;
                        ren_opp.Primary_Purpose_of_Events_Other__c = null;
                        ren_opp.Need_SPS__c = null;
                  
                        //Other Deletions   
                        ren_opp.ForecastCategoryName = 'Pipeline';
                        ren_opp.Agreed_to_Case_Study__c = false; 
                         
                             insert ren_opp;
                             List<OpportunityContactRole> rolesToInsert = new List<OpportunityContactRole>();
                             for (OpportunityContactRole opprole : oppconroles) {
                            System.debug(LoggingLevel.ERROR, 'ContactRole: ' + opprole.ContactId);
              OpportunityContactRole ocp = opprole.clone(false, true, true, true);
                            ocp.OpportunityId = ren_opp.Id;
                            rolesToInsert.add(ocp);
                        }
                        
                        if (rolesToInsert.size() > 0) {
                            insert rolesToInsert;
                        }
          
                           update ren_opp;
                        
             }                      else{}       
           }            else{
                          System.debug('Case is not of the correct type');
       }    
    }         else{ 
                System.debug('Case '+ opp.Name + ' is not Closed and Won');
   }
  }
 }
}

 

Hello Apex Developers!

I am a new employee at a wealth management first. Due to the type of industry wealth management is, fields ar changing and being updated quite often (lets say as an example, tax brackets in a drop down list). I have been blazing my way through TrialHead, and have REALLY been enjoying what I have found, and my mind is loving all the opportunites I now have to create solutions for the firm, especially in regards to work flows and APEX. Anyways, back to it...

So lets say tax brackets change, or a field is replaced with another. The firm in history has created a new field, run reports, and used data loader to update fields. However none of them ever new anything about Apex, and after going through trailhead, I can't help but see the simplicity and power of it. I am familiar with coding laguages, and know there must be a simple way to iterate through all accounts or contacts looking for a certain field and entry, and updating it and assinging it to the new updated field options. Yet is seems like this actually may not be that simple using apex. Am I supposed to use a batch function, or is there a way I can run loops?

I don't need the code handed to me on a platter, but I would really appreciate a solution framework as to how I could use apex to update these fields relatively easily. If "I" could find a solution as to how I could do this, it would be implemented a couple times a week, and save countless hours of hassle and frustration.

Thank you so much guys, let me know what you think!

- David