+ Start a Discussion

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)
  /*  for(ContentWorkspaceDoc cw:cow)
    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;
   update updatecontent;


Best Answer chosen by Admin (Salesforce Developers) 

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)
List<Opportunity> projId=[select Id,Project__c from Opportunity where id in :oppIds];
for (ContentVersion cv : Trigger.new)
    for (Opportunity o : projId)