You need to sign in to do that
Don't have an account?
I need apex class to get documentlink field
Hi all,
I need apex class to get documentlink field called LinkedEntityId.
This is my create task when document upload.
trigger CreateTask on ContentVersion (after insert)
{
List<Task> insertTask = new List<Task>();
Task newTask = new Task();
for(ContentVersion newCase : Trigger.new)
{
newTask.subject = 'Document Expiration';
newTask.WhatId = newCase.Id;
newTask.ActivityDate = newCase.Expiry_Date__c;
newTask.ownerId = newCase.OwnerId;
newTask.status = 'Not started';
newTask.Priority = 'Normal';
insertTask.add(newTask);
if(insertTask.isEmpty())
{
try{
}catch(DmlException de)
{
System.debug(de);
}
}
}
}
I need to assign LinkedEntityId this id to createTask . Trigger -> WhoId.
Is anyone help?
Thanks in advance.
In the below line of code, it is not recommended to use SOQL query inside for loop because of pre-defined governor limits.
newTask.WhatId =[select LinkedEntityId from ContentDocumentLink where ContentDocumentId IN : newCase.ContentDocumentId].Id;
If you want to assign the Id directly without verifying whether it is whoId or WhatId, you can use the following line of code:
newTask.WhatId =[select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: newCase.ContentDocumentId].LinkedEntityId;
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com (http://www.sachinsf.com)
All Answers
Please follow the following steps:
Step 1: You need to get the ContentDocumentId from the content version.
Step 2: Query LinkedEntityId FROM ContentDocumentLink where ContentDocumentId exists in the above list.
Step 3: Check the LinkedEntityId whether it is whoId (Id of the object : Contact or Lead) or What Id by using the below line of code:
String sObjName = LinkedEntityId.getSObjectType().getDescribe().getName();
Step 4: If the LinkedEntityId belongs to Contact or Lead, then assign it to the WhoId field otherwise, assign it to the whatId field.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
Thanks for your replay.
I just want assign WhoId/WhatId value in trigger directly.In the above code I just add the following query.Can you please share is this possible?
newTask.WhatId =[select LinkedEntityId from ContentDocumentLink where ContentDocumentId IN : newCase.ContentDocumentId].Id;
In the below line of code, it is not recommended to use SOQL query inside for loop because of pre-defined governor limits.
newTask.WhatId =[select LinkedEntityId from ContentDocumentLink where ContentDocumentId IN : newCase.ContentDocumentId].Id;
If you want to assign the Id directly without verifying whether it is whoId or WhatId, you can use the following line of code:
newTask.WhatId =[select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: newCase.ContentDocumentId].LinkedEntityId;
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com (http://www.sachinsf.com)