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

Trigger help

In contact object i have field called MyExpenses(hidden field). This field is populated from CompanyExpenses/no.of.contacts

There two contacts now, So for each contact myExpeness is $100...companyExpensesis $200(constant)

Now if i create another new  contact then Myexpenses filed should be 200/3..

So i need to write trigger to update this hidden field when i create any new contact..

Please help to write this trigger

could you please tell where companyexpense field is present in contact or account?
srlawr uksrlawr uk
I would probably consider completing this process declarivitly rather than using a trigger? I am pretty sure you can do this.

First, create a roll up summary on the Company object called "Number of Contacts" - this will, quite simply, be a SUM of the CONTACTs on this company. From your description, you already have the CompanyExpenses (constant) on this record.

Now, in the Contact object, add a custom formula field called "MyExpenses" and make it be the Parent record CompanyExpenses / Number of Contacts... something like this

User-added image

This will then show the mathematical sum you are after..

Of course there are a number of possible limitations here, so - sorry if this is useless information to you! If your contact/company relationship is a look up - you can't roll it up. If you want to do cross-relationship formulas or rollups on the new "MyExpenses" field, you also may be stuck.. etc.etc.

If this is the case, and you are determined to use a trigger, give me a shout and I will try to throw some pseudo-code together for you :)

srlawr uksrlawr uk
balls... the roll up summary would probably be a COUNT not a SUM :D sorry!! Need more coffee.
Hi Raysfdc1988,

Write a trigger on Contact object event is after insert. 

trigger updatingContact on Contact(after insert){
    set<id>accountidSet = new set<id>();
    list<Account> accountList = new list<Account>();
   map<id, list<Contact>> id_contcatlist = new map<id, list<Contact>>();
   list<Contact> listofContcat = new list<Contact>();

    for(Contact c:{
        accountidSet .add(c.AccountId);    
  accountList = [select id, Name,(select id, name,Myexpenses__c from Contacts) from Account where id IN: accountidSet];

  for(Account account: accountList){
     id_contcatlist.put(, account.Contacts);    

     Contact con = new Contact(id=,Myexpenses__c  = 0);
          con.Myexpenses__c = 300/ id_contcatlist.get(c.Accountid).size();
          listofContcat .add(con);
     update listofContcat ;

mark it solved if it solves your question.

srlawr uksrlawr uk
Hmm. Note the following from that (or any) trigger approach:

1) If you have an account with 2 contacts, and you add a third, the two existing contacts are not updated (ie. the first two will still show 100 each and the new one will show 66.66)

2) If you delete a contact, myExpenses will not be updated (so if you have 4 contacts at 50 each, and you delete one, the others will not be updated to 66.66)

3) If you edit the CompanyExpenses amount on the account, none of the contacts are updated

4) If you edit a contact to re-parent it to a new account, none of the expense values (of the old and new siblings, or old and new accounts) will be updated.

...and in fact many, many more case scenarios that are going to go completely under the radar and be missed.
srlawr uksrlawr uk
(that said, technically, from your requirement, that kinda does meet it, semantically!!)
Sorry Guys...May be my question is not clear ...
Lemme Explain in dateil....
In contact(employee) object i have hidden field called Myexpenses:
and I have  another object: Expenses ...I will enter this data only once at time... Say For the month June its-$500

this expense should to devided for each contact(employee)..but contact and Expenses object are non-related i wrote trigger on Expenses which will populate the myExpencses field value/no of contacts (AVG) on contact..Its working fine..

Ex: For month June there 5 employees...then lets create expense for that month ..say $500 then..from trigger myexpenses field is updated to $100(500/5)..

Now i ll add new contact ..then the myexpenses field should autoupdate to 500/6(no of contacts)...

So i need to write a trigger on contact ,which will check the all existing contacts myexpenses and adds them to a  total(500) then it update all contacts myexpense fields with value 500/6...
srlawr uksrlawr uk
ok... how about something like this then?
(this is handtyped, and not compiler tested, as I don't have the fields etc)

trigger rerollExpenses on Contact (before insert, before update, before delete)
    // get all existing contacts
    List<Contact> allContacts = [SELECT Id, Name, MyExpenses FROM Contact];
    // augment with new data (might want to look into duplicates here...?)
    Double expensesTotal = 0;
    // this gets the current total
    for(Contact thisContact : allContacts)
        expensesTotal = expensesTotal + thisContact.MyExpenses;

    // update all contacts with new calculated total
    for(Contact thisContact : allContacts)
        thisContact.MyExpenses = expensesTotal / allContacts.size();


limitations here I think you will need to address to make this fully functioning...

I'm not sure how the list will react on an update with regard to duplicates in the list (see comment)

This considers ALL contacts in the org to be under one and the same expenses total, right?

There are some inefficiencies in there (such as calculating/calling size() in the last loop - I would extract that to a value once and set it many times... (that is a good-code test I have left you ;) ))

This doesn't consider undelete scenarios... (bleugh)

This does not consider govenor limits if you have 2,000,000 contacts.