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
Dean BlanchDean Blanch 

HELP...I have created an apex trigger in sandbox to add the field "Number of Contacts" in to the account!

I have created an apex trigger in sandbox to add the field "Number of Contacts" in to the account in sandbox worked perfectly fine, however when deployed to production the field is empty.
Dean BlanchDean Blanch
THE CODING USED IS.....

trigger  NumberOfContacts on Contact (after insert) {
set<id> setid = new set<id>();

list<account> listaccount = new list<account>();
for(contact con : trigger.new){

setid.add(con.accountid);
}

For(account acc : [select id,name,Number_of_Contacts__c, (select id from contacts)from account where id=:setid]) {
Account ac1 = new account();

ac1.id=acc.id;

ac1.Number_of_Contacts__c= acc.contacts.size();

listaccount.add(ac1);
}

update listaccount;



}
Jainam ContractorJainam Contractor
Hi Dean,

I tried this Trigger in my Developer Instance and then i deployed the same in my other instance using Packaging but i did not find any problem and the Trigger works fine in both the Orgs.

I don''t know why you are getting such problem in your Production Org. Can you please check if there is something else which is again updating the Record and add some Debug statements in you Trigger to check the ListDetails before the Actual Update Something like : system.debug('ListAccount-----'+listaccount); and then check the Debug log.

This way you might be able to get the reason behind the same.

Please let me know if you need some more assistance.

Thanks,
Jainam Contractor,
Saleforce Consultant,
Varasi LLC
www.varasi.com
Dean BlanchDean Blanch
I have just set my logs for debugging im not sure if this process takes some time?

Would I have to set up any of the following to bring through results...?
FieldsNew
Object
Validation RulesNew
Approvals

Cheers 
Dean