+ Start a Discussion

Want to write a trigger for cross object

I have field in account named pick (picklist field) and I want to populate that field automatically in contact , a field name called accountpickvalues




For that you should write cross object formula on Contact object and formula (return type is text) is


CASE(Account.pick__c, "value1", "value1", "value2", "value2","None")





No I want that if new contact is been created , we selct an account also to link contact to account through lookup,
A trigger to popluate one of the field of account in the contact,
I want to use triggers only


You don't need to write a trigger for that.


There could be two scenarios ;


1. When contact is getting created from Related List at Account's view page.

2. When contact is getting created from Contacts Home page.


In both scenarios you can populate contact field from account's field value by using an internediate page.


1. Get the html Id of the contact field.

2. get the url of Contact Edit page.

3. First override your 'New Contact'  button to redirect to your visualforce page.

4. For First scenario :  your account field will be set.

    For second scenario : your account field will not be set.

    You have to write an action method in your visualforce page controller which checks for if account is null or not if account is  null then in same    

     visualforce page ask user to select account .

     Now you have account Id so query for account's field which you want to populate at contact's field.

5. create a pageReference from step 2 url.

6. Need to add parameter to that pagereference where key will 'htmlId' of contact's field and value will be Account's Field vlaue.

7. Then you need to redirect to above pagereference and your contact field will be prepopulated with account's field value.





Blog: Apex on Force.com - An Object Oriented Approach