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
John King20John King20 

Can't find where the field is updating

Hi, 
I have a new field I created. This field is only included in one trigger and nowhere else. The field updates fine, but immediately after, the value of the field changes back to the previous value. The field is not referenced anywhere else so I'm thinking there must be some process that is updating all fields on the object. Is there a way to find this? 

The field is definitely not referenced anywhere else in our code. 
AnkaiahAnkaiah (Salesforce Developers) 
Hi John,

Can you share your trigger code?

because of that field change again trigger might be firing.

Thanks!!
John King20John King20
HI,
The trigger is only firing once from what I can see. Checking the logs etc. I know there may be some issues with the trigger, but it is working in most scenarios, just one specific scenario where I think something else is being triggered after.
MyField is always updating, but in this specific scenario it is reverting to previous value immediately. I'm wondering is there some sync jon or something happening after. But I don't know how to find it.
trigger ContentDocumentLinkAfterTrigger on ContentDocumentLink (after insert, after delete) {
    Set<Id> parentDoc = new Set<Id>(); 
    if (Trigger.isInsert||Trigger.isDelete) {
        List<ContentDocumentLink> cdls = Trigger.isInsert ? Trigger.new: Trigger.old;

        for (ContentDocumentLink cdl : attchs) {
            if (cdl.LinkedEntityId.getSObjectType() == Document.SObjectType) {
                parentDoc.add(cdl.LinkedEntityId);
            }
        }
        if (!parentDoc.isEmpty()) {
            Map<Id, Integer> docAttachmentCount = new Map<Id, Integer>();
            for (ContentDocumentLink cdl: [select Id, LinkedEntityId from ContentDocumentLink where LinkedEntityId in :parentDoc]) {
                Integer count = docAttachmentCount.get(c.LinkedEntityId);
                if (count == null) count = 0;
                count++;
                docAttachmentCount.put(cdl.LinkedEntityId, count);
            }

            Map<Id, ContentDocumentLink> documentAttachment = new Map<Id, ContentDocumentLink>();
            for (ContentDocumentLink cdl: [select Id, ContentDocumentId, LinkedEntityId from ContentDocumentLink where LinkedEntityId in :parentDoc]) {
                documentAttachment.put(cdl.LinkedEntityId, cdl);
            }
            
            List<Document> docList = [
                    select Id
                    from Document
                    where Id in : parentDoc];

                for (Document doc: docList){
                    if(documentAttachmentCount.get(doc.Id)>0){
                        doc.MyField = documentAttachment.get(doc.Id).contentDocumentId;
                    }
                    else{
                        doc.MyField = null;
                    }
                }
            update docList;
        }
    }
}