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
J80J80 

trigger Error

Any ideas? Its driving me mad!

Trigger
pbattissonpbattisson
It won't compile because your contact object does not have a field with that name on it. Check your Contact object and validate that the api name of the field is as you are expecting.
J80J80
its a custom object field. What should precede 'DRM_Contact_Employer__c'? 
pbattissonpbattisson
What are you trying to do? From your code it seems that when an Contact is inserted or updated you wish to set a field on the Contact to be the Name of the Account related to that Contact. Is that correct?
 
J80J80
yes thats correct. I think it should be custom object plus custom object field : Employment_History__c.DRM_Contact_Employer__c = con.Account.name;
pbattissonpbattisson
Yeah your code should probably be something like:
 
trigger PrimaryAccount on Contact(before insert, before update){
  List<Employment_History__c> histories = new List<Employment_History__c>();
  for(Contact con: trigger.new){
    Employment_History__c newHist = new Employment_History__c();
    newHist.DRM_Contact_Employer__c = con.Account.Name;
    newHist.Contact__c = con.Id; //This will need changing to be a correct relationship
    histories.add(newHist);
  }
  insert histories;
}

Note that you will probably want to relate your Employment HIstory object to the contact so that this makes sense (see the comment in the code). For the record, you could just create a formula field on the Employment History object to display the Account Name without the need for any code.