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
Sumant KuchipudiSumant Kuchipudi 

How to update LinkedEntityId in ContentDocumentLink ?

HI,
I need to pull the all ContentDocumentLink recrods related to All Contacts (30K) and update LinkedEntityId (related to Contact) with new custom object record ids. while updating ContentDocumentLink record  I got below error
LinkedEntityIdUnable to create/update fields: LinkedEntityId. Please check the security settings of this field and verify that it is read/write for your profile or permission set.INVALID_FIELD_FOR_INSERT_UPDATEfalse
I'm using python script to do this (but apex solution is also fine). please check below code
query_result  = self.svc.query("select id from contact where Student__c=true and id='00336000014TJv9'")
            cnt=0
            contList=[]
            while True:
                for row in query_result[self.sf.records:]:
                    cnt+=1
                    contList.append(str(row[1]))
                if str(query_result[self.sf.done]) == 'true':
                    break
                query_result = self.svc.queryMore(str(query_result[self.sf.queryLocator]))
            for con in contList:
                links = self.svc.query("select id,ContentDocumentid from ContentDocumentLink where LinkedEntityId = '"+str(con)+"' limit 1");
                if (links!=None and len(links)>0):
                    for link in links[self.sf.records:]:
                        writer.writerow({'Contac_ID': str(con), 'Document_ID': str(link[3]),'Document_Linked_ID': str(link[1])})
                        emNotes = {
                            'type' : 'Em_Note__c',
                            'Contact__c' : str(con),
                            'Name' : str(link[3])
                        }
                        results = self.svc.create(emNotes )
                        emNotesID= results[0]
                        contentDocumentLink = {
                            'type' : 'ContentDocumentLink',
                            'id' : str(link[1]),
                            'LinkedEntityId' : str(emNotesID)
                        }
                        self.svc.update(contentDocumentLink); # I guess the error is here
Please advice


 
Raj VakatiRaj Vakati
Try like this 
 
links = self.svc.query("select id,ContentDocumentid from ContentDocumentLink where LinkedEntityId = '"+str(con.id)+"' limit 1");

Complete code
 
query_result  = self.svc.query("select id from contact where Student__c=true and id='00336000014TJv9'")
            cnt=0
            contList=[]
            while True:
                for row in query_result[self.sf.records:]:
                    cnt+=1
                    contList.append(str(row[1]))
                if str(query_result[self.sf.done]) == 'true':
                    break
                query_result = self.svc.queryMore(str(query_result[self.sf.queryLocator]))
            for con in contList:
                links = self.svc.query("select id,ContentDocumentid from ContentDocumentLink where LinkedEntityId = '"+str(con.id)+"' limit 1");
                if (links!=None and len(links)>0):
                    for link in links[self.sf.records:]:
                        writer.writerow({'Contac_ID': str(con), 'Document_ID': str(link[3]),'Document_Linked_ID': str(link[1])})
                        emNotes = {
                            'type' : 'Em_Note__c',
                            'Contact__c' : str(con),
                            'Name' : str(link[3])
                        }
                        results = self.svc.create(emNotes )
                        emNotesID= results[0]
                        contentDocumentLink = {
                            'type' : 'ContentDocumentLink',
                            'id' : str(link[1]),
                            'LinkedEntityId' : str(emNotesID)
                        }
                        self.svc.update(contentDocumentLink); # I guess the error is here

Sumant KuchipudiSumant Kuchipudi
Hey Raj, thanks for reply.
I looked at your change that you are suggesting to use "con.id" but "con" is ID, it won't work.
Sumant KuchipudiSumant Kuchipudi
I found another way, i.e. get all fields from ContentDocumentLink, delete the ContentDocumentLink record, and create a new ContentDocumentLink record with new LinkedEntityId, this is working