Trigger Scenario 5 In salesforce Names:
Trigger triggername on contact(after insert ,after update,after delete,after undelete)
Set<id> accids = new set<id>();
map<id,string> mapcon = new map<id,string>();
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);
}
}
for(contact con : [select id,lastname,accountid from contact where accountid IN: accids]){
if(mapcon.containsKey(con.accountid)){
mapcon.put(con.accountid,mapcon.get(con.AccountId) +','+con.LastName);
}
else{
mapcon.put(con.AccountId,con.LastName);
}
}
list<account> lstacc = new list<account>();
for(account acc :[select id,name,All_ASN_providers__c from account where id IN: mapcon.keySet()]){
acc.All_ASN_providers__c = mapcon.get(acc.id);
lstacc.add(acc);
}
update lstacc;
}
---------------------------------------
Trigger:
trigger RollupFutureTrigger on Contact (after insert,after update ,after delete,after undelete) {
set<id> accids = new set<id>();
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);
}
}
RollupFutureCLS.countContacts(accids);
RollupFutureCLS.AddnameofContacts(accids);
}
----------
Class:
Global class RollupFutureCLS {
@future
global static void countContacts(set<id> setId)
{
List<Account> lstAccount = [select id,NumberOfEmployees, (select id from contacts ) from account where id in :setId ];
for( Account acc : lstAccount )
{
acc.NumberOfEmployees = acc.contacts.size();
}
update lstaccount;
}
@future
global static void AddnameofContacts(set<id> setId)
{
list<account> lstaccount = new list<account>();
map<id,string> mapacc = new map<id,string>();
for(contact con : [select id,lastname,email,accountid from contact where accountid=:setId]){
if(mapacc.containsKey(con.accountid) ||test.isRunningTest()){
mapacc.put(con.accountid,mapacc.get(con.accountid)+','+con.lastname);
}
else{
mapacc.put(con.accountid,con.lastname);
}
}
for( Account acc : [select id,name,email__c,Z_Grandfather_for__c from account where id=:mapacc.keySet()] )
{
acc.Z_Grandfather_for__c = mapacc.get(acc.id);
lstaccount.add(acc);
}
update lstaccount;
}
}
--------
Test Class:
@IsTest
public class RollupFutureCLS_Test {
public static testmethod void TestRollupFutureMetod()
{
Account a = new Account();
a.Name = 'Test Account';
Insert a;
Contact cont = New Contact();
cont.FirstName ='Bob';
cont.LastName ='Masters';
cont.AccountId = a.Id;
cont.Phone ='9916767632';
cont.Email ='test@gmail.com';
Insert cont;
set<Id> setAccId = new Set<ID>();
setAccId.add(a.id);
Test.startTest();
RollupFutureCLS.countContacts(setAccId);
RollupFutureCLS.AddnameofContacts(setAccId);
Test.stopTest();
Account ACC = [select NumberOfEmployees from Account where id = :a.id LIMIT 1];
System.assertEquals ( Integer.valueOf(ACC.NumberOfEmployees) ,1);
}
}
---------------------------------------
Trigger:
trigger RollupFutureTrigger on Contact (after insert,after update ,after delete,after undelete) {
set<id> accids = new set<id>();
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);
}
}
RollupFutureCLS.countContacts(accids);
RollupFutureCLS.AddnameofContacts(accids);
}
----------
Class:
Global class RollupFutureCLS {
@future
global static void countContacts(set<id> setId)
{
List<Account> lstAccount = [select id,NumberOfEmployees, (select id from contacts ) from account where id in :setId ];
for( Account acc : lstAccount )
{
acc.NumberOfEmployees = acc.contacts.size();
}
update lstaccount;
}
@future
global static void AddnameofContacts(set<id> setId)
{
list<account> lstaccount = new list<account>();
map<id,string> mapacc = new map<id,string>();
for(contact con : [select id,lastname,email,accountid from contact where accountid=:setId]){
if(mapacc.containsKey(con.accountid) ||test.isRunningTest()){
mapacc.put(con.accountid,mapacc.get(con.accountid)+','+con.lastname);
}
else{
mapacc.put(con.accountid,con.lastname);
}
}
for( Account acc : [select id,name,email__c,Z_Grandfather_for__c from account where id=:mapacc.keySet()] )
{
acc.Z_Grandfather_for__c = mapacc.get(acc.id);
lstaccount.add(acc);
}
update lstaccount;
}
}
--------
Test Class:
@IsTest
public class RollupFutureCLS_Test {
public static testmethod void TestRollupFutureMetod()
{
Account a = new Account();
a.Name = 'Test Account';
Insert a;
Contact cont = New Contact();
cont.FirstName ='Bob';
cont.LastName ='Masters';
cont.AccountId = a.Id;
cont.Phone ='9916767632';
cont.Email ='test@gmail.com';
Insert cont;
set<Id> setAccId = new Set<ID>();
setAccId.add(a.id);
Test.startTest();
RollupFutureCLS.countContacts(setAccId);
RollupFutureCLS.AddnameofContacts(setAccId);
Test.stopTest();
Account ACC = [select NumberOfEmployees from Account where id = :a.id LIMIT 1];
System.assertEquals ( Integer.valueOf(ACC.NumberOfEmployees) ,1);
}
}
No comments:
Post a Comment