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 for Primary Account

Hi Folks
I require code for the following scenario.I have a custom object called Employment history. I need to:
Build a  trigger which identifies the primary account (likely to be the latest active employment record) and adds this value to the parent account field
Ensure this field isn't visible to standard users
Ensure this field is wiped if their employment history is deleted or if all records are set to inactive.

Any ideas?

Many thanks 
Vishal NegandhiVishal Negandhi
Why don't you try writing this trigger and then coming back in case of any issues faced?
This will help you learn apex development.

Refer this if you have just started apex development:

Ive tried this Is this anywhere close?

trigger PrimaryAccount on Account (before update, before insert) { 
//Set of Account Ids 
 for(Account fcon : Trigger.New)
    if(fcon.DRM_Active_c != TRUE)
        fcon.DRM_Contact_employer = fcon.ParentId;
Vishal NegandhiVishal Negandhi
Ok, so here what you're doing is:
every time an Account is updated or inserted, you check for the DRM Active flag.
If it's true, you assign the Parent Account to DRM Contact Employer (this should also be a lookup to Account). 

I don't think this is what your requirement is. How have you identified the Primary Account here?
Hi Vishal
It should be everytime a Contact is updated or inserted you check the DRM Actiive flag. 
If its true you assign the DRM Contact Employer as the Parent Account.
The Primary account will be whatever Account the DRM Contact Employer is. 

Hope this makes more sense.
Sorry I meant Primary account in the second line!
Hi Vishal. Have you had any time to think about my last two comments? Your help would be gratefully appreciated!