+ Start a Discussion

writing triggers

haven't a clue how to figure out how to write triggers.  none of the examples have enuff explanation to get me going.
i want to write a trigger that does the following:
the trigger needs to fire on update of contacts.Account
it would have to look up the accounts.organization_code__c where accounts.Name=contacts.Account
and make contact.org_code__c =account.organization_code__c
any takers :)
Hello cmctam,

With the information you have given us it can probably be done with either formula fields or workflow rule. The workflow rule can be triggered when the Account associated with a Contact changes, or other criteria, and will pulll the Account.organization_code__c and place it in the field on your Contacts. If you need this done for a specific reason with Apex then it will take more work to process and require the testing before it can be done within a production instance. If you want something like this built then please email me or go to my site to contact me.
hi thanks
i tried looking at workflows and formula fields for this before but didn't come up with a solution that works
the trigger needs to be something like below but i can't find documentation or examples that make it clear to me what i'm missing.
trigger update_org_code on Contact (after update) {

    private Contact[] newContact= Trigger.new;
    private Contact[] oldContact= Trigger.old
    //creates a variable reference to the object that fired the tirgger
    if (newContact.Account!=oldContact.Account)
        for (Account a : [select organization_code__c
                      from account where Name in :newContact.Account]){
        update newContact
Actually, with the new cross-object formulas, couldn't you just create a formula field on Contact that equals Account.org_code__c? No need for Triggers or Workflow.