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
Jonathan Wolff 7Jonathan Wolff 7 

Error on ContentDocument Trigger

Hello, I tried to build a trigger myself. I have a custom Object Mediathek__c with a custom field ContentDocumentId__c. I want to achive that, when a new file is uploaded to a record of this object, the contentDocumentId of the file is safed within the field. 

My try:
trigger ContentDocumentIdTrigger on ContentDocumentId ( after insert, after update, after delete ) {

    List<ContentDocumentId> cdid = ( Trigger.new == null ? Trigger.old : Trigger.new );


    for ( ContentDocumentId cdid : cdid ) {
        parentIds.add( cdl.LinkedEntityId );
}

    for ( List<Mediathek__c> media : [ SELECT Id, ( SELECT Id FROM ContentDocument LIMIT 1 ) FROM Mediathek__c  ] ) {
        
        for ( Mediathek__c m : media ) {
            m.ContentDocumentID__c = ( m.ContentDocumentId );
        }

        update media;

    }

}

 
Best Answer chosen by Jonathan Wolff 7
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jonathan,

try with below code.
trigger ContentDocumentIdTrigger on ContentDocumentLink(after insert){

map<id,id> parentids = new map<id,id>();

for(ContentDocumentLink cdl:trigger.new){
parentids.put(cdl.LinkedEntityId,cdl.ContentDocumentId);
}

List<Mediathek__c> ContentDocumentIdupdate = new List<Mediathek__c>();

for(Mediathek__c mt:[select id, from Mediathek__c where id IN:parentids.keyset()]){

if(parentids.containskey(mt.id)){
mt.ContentDocumentID__c = parentids.get(mt.id);
ContentDocumentIdupdate.add(mt);
}
}
update ContentDocumentIdupdate;
}

If this helps, Please mark it as best answer.

Thanks!!

 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Jonathan,

You need to write the trigger on ContentDocumenLink object.

try with below code.
trigger ContentDocumentIdTrigger on ContentDocumentLink(after insert){

map<id,id> parentids = new map<id,id>();

for(ContentDocumentLink cdl:trigger.new){
parentids.put(cdl.LinkedEntityId,cdl.ContentDocumentId);
}

List<Mediathek__c> ContentDocumentIdupdate = new List<Mediathek__c>();

for(Mediathek__c mt:[select id, from Mediathek__c where id IN:parentids]){

if(parentids.containskey(mt.id)){
mt.ContentDocumentID__c = parentids.get(mt.id);
ContentDocumentIdupdate.add(mt);
}
}
update ContentDocumentIdupdate;
}

If this helps, Please mark it as best answer.

Thanks!!
 
Jonathan Wolff 7Jonathan Wolff 7
Hello ankaiah,

thank you so much! I only got the error in line 11: IN operator must be used with an iterable expression 

Can i fix this somehow?

Kind regards!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jonathan,

try with below code.
trigger ContentDocumentIdTrigger on ContentDocumentLink(after insert){

map<id,id> parentids = new map<id,id>();

for(ContentDocumentLink cdl:trigger.new){
parentids.put(cdl.LinkedEntityId,cdl.ContentDocumentId);
}

List<Mediathek__c> ContentDocumentIdupdate = new List<Mediathek__c>();

for(Mediathek__c mt:[select id, from Mediathek__c where id IN:parentids.keyset()]){

if(parentids.containskey(mt.id)){
mt.ContentDocumentID__c = parentids.get(mt.id);
ContentDocumentIdupdate.add(mt);
}
}
update ContentDocumentIdupdate;
}

If this helps, Please mark it as best answer.

Thanks!!

 
This was selected as the best answer