You need to sign in to do that
Don't have an account?

Trigger after insert, after update
Hi, i have a problem with a trigger.
I need to reduce the count when a "Iscrizione" is deleted from a Contact. The trigger doesn't work when i remove the record. How can i solve this ?
trigger ContaIscr on Iscrizione__c (after insert ,after delete) {
set<id> studId = new set<id>();
for(Iscrizione__c a : trigger.new){
studId.add(a.Contact__c);
}
list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
for(Iscrizione__c isc : Trigger.new){
for(Contact con : lstCoustumObject){
if(isc.contact__c != NULL){
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
}else{
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;
}
}
}
update lstCoustumObject;
}
I need to reduce the count when a "Iscrizione" is deleted from a Contact. The trigger doesn't work when i remove the record. How can i solve this ?
trigger ContaIscr on Iscrizione__c (after insert ,after delete) {
set<id> studId = new set<id>();
for(Iscrizione__c a : trigger.new){
studId.add(a.Contact__c);
}
list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
for(Iscrizione__c isc : Trigger.new){
for(Contact con : lstCoustumObject){
if(isc.contact__c != NULL){
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
}else{
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;
}
}
}
update lstCoustumObject;
}
There is nothing wrong with your logic just a minor change is required. According to salesforce, you cannot use trigger.new in delete context.
trigger ContaIscr on Iscrizione__c (after insert ,after delete) {
if(trigger.isAfter && trigger.isInsert){
ContalscrHelper.checkMethod(trigger.new);
}
else if(trigger.isAfter && trigger.isDelete){
ContalscrHelper.checkMethod(trigger.old);
}
public class ContalscrHelper{
public static void checkMethod(list<Iscrizione__c > IscrizioneList){
set<id> studId = new set<id>();
for(Iscrizione__c a : IscrizioneList){
studId.add(a.Contact__c);
}
list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
for(Iscrizione__c isc : IscrizioneList){
for(Contact con : lstCoustumObject){
if(isc.contact__c != NULL){
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
}else{
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;
}
}
}
update lstCoustumObject;
}
}
Please mark my answer as it will help to close this request.
All Answers
Your ask looks similar to https://developer.salesforce.com/forums/?id=9060G000000XhhYQAS . You can take the code snippet as example to get started.
There is nothing wrong with your logic just a minor change is required. According to salesforce, you cannot use trigger.new in delete context.
trigger ContaIscr on Iscrizione__c (after insert ,after delete) {
if(trigger.isAfter && trigger.isInsert){
ContalscrHelper.checkMethod(trigger.new);
}
else if(trigger.isAfter && trigger.isDelete){
ContalscrHelper.checkMethod(trigger.old);
}
public class ContalscrHelper{
public static void checkMethod(list<Iscrizione__c > IscrizioneList){
set<id> studId = new set<id>();
for(Iscrizione__c a : IscrizioneList){
studId.add(a.Contact__c);
}
list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
for(Iscrizione__c isc : IscrizioneList){
for(Contact con : lstCoustumObject){
if(isc.contact__c != NULL){
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
}else{
con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;
}
}
}
update lstCoustumObject;
}
}
Please mark my answer as it will help to close this request.