You need to sign in to do that
Don't have an account?
Jkk
how to convert following class to handler:
trigger countmeetings on Event (after insert, after delete) {
List<id> levent = new List<id>();
Set<id> accid = new Set<id>();
List<Event> eventtoupdate = new List<Event>();
public integer inp=0;
if(trigger.isInsert){
for(Event ce:trigger.new){
accid.add(ce.WhatId);
}
}
if(trigger.IsDelete){
for(Event ce: trigger.old){
accid.add(ce.WhatId);
}
}
for(Event ev:[SELECT id FROM event WHERE whatid=:accid]){
levent.add(ev.id);
}
for(Event event :[SELECT id,Distribution_Meetings__c FROM Event WHERE id=:levent]){
event.Distribution_Meetings__c = levent.size();
eventtoupdate.add(event);
}
If(eventtoupdate.size()>0){
update eventtoupdate;
}
}
List<id> levent = new List<id>();
Set<id> accid = new Set<id>();
List<Event> eventtoupdate = new List<Event>();
public integer inp=0;
if(trigger.isInsert){
for(Event ce:trigger.new){
accid.add(ce.WhatId);
}
}
if(trigger.IsDelete){
for(Event ce: trigger.old){
accid.add(ce.WhatId);
}
}
for(Event ev:[SELECT id FROM event WHERE whatid=:accid]){
levent.add(ev.id);
}
for(Event event :[SELECT id,Distribution_Meetings__c FROM Event WHERE id=:levent]){
event.Distribution_Meetings__c = levent.size();
eventtoupdate.add(event);
}
If(eventtoupdate.size()>0){
update eventtoupdate;
}
}
Hi JKK,
Please try this.
Trigger ->
trigger countmeetings on Event (after insert, after delete) {
if(trigger.IsInsert) {
EventHelper.updateDistributionMeetings(trigger.new);
}
if(trigger.IsDelete) {
EventHelper.updateDistributionMeetings(trigger.old);
}
}
HELPER ->
public class EventHelper {
public Set<Id> accountIds {get; set;}
public Void updateDistributionMeetings(List<Event> allEvents) {
for (Event eventRec : allEvents) {
accountIds.add(eventRec.WhatId);
}
Map<Id, Integer> accountWithCount = new Map<Id, Integer>();
List<Event> events = [SELECT Id, Distribution_Meetings__c, WhatId FROM Event WHERE WhatId =: accountIds];
for (Id accId : accountIds) {
Integer totalAccount = 0;
for (Event eventRec : events) {
if (eventRec.WhatId == accId) {
totalAccount++;
}
}
accountWithCount.put(accId, totalAccount);
}
List<Event> eventsForUpdate = new List<Event>();
for (Event eventRec : events) {
eventRec.Distribution_Meetings__c = accountWithCount.get(eventRec.WhatId);
eventsForUpdate.add(eventRec);
}
Update eventsForUpdate;
}
}
I hope it helps you.
Kindly let me know if it helps you and please mark as Best Answer.
Thanks and Regards,
Biswojeet
All Answers
Hi JKK,
Please try this.
Trigger ->
trigger countmeetings on Event (after insert, after delete) {
if(trigger.IsInsert) {
EventHelper.updateDistributionMeetings(trigger.new);
}
if(trigger.IsDelete) {
EventHelper.updateDistributionMeetings(trigger.old);
}
}
HELPER ->
public class EventHelper {
public Set<Id> accountIds {get; set;}
public Void updateDistributionMeetings(List<Event> allEvents) {
for (Event eventRec : allEvents) {
accountIds.add(eventRec.WhatId);
}
Map<Id, Integer> accountWithCount = new Map<Id, Integer>();
List<Event> events = [SELECT Id, Distribution_Meetings__c, WhatId FROM Event WHERE WhatId =: accountIds];
for (Id accId : accountIds) {
Integer totalAccount = 0;
for (Event eventRec : events) {
if (eventRec.WhatId == accId) {
totalAccount++;
}
}
accountWithCount.put(accId, totalAccount);
}
List<Event> eventsForUpdate = new List<Event>();
for (Event eventRec : events) {
eventRec.Distribution_Meetings__c = accountWithCount.get(eventRec.WhatId);
eventsForUpdate.add(eventRec);
}
Update eventsForUpdate;
}
}
I hope it helps you.
Kindly let me know if it helps you and please mark as Best Answer.
Thanks and Regards,
Biswojeet
Can you tell me what are you trying to do here? I mean what is the required functionality of this code.