1

I wrote an integration app that imports contacts from an external system.

If the contacts already exists (ie. same name, or same email, or user selected) the data of the contact in salesforce is overwritten with the data of the contact in the ext system.

Therefore I would like to be sure that Field history tracking is enabled on those fields (Contact.FirstName,LastName,Email) and if not, enable it.

I saw here Field History : how to detect in apex which fields are tracked? that I can check wether the field is tracked or not, but as we could suppose :

Field is not writeable: FieldDefinition.IsFieldHistoryTracked

Is there any way to enable it programmatically ?

I saw that the Contact.object file changed when I manually activated the history on email field from

<enableHistory>false</enableHistory>
...
<fields>
    <fullName>Email</fullName>
    <trackFeedHistory>false</trackFeedHistory>
</fields>

to

<enableHistory>true</enableHistory>
...
<fields>
    <fullName>Email</fullName>
    <trackFeedHistory>false</trackFeedHistory>
    <trackHistory>true</trackHistory>
</fields>

So I guess deploying this file might be a way (even if I don't really know how), but how to do that in a package installation ?

  • 1
    You could try deploying updated metadata through the metadata api. An easy way to upload metadata is through workbench. – battery.cord Oct 11 '17 at 13:21
  • 1
    Looks like you're in luck, a metadata api for apex was released in summer '17 Source ... or not, seeing as it only provides two types of metadata.. yikes. – battery.cord Oct 11 '17 at 13:32
  • 2
    Looks like you'd need to use the metadata api wrapper, which requires some setup, and basically calls out to the webservice metadata api from apex. – battery.cord Oct 11 '17 at 13:33
  • 1
    Try modifying a .object file, and deploying it through workbench. If this works, and enables field tracking, then setup the metadata api and write some code to callout when appropriate. – battery.cord Oct 11 '17 at 13:35
  • 1
    I'm with battery.cord here - you should be able to use the Apex Metadata API wrapper as it will use the sessionId of the running user (presumably the package installer, a sysad) – cropredy Oct 11 '17 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.