You need to sign in to do that
Don't have an account?
Ashw
how to put this trigger in class and call it from trigger
As a new comer to salesforce i know how to write a trigger and class but how to conver the below code to class and call it in trigger, so please help me how to do this. Thanks guys in Advance
Set<Id> accids = new Set<Id>(); if(trigger.isInsert || trigger.isUpdate ||trigger.isUndelete){ for(Contact con : trigger.new){ accids.add(con.accountId); } } if(trigger.isUpdate || trigger.isdelete){ for(Contact con : trigger.old){ accids.add(con.accountId); } } List<account> lst = [Select id, No_Of_contacts__c,(Select id from Contacts) from account where Id in: accids]; for(Account acc : lst){ acc.no_of_contacts__c = acc.contacts.size(); } update lst;
Trigger:
Trigger UpdateTest on Contact(Before Insert,Before Update, Before Delete, After Insert, After Update, After Delete, After Undelete){
if(trigger.isInsert || trigger.isUpdate ||trigger.isUndelete){
utilityclass util=new utilityclass();
util.updatecontact(trigger.new);
}
}
Class:
Public Class Utilityclass{
Public static Void updatecontact(List<Contact> contacts){
Set<Id> accids = new Set<Id>();
for(Contact con : contacts){
accids.add(con.accountId);
}
}
}
Thanks for your response, so there is no need of quering account as i did in trigger