Skip to main content
1 of 2

Object reference not set to an instance of an object. (Attachment Trigger to Convert to Files)

I've been stuck with this trigger for 2 days now. Essentially the trigger will convert any attachments that are generated in the Agreement Object to Salesforce Files. However upon generating attachments this specific error keeps getting thrown. "Object reference not set to an instance of an object"


    public static void afterInsert(List<Attachment> attachinserted){
        
        Set<ID> agreementset = New Set<ID>();
        List<ID> attachedIDs = New List<ID>();
        for(Attachment attach : attachinserted){
            if(attach.ParentId.getSobjectType() == Apttus__APTS_Agreement__c.SobjectType){
               agreementset.add(attach.ParentId);
               attachedIDS.add(attach.Id);
            }
        }
        //Holds agreements with new Attachments      
        List<Apttus__APTS_Agreement__c> agreementAUS = [Select Id FROM Apttus__APTS_Agreement__c 
                                                        WHERE CNG_AUS_RecordType__c = TRUE AND ID IN:agreementset];
        
        //Holds attachments from correct Recordtypes
        List<Attachment> attachtoupdate = [SELECT Id, ParentId, Name, IsPrivate, Body, OwnerId, CreatedById, CreatedDate, Description FROM Attachment 
                                                        WHERE ParentId IN:agreementAUS AND ID IN:attachedIDs];  
                
//Conversion of Attachments into ContentVersions                
          
//map Attachment Id with ContentVersion record
                Map<Id,ContentVersion> attachmentCVs = new Map<Id,ContentVersion>();
//Map —> Attachment – Attachment Parent Id 
                Map<Id,Id> attachmentParentIds = new Map<Id,Id>();

//Generating file version using content version object
        if(!attachtoupdate.isEmpty()){
                for(Attachment att : attachtoupdate) {
                     ContentVersion cv = new ContentVersion();
                     cv.ContentLocation = 'S';
                     cv.PathOnClient = att.Name;
                     cv.Origin = 'H';
                     cv.OwnerId = att.OwnerId;
                     cv.Title = att.Name;
                     cv.VersionData = att.Body;
                     cv.FirstPublishLocationId = att.ParentId;
                    attachmentCVs.put(att.Id,cv);
                    attachmentParentIds.put(att.Id,att.ParentId);
                 }
           }
        
//Insert the content versions from attachment data
            if(attachmentCVs.values().size() > 0 ) {
                insert attachmentCVs.values();
            }

//to map ContentVersionId with AttachmentParent Id for ContentDocument Linking
            map<Id,Id> cvToAttachmentParentIdMap = new map<Id,Id>();

            List<Id> cvIds = new List<Id>();
                for(Id key : attachmentCVs.keySet()){
                ContentVersion cv = attachmentCVs.get(key);
                    Id attachmentParentId = attachmentParentIds.get(key);
    //System.debug('cvToAttachmentParentIdMap'+cvToAttachmentParentIdMap);
                    cvToAttachmentParentIdMap.put(cv.Id,attachmentParentId);
                    cvIds.add(cv.Id);
    //System.debug('cvIds'+cvIds);
}
//Fetching contentDocumentId using contentVersionId

            List<ContentVersion> cvWithDocIds=[select Id,ContentDocumentId from ContentVersion where Id IN :cvIds];

//to link the files to its parent records
            List<ContentDocumentLink> contentDocumentLinks = new List<ContentDocumentLink>();
                for (ContentVersion cv : cvWithDocIds) {
                    Id attachmentParentId = cvToAttachmentParentIdMap.get(cv.Id);
                    ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = attachmentParentId, ContentDocumentId = cv.ContentDocumentId, ShareType = 'V');
                    ContentDocumentLinks.add(cl);
                }

            if(contentDocumentLinks.size() > 0){
            insert contentDocumentLinks;
            }
        
        }
}```