Trigger Scenario 6 In salesforce ROLLUP SUMMARY for COUNT:
//Trigger Best practice for count
trigger CountContacts on contact (after delete,after update, after insert, after undelete) {
list<account> lstacc = new list<account>();
set<id> accids = new set<id>();
if(trigger.isInsert && trigger.isafter){
for(contact con : Trigger.new){
accids.add(con.accountid);
}
}
else if(trigger.isDelete && trigger.isafter){
for(contact con : Trigger.old){
accids.add(con.accountid);
}
}
else if(trigger.isUnDelete && trigger.isafter){
for(contact con : Trigger.new){
accids.add(con.accountid);
}
}
else if(trigger.isUpdate && trigger.isafter){
for(contact con : trigger.new){
if(trigger.oldmap.get(con.id).accountid!=con.accountid){
accids.add(con.accountid);
accids.add(trigger.oldmap.get(con.id).accountid);
}
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), accountid FROM contact where accountid IN :accids GROUP BY accountid ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get('accountid');
Integer count = (INTEGER)ar.get('expr0');
account cust1 = new account(Id=custid);
cust1.NumberofLocations__c = count;
lstacc.add(cust1);
}
update lstacc;
}
-------------------------------------------------------------------
Trigger Scenario for Sum of Amount of all child to parent
trigger TestingContactTrigger on Contact (before update,after insert,after update,after delete,after undelete) {
list<account> lstacc = new list<account>();
set<id> ids = new set<id>();
if(Trigger.isafter && (trigger.isinsert || trigger.isupdate || trigger.isundelete)){
for(contact con : trigger.new){
ids.add(con.accountid);
}
}
if(trigger.isafter && trigger.isdelete){
for(contact con : trigger.old){
ids.add(con.AccountId);
}
}
list<aggregateresult> lstagr =[Select SUM(Amount__c) totalSum, accountid From contact Where accountid IN : Ids GROUP BY accountid];
for(aggregateresult ag : lstagr){
id accid = (ID)ag.get('accountid');
double amount = (DOUBLE)ag.get('totalsum');
account acc = new account(id = accid);
acc.Total__c = amount;
lstacc.add(acc);
}
update lstacc;
}
-------------------------------------------------------------------
---------------------------------------------------------------------Another way-----
Trigger triggername on contact(after insert,after update,after delete,after undelete){
Set<id> accids = new set<id>();
map<id,list<contact>> mapcon = new map<id,list<contact>>();
if(trigger.isafter &&(trigger.isinsert ||trigger.isupdate || trigger.isundelete)){
for(contact con : trigger.new){
accids.add(con.AccountId);
}
}
if(trigger.isafter && trigger.isdelete){
for(contact con : trigger.old){
accids.add(con.AccountId);
}
}
// list<contact> lstcon = [select id,lastname,amount1__c,accountid from contact where accountid IN: accids];
// integer count = lstcon.size();
list<account> lstacc = new list<account>();
for(account acc :[select id,name,NumberofLocations__c,(select id from contacts)from account where id IN:accids]){
acc.NumberofLocations__c = acc.contacts.size();
lstacc.add(acc);
}
update lstacc;
}
----------------------------------------------------
Rollup summary Amount
trigger RollupOppAmountTrigger on Opportunity (after insert,after update,after delete,after undelete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<string> acctIds = new Set<string>();
if(trigger.isUpdate || trigger.isInsert || trigger.isundelete){
for(Opportunity oppty : trigger.New){
acctIds.add(oppty.AccountId);
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
acctIds.add(oppty.AccountId);
}
}
if(acctIds.size() > 0){
for(Opportunity oppty : [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds]){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
for(Account acct : [SELECT NumberofLocations__c FROM Account WHERE Id IN: acctIds]){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.NumberofLocations__c = totalOpptyAmount;
acctList.add(acct);
}
update acctList;
}
}
//Trigger Best practice for count
trigger CountContacts on contact (after delete,after update, after insert, after undelete) {
list<account> lstacc = new list<account>();
set<id> accids = new set<id>();
if(trigger.isInsert && trigger.isafter){
for(contact con : Trigger.new){
accids.add(con.accountid);
}
}
else if(trigger.isDelete && trigger.isafter){
for(contact con : Trigger.old){
accids.add(con.accountid);
}
}
else if(trigger.isUnDelete && trigger.isafter){
for(contact con : Trigger.new){
accids.add(con.accountid);
}
}
else if(trigger.isUpdate && trigger.isafter){
for(contact con : trigger.new){
if(trigger.oldmap.get(con.id).accountid!=con.accountid){
accids.add(con.accountid);
accids.add(trigger.oldmap.get(con.id).accountid);
}
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), accountid FROM contact where accountid IN :accids GROUP BY accountid ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get('accountid');
Integer count = (INTEGER)ar.get('expr0');
account cust1 = new account(Id=custid);
cust1.NumberofLocations__c = count;
lstacc.add(cust1);
}
update lstacc;
}
-------------------------------------------------------------------
Trigger Scenario for Sum of Amount of all child to parent
trigger TestingContactTrigger on Contact (before update,after insert,after update,after delete,after undelete) {
list<account> lstacc = new list<account>();
set<id> ids = new set<id>();
if(Trigger.isafter && (trigger.isinsert || trigger.isupdate || trigger.isundelete)){
for(contact con : trigger.new){
ids.add(con.accountid);
}
}
if(trigger.isafter && trigger.isdelete){
for(contact con : trigger.old){
ids.add(con.AccountId);
}
}
list<aggregateresult> lstagr =[Select SUM(Amount__c) totalSum, accountid From contact Where accountid IN : Ids GROUP BY accountid];
for(aggregateresult ag : lstagr){
id accid = (ID)ag.get('accountid');
double amount = (DOUBLE)ag.get('totalsum');
account acc = new account(id = accid);
acc.Total__c = amount;
lstacc.add(acc);
}
update lstacc;
}
-------------------------------------------------------------------
---------------------------------------------------------------------Another way-----
Trigger triggername on contact(after insert,after update,after delete,after undelete){
Set<id> accids = new set<id>();
map<id,list<contact>> mapcon = new map<id,list<contact>>();
if(trigger.isafter &&(trigger.isinsert ||trigger.isupdate || trigger.isundelete)){
for(contact con : trigger.new){
accids.add(con.AccountId);
}
}
if(trigger.isafter && trigger.isdelete){
for(contact con : trigger.old){
accids.add(con.AccountId);
}
}
// list<contact> lstcon = [select id,lastname,amount1__c,accountid from contact where accountid IN: accids];
// integer count = lstcon.size();
list<account> lstacc = new list<account>();
for(account acc :[select id,name,NumberofLocations__c,(select id from contacts)from account where id IN:accids]){
acc.NumberofLocations__c = acc.contacts.size();
lstacc.add(acc);
}
update lstacc;
}
----------------------------------------------------
Rollup summary Amount
trigger RollupOppAmountTrigger on Opportunity (after insert,after update,after delete,after undelete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<string> acctIds = new Set<string>();
if(trigger.isUpdate || trigger.isInsert || trigger.isundelete){
for(Opportunity oppty : trigger.New){
acctIds.add(oppty.AccountId);
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
acctIds.add(oppty.AccountId);
}
}
if(acctIds.size() > 0){
for(Opportunity oppty : [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds]){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
for(Account acct : [SELECT NumberofLocations__c FROM Account WHERE Id IN: acctIds]){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.NumberofLocations__c = totalOpptyAmount;
acctList.add(acct);
}
update acctList;
}
}
No comments:
Post a Comment