function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DevelopementDevelopement 

Please Help with this trigger

trigger ContactS on Contact (after delete, after insert)

{

Contact[] con;

con = Trigger.new;

Set<ID> acctIds = new Set<ID>();

Map<ID, Account> Update = new Map<ID, Account>([select Id,Count__c from Account where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();

if (acct.Count__c != conIds.size())
acct.Count__c = conIds.size();
}

update acctUpdate.values();

}

 

Thanks.

Vinit_KumarVinit_Kumar

On which fields of Account you want to populate Billing Contact Name, email and Phone number.If you can give me the fields ,I can then help you out.

DevelopementDevelopement
Fields are
Bname_c
Bphone_c
Bemail_c

Thanks
DevelopementDevelopement
Fields are
Bname_c
Bphone_c
Bemail_c

Eugene NeimanEugene Neiman

Why are you using a trigger?  I would add a rollup summary field on the Account object.  The roll up type would be "count" for records matching criterium Field/Operator/Value:  "CW_Contact_Type__c"  "contains" "Billing"

DevelopementDevelopement
I am out of the limit now to use rollup summary.
Eugene NeimanEugene Neiman

You already have 10 rollup summaries on Account?  Have you considered building a dashboard that accesses summary reports?

DevelopementDevelopement

i have tried all possible ways to update field on account page.So lastly i was left with no choice and only to use triggers.

Vinit_KumarVinit_Kumar

Hi,

 

Try below :-

 

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete,after update)
{
Contact[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;

Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id,AccountId,name,phone,email from Contact where AccountId in:acctIds and CW_Contact_Type__c like '%Billing%']);
Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id,Count__c from Account where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contact con : contactsForAccounts.values()) {
acct.Bname_c = contactsForAccounts.get(con.AccountId).Name;
acct.Bphone_c = contactsForAccounts.get(con.AccountId).phone;
acct.Bemail_c = contactsForAccounts.get(con.AccountId).email;
if (con.AccountId == acct.Id){
conIds.add(con.Id);
}
if (acct.Count__c != conIds.size()){
acct.Count__c = conIds.size();
}
}
}
update acctsToUpdate.values();
}