You need to sign in to do that
Don't have an account?
neeru
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
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.
All Answers
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.
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
Yours would look something like this
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.
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
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.
If you have a masterdetail relationship why are you using apex. You should create a roll up summary field on the employee object.
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.
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
Will you post the code you are using.
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
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
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.
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
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.
thanx andrew
i am waiting for ur reply..its really helpful to me...
thanks,
Jake
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.
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.