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
Ramesh Malkagiri 2Ramesh Malkagiri 2 

Field Update for Rollup Summary

Lab Object is the child of Student objectWorkflow action rulesStudent Object is the Master Object of Lab Object

Hi All
I would appreciate any help with the above scenario, Attached are the screen shots of the scenario.
The situation is :
I have created a Master-Detail relationship from Lab object to Student Object, where student is the Master and lab is child.
On student object i created a rollup summary field called "Lab Absent"
My workflow rule is (Whenever a student is absent for the lab for 2 consecutive days, the "Lab absent" field on student object should update and automatically send an email to the faculty).

******** The problem we have is ---- when the student returns on the third day and is marked present on the "Lab Object"
The "Lab Absent"  field on student object doesn't change the value back to zero and it is still sending an email that the student is absent.

Please advise solution for the above problem and if there is any issues with my logic.
Note: the above situation doesn't involve any coding, its purely using Admin. If there is any coding required please advise the code.

Regards
Ramesh
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Ramesh,

You need to update WF enntry criteria to "Everytime record is created and updated".

Ideally, you should be creating a Rollup field on Student object which will Count only those records where you have Attendance__c = 'Absent'.

This should solve your problem.
Jigar.LakhaniJigar.Lakhani
It looks like you are trying to update rollup summary field(Lab Absent) from field update which is not possible. We cannot update rollup summary field since iot it is read only fields.

Goal:
Need to reset the "Lab Absent" rollup summary field to - 0

Solution:
- Add one more picklist value in "Attendance" field like Absent Email Sent
- Add one checkbox field(internal user only) like "Send Absent Email", update that checkbox to true from your existing workflow
- Write trigger on Lab object, which will execute only that "Send Absent Email" checkbox is true. Now in trigger you need to get all Lab records(With Attendance = Absent) of its student record and update all those lab records to Attendance field value to "Absent Email Sent". As well as reset that checkbox flag to false.
- So now, after your workflow executes and after email sent you have no any lab which have "Absent" value exist so rollup summary field(Lab Absent) have value 0 and it would not send email again.

Below is trigger, Please try.
May be you need to update object and field api names
Send_Absent_Email__c - API name of new checkbox field
trigger LabTrigger on Lab__c (AFTER UPDATE) {
	
	// Update Lab Records to "Absent Email Sent"
	Set<Id> setStudentIds = new Set<Id>();
	for (Lab__c objLab:Trigger.New) {
		if (objLab.Send_Absent_Email__c == true && Trigger.OldMap.Get(objLab).Send_Absent_Email__c == false) {
			setStudentIds.Add(objLab.Student_Name__c);
		}
	}
	
	List<Lab__c> listUpdateLabs = new List<Lab__c>();
	for (Student__c objStudent:[SELECT Id,(SELECT Id FROM Labs__r WHERE Attendance__c = 'Absent') FROM Student__c WHERE Id In:setStudentIds]) {
		for (Lab__c objLab:objStudent.Labs__r) {
			objLab.Attendance__c = 'Absent Email Sent';
			listUpdateLabs.Add(objLab);
		}
	}
	
	if (listUpdateLabs != null && listUpdateLabs.size() > 0) {
		Update listUpdateLabs;
	}
	
	// Reset the Send Absent Email flag
	List<Lab__c> listUpdateLabsToReset = new List<Lab__c>();
	for (Lab__c objLab:Trigger.New) {
		objLab.Send_Absent_Email__c = false;
		listUpdateLabsToReset.Add(objLab);
	}
	if (listUpdateLabsToReset != null && listUpdateLabsToReset.size() > 0) {
		Update listUpdateLabsToReset;
	}
}

Let me know if any issue.

Thanks & Cheers,
Jigar
Ramesh Malkagiri 2Ramesh Malkagiri 2
Hi Jigar,
Thanks for your reply.
I have tried the above but getting  incompatible key type error.
I have attached the screen shot could you please advise

Regards
RameshUser-added image
Jigar.LakhaniJigar.Lakhani
Hello,

Please update below line
if (objLab.Send_Absent_Email__c == true && Trigger.OldMap.Get(objLab).Send_Absent_Email__c == false) {
With
if (objLab.Send_Absent_Email__c == true && Trigger.OldMap.Get(objLab.id).Send_Absent_Email__c == false) {

 

Thanks & Cheers,
Jigar (pateljb90@gmail.com)