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
Prady01Prady01 

Trigger on content object

Hello Everybody.... Thanks to anyone who can lend me some insights on my problem..

 

        First of all i am writting a simple trigger on content object to update a custom lookup field, but not seem able to query the fields on content object, when i generated the API in the org i could see that all the custom fields are under the object name ContentVersion....

 

         Wat i am trying to do is, there a an opportunity lookup field on content object and then there is another field called project lookup field on content object, I want to populate this project field by getting the data from related opportunity(from which i want to pull the data to populate project field on content object)..  here is my code.. Any suggestions wil be very helpful thanks..

 

 

 

trigger ProjectName on ContentVersion(after insert)
{
    
    set<id> oppid = new set<id>();
    set<id> contid = new set<id>();
 //   set<id> conwsid =new set<id>();
    List<ContentVersion> content = new List<ContentVersion>();
    content=[select ContentDocumentId,Opportunity__c from ContentVersion where Id in:Trigger.new];
    // List<ContentWorkspaceDoc> cow = new List<ContentWorkspaceDoc>();
   //cow=[select ContentWorkspaceId from ContentWorkspaceDoc where ContentDocumentId in:contid ];
    
    for(ContentVersion c:content)
    {
   
    contid.add(c.ContentDocumentId);
    oppid.add(c.Opportunity__c);
    }
  /*  for(ContentWorkspaceDoc cw:cow)
    {
    conwsid.add(cw.ContentWorkspaceId);
    }*/
    
    
    List<ContentVersion> updatecontent = new List<ContentVersion>();
    List<Opportunity> opp = new List<Opportunity>();
    opp = [select id, DreamTeam_Project__c from opportunity where id in: oppid ];
    List<ContentVersion> cont = new List<ContentVersion>();
    cont=[select id,DreamTeam_Project__c from ContentVersion where id in:contid and opportunity__c in:oppid and Id in:contid];//and RecordTypeId=:'00h400000013I6S'];
    for(Opportunity op:opp){
    for(ContentVersion cv:cont){
    cv.DreamTeam_Project__c = op.DreamTeam_Project__c;
   
    updatecontent.add(cv);
    }
   update updatecontent;
    }
   
    }
    
    

 

Best Answer chosen by Admin (Salesforce Developers) 
Prady01Prady01

Whops!!! Sorry people the above code was a waste of time and it worked with a much simpler code and its very easy... This could be done by just assiging the values instead of updating the whole record again here is the code...

trigger ProjectName on ContentVersion (before update,before insert) {

Set<Id> oppIds = new Set<Id>();
for (ContentVersion cv : Trigger.new)
oppIds.add(cv.opportunity__c);
List<Opportunity> projId=[select Id,Project__c from Opportunity where id in :oppIds];
for (ContentVersion cv : Trigger.new)
    for (Opportunity o : projId)
        if(cv.opportunity__c==o.Id)
            cv.Project__c=o.Project__c;


}