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
dblazedblaze 

Trigger to Update Custom Object

Hi Everyone,

I am trying to create a trigger that updates a field (Passed_Test_Count__c on the Employee__c Object.  It will update the field by adding +1 each time another field Passed_Date__c on the Team_Member_Test_Result__c changes from null to an actual date.  Listed below is my code, any help is greatly appreciated!

 

 

trigger PassedTestCounter on Team_Member_Test_Result__c (after insert, after update) {
{
Map<Id,Id> TeamMemberResultsMap = new Map<Id,Id>();
for(Team_Member_Test_Result__c A : trigger.new)
TeamMemberResultsMap.put(A.Passed_Date__c,A.Id);

List<Employee__c> EmployeeToUpdate = new List<Employee__c>{};

for (Employee__c Employee: [SELECT Id,Passed_Test_Count__c FROM Employee__c WHERE Id IN: TeamMemberResultsMap.keySet()])
{
Id empId = TeamMemberResultsMap.get(EmployeeToUpdate.Id);
Employee__c emp = trigger.newMap.get(empId);

boolean transition = trigger.IsInsert || (trigger.isUpdate && trigger.oldMap.get(emp.Id).Passed_Date__c != emp.Passed_Date__c);
if (emp.Passed_Date__c != null){
EmployeeToUpdate.Passed_Test_Account__c = EmployeeToUpdate.Active_Count__c.Passed_Test_Account__c + 1 ;
EmployeeToUpdate.add(EmployeeToUpdate);
}else if (emp.Passed_Date__c == 'null') {
EmployeeToUpdate.Active_Count__c = EmployeeToUpdate.Active + 0;
EmployeeToUpdate.add(Employee);
}
}
if(EmployeeToUpdate.Active_Count__c != null && !EmployeeToUpdate.Active_Count__c.isEmpty())
Database.update(EmployeeToUpdate.Active_Count__c);
}
}

 

 

 

Thank you!!

Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

Dblaze,

 

Lookup relationships are fine. Where I had listed "EmployeeId__c" you will need to substitute with your lookup field. Most likely something like Employee__c.

 

Best of luck!

All Answers

Tim BarsottiTim Barsotti

trigger PassedTestCounter on Team_Member_Test_Result__c (after insert, after update) {
Map<Id, Date> TeamMemberResultsMap = new Map<Id, Date>();
List<Employee__c> EmployeeToUpdate = new List<Employee__c>();
for(Team_Member_Test_Result__c A : trigger.new) {
TeamMemberResultsMap.put(A.EmployeeId__c, A.Passed_Date__c);
}
List<Employee__c> EmployeesList = [SELECT Id,Passed_Test_Count__c FROM Employee__c WHERE Id IN: TeamMemberResultsMap.keySet()

for (Team_Member_Test_Result__c tm: trigger.new) {
for(Employee__c e: EmployeesList) {
if(e.Id == tm.EmployeeId__c && tm.Passed_Date__c != null && trigger.oldMap.get(tm.Id).Passed_Date__c == null) {
if(e.Passed_Test_Account__c != null) {
e.Passed_Test_Account__c++;
} else {
e.Passed_Test_Account__c = 1;
}
EmployeeToUpdate.add(e);
}
}
}
if(EmployeeToUpdate.size()>0)
update EmployeeToUpdate;
}
}

dblazedblaze

Thanks for the reply Tim!  It looks like there is a syntax error with the code you sent 
Error: Compile Error: unexpected token: ( at line 8 column 12)  how do you suggest I resolve this?

Yoganand GadekarYoganand Gadekar

Can you please post more details on your object structture. What is the relationship between  Employee__c and Team_Member_Test_Result__c ?

 

Thanks and Regards,

Salesforce learn by example @

Learn By example

dblazedblaze

Sure!  The Team_Member_Test_Result__c object has a lookup field (Employee__c) related to the Employee Object.  Our goal is to do reporting on the Passed_Date__c field under the Team_Member_Test_Result__c object to check compliance.  Since the Employee and Team_Member_Test_Result objects are not master-detail we are unable to do standard roll-up summary type reports so the work around is to have a trigger update a field on the employee object.  Once that field is updated we can then use another object that is master-detail to do reporting.  Hope this helps!  Please let me know if you need more information.

Tim BarsottiTim Barsotti

DBlaze, 

 

This was example code, since the field names are custom to your tenant you will need to ensure they are correct. However, I did miss a closing bracket on this line:

List<Employee__c> EmployeesList = [SELECT Id,Passed_Test_Count__c FROM Employee__c WHERE Id IN: TeamMemberResultsMap.keySet()

 

Change it to:

List<Employee__c> EmployeesList = [SELECT Id,Passed_Test_Count__c FROM Employee__c WHERE Id IN: TeamMemberResultsMap.keySet()];

dblazedblaze

Tim,

Will this only work if the relationship is set to Master-Detail? Team_Member_Test_Result__c is currently set up as a lookup relationship to the Employee__c Object.

Tim BarsottiTim Barsotti

Dblaze,

 

Lookup relationships are fine. Where I had listed "EmployeeId__c" you will need to substitute with your lookup field. Most likely something like Employee__c.

 

Best of luck!

This was selected as the best answer
dblazedblaze

Thanks Tim!  Here is the final code that appears to be working correctly I just need to write a test class so that I can post it along with this for anyone who might stumble across this in the future.  Any assistance would be greatly appreciated.

 

trigger PassedTestCounter on Team_Member_Test_Result__c (after insert, after update) {
Map<Id, Date> TeamMemberResultsMap = new Map<Id, Date>();
List<Employee__c> EmployeeToUpdate = new List<Employee__c>();
for(Team_Member_Test_Result__c A : trigger.new) {
TeamMemberResultsMap.put(A.Employee__c, A.Passed_Date__c);
}
List<Employee__c> EmployeesList = [SELECT Id,Passed_Test_Count__c FROM Employee__c WHERE Id IN: TeamMemberResultsMap.keySet()];

for (Team_Member_Test_Result__c tm: trigger.new) {
for(Employee__c e: EmployeesList) {
if(e.Id == tm.Employee__c && tm.Passed_Date__c != null && trigger.oldMap.get(tm.Id).Passed_Date__c == null) {
if(e.Passed_Test_Count__c != null) {
e.Passed_Test_Count__c++;
} else {
e.Passed_Test_Count__c = 1;
}
EmployeeToUpdate.add(e);
}
}
}
if(EmployeeToUpdate.size()>0)
update EmployeeToUpdate;
}

 

Tim BarsottiTim Barsotti

I can't test this without replicating your tenant, but try this and see if it passes. 

 

@IsTest
Public Class PassedTestCounterTest{
	Public Static Employee__c CreateEmployee(){
		Employee__c e = new Employee();
		e.Name = 'Test Employee';
e.Passed_Test_Count__c = null; //add in any other required fields insert e; system.assert(e.Id != null); return e; } Public Static Team_Member_Test_Result__c InsertTmtr(Id empId) { Team_Member_Test_Result__c tmtr = new Team_Member_Test_Result__c(); tmtr.Employee__c = empId; tmtr.Passed_Date__c = null; //add in any other required fields insert tmtr; system.assert(tmtr.Id != null); return tmrt; } Public Static Void UpdateTmtr(Id tmtrId) { Team_Member_Test_Result__c tmtr = new Team_Member_Test_Result__c(); tmtr.Passed_Date__c = System.Today(); tmtr.Id = tmtrId; update tmtr; } Static TestMethod void TestTrigger() { Employee__c e = CreateEmployee(); //create employee Team_Member_Test_Result__c tmtr = InsertTmtr(e.Id); //create test result System.assert(e.Passed_Test_Count__c == 0); //after creating test count should equal 0 - as it was a null date UpdateTmtr(tmtr.id); System.assert(e.Passed_Test_Count__c == 1); //after updating the passed test count should equal 1 } }