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
neeruneeru 

Error: Compile Error: Variable does not exist: e at line 11 column 12

Hi Everyone

 

trigger Rollup on Submission__c (after insert) {
   List<Employee__c> Emp=new List<Employee__c>();
   integer count=0;
  if(trigger.isinsert){
   for(Submission__c S:trigger.new){
      for(Employee__c e:[select id,Address__c,Count__c,Organization__c from Employee__c where id=:S.Submittedby_del__c])
   
          e.Count__c = e.count__c+1;  
                                       
   }
   Emp.add(e);
}
update Emp;
}

 

Thanks,

Jake

Best Answer chosen by Admin (Salesforce Developers) 
neeruneeru

Hi andrew

one of my my friend suggested like this

A trigger will cut it if you can deal with the Submission Count being out of Sync until the first Submission of the month is created. (You can create a DatField Submission_Last_Updated__c, which is updated by the trigger, each time is updating the count field)


So when a trigger runs, it always check if the Month of the current data is the same as the Month on the Submission_Last_Updated__c, if not it resets it to 0 and adds 1 to it, so it sets it to 1.

 

Or the more elegant solution will be scheduled (Batch) apex, which runs each night and selects all Submissions from that month and recalculates the count each night. You can of course schedule this as frequently as you'd like.

 

 upto know this was my approach here i'm missing Submission_last_modified_date__c ,could you please make changes for this 

 

 

global class Submissioncount implements Database.Batchable<sobject>,Schedulable {

global final string query= 'select Name,Current_Month__c,Date_of_Submission__c from Submission__c';
global String St;
global Submissioncount(){

Datetime dt=Datetime.now();
St=dt.format('MMMM');
}
global void execute(SchedulableContext SC) {

Submissioncount MR2 = new Submissioncount();
ID batchprocessid = Database.executeBatch(MR2,50);

}
global Database.QueryLocator start(Database.BatchableContext BC)
{

return Database.getQueryLocator(this.query);
}

global void execute(Database.BatchableContext BC, List<Submission__c> scope)
{
List<Submission__c> S1=new List<Submission__c>();

for(Submission__c S2:Scope)
{
for(Employee__c E:[select id,count__c from Employee__c]){
if(St==S2.Current_Month__c){

}
}
}
}
global void finish(Database.BatchableContext BC){

}
}

 

Thanks,

Jake

All Answers

Andrew WilkinsonAndrew Wilkinson

Alright a couple things I would change about this. I would get rid of the nested soql statement in your for loop. You could add it in a set first. The variable needs to be inside the nested for loop to be added.

 

trigger Rollup on Submission__c (after insert) {
   List<Employee__c> Emp=new List<Employee__c>();
   //add a set to get rid of the nested soql in for loop
   Set<String> submittedIds = new Set<String>();
   integer count=0;
  if(trigger.isinsert){
   for(Submission__c S:trigger.new){
      submittedIds.add(S.Submittedby_del__c);
   }
      
      for(Employee__c e:[select id,Address__c,Count__c,Organization__c from Employee__c where id IN :submittedIds])
          //did you try this
          //e.Count__c++:
     
          e.Count__c = e.count__c+1;
     //this was the change you needed.  
     Emp.add(e);                                  
   }
   

update Emp;
}

 I think you are trying to do a rollup summary of how many submissions an employee record has submitted...if I were doing this it would like more like this...this is an example from my developer org with objects related by a lookup relationship and not a master detail

 

trigger AttendeeTrigger on Attendees__c (after insert,after update) {
    //create set to hold conferences that should be updated
    Set<Id> conferenceIds = new Set<Id>();
    //list of conferences that are updated
    List<Conference__c> conferencesToUpdate = new List<Conference__c>();
    
    
    //populate the set
    for(Attendees__c att : newMap.values()){
        conferenceIds.add(att.Conference__c);
    }
    
    
    //use soql for loop
    for(Conference__c conf : [SELECT Id,Current_Number_Of_Attendees__c,(SELECT Id FROM Attendees__r) FROM Conference__c WHERE Id IN :conferenceIds]){
        conf.Current_Number_Of_Attendees__c = conf.Attendees__r.size();
        conferencesToUpdate.add(conf);
    }
    
    
    //dml to update all conferences at once
    update conferencesToUpdate;

}

 

 Yours would look something like this

 

trigger Rollup on Submission__c (after insert) {
    //create set to hold employees that should be updated
    Set<Id> employeeIds = new Set<Id>();
    //list of employees that will be updated
    List<Employee__c> employeesToUpdate = new List<Employee__c>();
    
    
    //populate the set
    for(Submission__c s : Trigger.new){
        employeeIds.add(s.submittedby_del__c);
    }
    
    
    //use soql for loop
    for(Employee__c e : [SELECT Id,Count__c,(SELECT Id FROM Submissions__r) FROM Employee__c WHERE Id IN :employeeIds]){
        e.Count__c = e.submissions__r.size();
        employeesToUpdate.add(e);
    }
    
    
    //dml to update all employees at once
    update employeesToUpdate;

}

 I didn't test this in an ide...so there may be some minor errors but that is the basics of creating a rollup summary on a lookup field for the count of an object. Hope that helps.

 

 

 

neeruneeru

Thanx For Your Reply Newbie i am trying with the masterdetail relationship

 

i am getting the same error for ur 1st code sample...

 

and i am geting the last code sample

 

error is

Error: Compile Error: Didn't understand relationship 'Submissions__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 15 column 25

 

Thanks,

Jake

Andrew WilkinsonAndrew Wilkinson

I am not sure what the relationship name is to submissions...try changing the select in the soql from submissions__r to submission__r. I do not know what the custom relationship name is.

Andrew WilkinsonAndrew Wilkinson

If you have a masterdetail relationship why are you using apex. You should create a roll up summary field on the employee object.

Andrew WilkinsonAndrew Wilkinson
trigger Rollup on Submission__c (after insert) {
   List<Employee__c> Emp=new List<Employee__c>();
   //add a set to get rid of the nested soql in for loop
   Set<String> submittedIds = new Set<String>();
   integer count=0;
  if(trigger.isinsert){
   for(Submission__c S:trigger.new){
      submittedIds.add(S.Submittedby_del__c);
   }
      
      for(Employee__c e:[select id,Address__c,Count__c,Organization__c from Employee__c where id IN :submittedIds]){
          //did you try this
          //e.Count__c++:
     
          e.Count__c = e.count__c+1;
     //this was the change you needed.  
     Emp.add(e);                                  
   }
   

update Emp;
}

 For this example I forgot the {. This will work but it won't do what you want it to do, if you are trying to create a roll up summary.

neeruneeru

i am getting runtime error like this

 

Apex trigger Rollup caused an unexpected exception, contact your administrator: Rollup: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Rollup: line 13, column 1

 

Thanks,

Jake

 

Andrew WilkinsonAndrew Wilkinson

Will you post the code you are using.

neeruneeru

Thanks For reply andrew

 

 

Actually my scenario is

 

I have two custom objects Employee (Master) , subminnion (Detail).My rollup in Employee gives no of submissions made by each .I need to filter these submissions based on current month and for each employee... for this  i'm planning to write a trigger . here i created a count field on employee everytime if a record enter into submission object the count should be incremented.but i need employee wise submissions count on monthly basis could you please help me out with some code

 

Thanks,

Jake

neeruneeru

This is my code

trigger Rollup on Submission__c (after insert) {
   List<Employee__c> Emp=new List<Employee__c>();
   //add a set to get rid of the nested soql in for loop
   Set<String> submittedIds = new Set<String>();
   integer count=0;
  if(trigger.isinsert){
   for(Submission__c S:trigger.new){
      submittedIds.add(S.Submittedby_del__c);
   }
      
      for(Employee__c e:[select id,Address__c,Count__c,Organization__c from Employee__c where id IN :submittedIds]){
          //did you try this
          //e.Count__c++:
     
          e.Count__c = e.count__c+1;
     //this was the change you needed.  
     Emp.add(e);                                  
   }
   

update Emp;
}
}

 

Error is

Apex trigger Rollup caused an unexpected exception, contact your administrator: Rollup: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Rollup: line 13, column

 

 

Thanks,

jake

Andrew WilkinsonAndrew Wilkinson

This code example won't work and the original code isn't set up for bulk. The reason this won't work is if there are multiple submissions by one employee the set will only update the employee record by one. Since there cannot be duplicate ids in a set. If the relationship between these objects is a master detail relationship then create a roll up summary field on the employee object. As for month to month...you are looking for the idea of trending reports. The closest thing to this is Analytics Snapshots...http://na11.salesforce.com/help/doc/en/data_about_analytic_snap.htm...

 

If this is a lookup field then you will need a trigger to create the roll up summary. Or maybe you should evaluate if the relationship could be a master detail. Once you change a lookup to a master detail you cannot change it back. The main issue with a master detail is if the parent is deleted so are the children and the relatiponship is set in stone. You cannot change the association from a child object once set.

 

So basically the solution would be 

If the relationship is a master detail...go to the Employee object, create a new rollup summary of the related submission object. Create analytic snapshots for each month to track the submissions for each employee. You may have to get a little creative with your reports.

 

neeruneeru

thanx for idea andrew

 

this code is working fine.....but my requirement is i need monthlywise submissions per each employee....how i can acheive this functionality

 

trigger Rollup on Submission__c (after insert) {
   List<Employee__c> Emp=new List<Employee__c>();
   //add a set to get rid of the nested soql in for loop
   Set<String> submittedIds = new Set<String>();
   integer count=0;
  if(trigger.isinsert){
   
    for(Submission__c S:trigger.new){  
      for(Employee__c e:[select id,Address__c,Count__c,Organization__c from Employee__c where id =:S.Submittedby_del__c]){
          //did you try this
          e.Count__c++;
     
          //e.Count__c = count+1;
     //this was the change you needed.  
     Emp.add(e);                                  
   }
   

update Emp;
}
}
}

 

Thanks,

Jake

Andrew WilkinsonAndrew Wilkinson

So if you wanted this functionality you would need to create a new object. It would be monthlySubmissions__c which would be created for each month or whatever you wanted to call it. The Employee would be the parent to monthlySubMissions then each submission would be a child of monthyl submissions. So it would by Employee(grandparent)-monthlySubmissions(parent)-submissions(child). The roll up summary field would be on the monthylSubmissions object of the submissions. The part that would need to be done in a trigger is setting the submission to the right month submission object if one existed for that month or create a new month submission and relate it to that.

 

For example...

 

A new Submission is created May 1 2012. If a monthylSubmission for the employee object existed for May then the submission would be related to that object. If the monthly submission did not exist then the trigger would create a new monthly submission object for may for that employee and relate the submission to it. It will take me a little bit to come up with the whole solution. I will create it in my dev org and post the code later.

neeruneeru

thanx andrew

 i am waiting for ur reply..its really helpful to me...

 

 

thanks,

Jake

Andrew WilkinsonAndrew Wilkinson

Sorry I haven't gotten back to you. I have not had a lot of time to construct the code. Also I think you should give it a try and then I can assist. Basically you need to create a junction object that would hold monthly submissions. Employee is the parent of monthly submissions and monthly submissions is the parent of submissions. You can write in your trigger to create a new monthly submission if one does not exist for that month or set the parent of the submission to the monthly object if the months match.

neeruneeru

Hi andrew

one of my my friend suggested like this

A trigger will cut it if you can deal with the Submission Count being out of Sync until the first Submission of the month is created. (You can create a DatField Submission_Last_Updated__c, which is updated by the trigger, each time is updating the count field)


So when a trigger runs, it always check if the Month of the current data is the same as the Month on the Submission_Last_Updated__c, if not it resets it to 0 and adds 1 to it, so it sets it to 1.

 

Or the more elegant solution will be scheduled (Batch) apex, which runs each night and selects all Submissions from that month and recalculates the count each night. You can of course schedule this as frequently as you'd like.

 

 upto know this was my approach here i'm missing Submission_last_modified_date__c ,could you please make changes for this 

 

 

global class Submissioncount implements Database.Batchable<sobject>,Schedulable {

global final string query= 'select Name,Current_Month__c,Date_of_Submission__c from Submission__c';
global String St;
global Submissioncount(){

Datetime dt=Datetime.now();
St=dt.format('MMMM');
}
global void execute(SchedulableContext SC) {

Submissioncount MR2 = new Submissioncount();
ID batchprocessid = Database.executeBatch(MR2,50);

}
global Database.QueryLocator start(Database.BatchableContext BC)
{

return Database.getQueryLocator(this.query);
}

global void execute(Database.BatchableContext BC, List<Submission__c> scope)
{
List<Submission__c> S1=new List<Submission__c>();

for(Submission__c S2:Scope)
{
for(Employee__c E:[select id,count__c from Employee__c]){
if(St==S2.Current_Month__c){

}
}
}
}
global void finish(Database.BatchableContext BC){

}
}

 

Thanks,

Jake

This was selected as the best answer