• Rija Sana
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 7
    Replies
I have the following class that is hitting cpu time limit exception. still learning so help very much appreciated!!  When its working I'll put it in a schedulable class. 
 
public class test1 {
 public static void test1() {
      
     Map<id,account> aMap = new Map<id,account>([Select Id,Name, Total_number_of_open_opportunities__c,Total_number_of_open_Upsel_opportunities__c, No_of_days_since_last_AE_activity__c
                        FROM ACCOUNT WHERE Assigned_AE__c != null AND (Assigned_AE__r.profile.Name = 'Sales Executive' OR 
   Assigned_AE__r.profile.Name = 'Premier Account Executive') AND No_of_days_since_AE_assigned__c >=60]);
     List<account> acc =  aMap.values() ;
   
   system.debug(acc.size());
   
   List<Opportunity> opps = new List<Opportunity>();    //list of all open opps
   List<account> acc1 = new List<account>();  
 
   
     
  if(acc.size() > 0)
   {
  // Query to get all open opportunites with no stage change in over 60 days 
   Map<id,opportunity> aMap2 = new Map<id,opportunity>([SELECT Id,accountid FROM Opportunity 
                                     WHERE AccountId IN :acc AND ISCLOSED = FALSE AND days_since_last_stage_change__c > 60 AND (RecordType.Name= 'New Business' OR RecordType.Name= 'Upsell')]);
       
       opps = aMap2.values() ;
   
  
     for (account a : acc )
     {
       
         
       If((a.Total_number_of_open_opportunities__c == 0 && a.Total_number_of_open_Upsel_opportunities__c == 0) || (a.No_of_days_since_last_AE_activity__c > 30 || a.No_of_days_since_last_AE_activity__c == 0) || opps.size()>0)
        { 
           
            a.Assigned_AE__c = null;
            acc1.add(a);
        }
       
     }
     update acc1;
   }
}
}


 
So the trigger i have is working fine in production except that when our pipeline is updating sfdc during off hours I get email notification 'Apex script unhandled trigger exception by user/organization: BlockUsers: System.LimitException: Too many SOQL queries: 101'

I am new to apex even newer to batch apex. Can someone help me with turning this code into batch apex trigger 
 
trigger BlockUsers on Account  (before insert, before update) {

Account accs = Trigger.new[0];
string accExec = accs.Assigned_AE__c;
integer i=[select count() from account where Assigned_AE__c= :accExec AND Assigned_AE__c!=null];
for(Account a: Trigger.new){
 If (trigger.isInsert || (trigger.isUpdate && trigger.newMap.get(a.Id).Assigned_AE__c != trigger.oldMap.get(a.Id).Assigned_AE__c))
     {          
                if(i >=100)
                a.addError('The AE you are trying to add already has 100 accounts in their name');
}
}
}

trigger gets activated whenever an update happens on assigned_ae__c field. This code is reteiving all accounts where that particualar AE is listed and then counts them and as long as the number is less than 100 allows the update to happen.
 
The idea is to run the following script once a month (haven't implemented the scheduling part yet) to monitor activities and remove if a sdr is listed on an account but they have no activity in last 30 days. In last line its printing 0 records processed. Can someone help me and let me know what I am doing wrong. 

global class UpdateLastSdrActivity implements 
    Database.Batchable<sObject>, Database.Stateful {
        
 global Integer recordsProcessed = 0;
        
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
          'SELECT id,assigned_sdr__c FROM account WHERE assigned_sdr__c != null'
        );        
}
        
global void execute(Database.BatchableContext bc, List<Account> scope){
     Task t; 
    Date todaysDate = system.today();
    List<user> i1= [SELECT id FROM user Where user.profile.name='Sales Development Representative'];
    //System.Debug(i1[0].id);
    for (Account acc : scope) {
      t = [SELECT Id, ActivityDate,Status, OwnerId, Owner.Name FROM Task where accountid = :acc.Id AND OwnerID IN :i1 order by activitydate desc Limit 1];
      
        IF(t.ActivityDate.daysBetween(todaysDate) >30  && t.Status =='Completed')
        {
            acc.Assigned_SDR__c = null;
            update acc;
            recordsProcessed = recordsProcessed + 1;
        }
    }
    
}
        
global void finish(Database.BatchableContext bc){
    
     System.debug(recordsProcessed + ' records processed. Shazam!');
    
}
        
    }
Im trying to creat a trigger that monitors activities by profiles. How would I find profile name of any user by using the users' name?
I have the following class that is hitting cpu time limit exception. still learning so help very much appreciated!!  When its working I'll put it in a schedulable class. 
 
public class test1 {
 public static void test1() {
      
     Map<id,account> aMap = new Map<id,account>([Select Id,Name, Total_number_of_open_opportunities__c,Total_number_of_open_Upsel_opportunities__c, No_of_days_since_last_AE_activity__c
                        FROM ACCOUNT WHERE Assigned_AE__c != null AND (Assigned_AE__r.profile.Name = 'Sales Executive' OR 
   Assigned_AE__r.profile.Name = 'Premier Account Executive') AND No_of_days_since_AE_assigned__c >=60]);
     List<account> acc =  aMap.values() ;
   
   system.debug(acc.size());
   
   List<Opportunity> opps = new List<Opportunity>();    //list of all open opps
   List<account> acc1 = new List<account>();  
 
   
     
  if(acc.size() > 0)
   {
  // Query to get all open opportunites with no stage change in over 60 days 
   Map<id,opportunity> aMap2 = new Map<id,opportunity>([SELECT Id,accountid FROM Opportunity 
                                     WHERE AccountId IN :acc AND ISCLOSED = FALSE AND days_since_last_stage_change__c > 60 AND (RecordType.Name= 'New Business' OR RecordType.Name= 'Upsell')]);
       
       opps = aMap2.values() ;
   
  
     for (account a : acc )
     {
       
         
       If((a.Total_number_of_open_opportunities__c == 0 && a.Total_number_of_open_Upsel_opportunities__c == 0) || (a.No_of_days_since_last_AE_activity__c > 30 || a.No_of_days_since_last_AE_activity__c == 0) || opps.size()>0)
        { 
           
            a.Assigned_AE__c = null;
            acc1.add(a);
        }
       
     }
     update acc1;
   }
}
}


 
So the trigger i have is working fine in production except that when our pipeline is updating sfdc during off hours I get email notification 'Apex script unhandled trigger exception by user/organization: BlockUsers: System.LimitException: Too many SOQL queries: 101'

I am new to apex even newer to batch apex. Can someone help me with turning this code into batch apex trigger 
 
trigger BlockUsers on Account  (before insert, before update) {

Account accs = Trigger.new[0];
string accExec = accs.Assigned_AE__c;
integer i=[select count() from account where Assigned_AE__c= :accExec AND Assigned_AE__c!=null];
for(Account a: Trigger.new){
 If (trigger.isInsert || (trigger.isUpdate && trigger.newMap.get(a.Id).Assigned_AE__c != trigger.oldMap.get(a.Id).Assigned_AE__c))
     {          
                if(i >=100)
                a.addError('The AE you are trying to add already has 100 accounts in their name');
}
}
}

trigger gets activated whenever an update happens on assigned_ae__c field. This code is reteiving all accounts where that particualar AE is listed and then counts them and as long as the number is less than 100 allows the update to happen.
 
The idea is to run the following script once a month (haven't implemented the scheduling part yet) to monitor activities and remove if a sdr is listed on an account but they have no activity in last 30 days. In last line its printing 0 records processed. Can someone help me and let me know what I am doing wrong. 

global class UpdateLastSdrActivity implements 
    Database.Batchable<sObject>, Database.Stateful {
        
 global Integer recordsProcessed = 0;
        
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
          'SELECT id,assigned_sdr__c FROM account WHERE assigned_sdr__c != null'
        );        
}
        
global void execute(Database.BatchableContext bc, List<Account> scope){
     Task t; 
    Date todaysDate = system.today();
    List<user> i1= [SELECT id FROM user Where user.profile.name='Sales Development Representative'];
    //System.Debug(i1[0].id);
    for (Account acc : scope) {
      t = [SELECT Id, ActivityDate,Status, OwnerId, Owner.Name FROM Task where accountid = :acc.Id AND OwnerID IN :i1 order by activitydate desc Limit 1];
      
        IF(t.ActivityDate.daysBetween(todaysDate) >30  && t.Status =='Completed')
        {
            acc.Assigned_SDR__c = null;
            update acc;
            recordsProcessed = recordsProcessed + 1;
        }
    }
    
}
        
global void finish(Database.BatchableContext bc){
    
     System.debug(recordsProcessed + ' records processed. Shazam!');
    
}
        
    }
Im trying to creat a trigger that monitors activities by profiles. How would I find profile name of any user by using the users' name?