Share the file to external User

 Requirement:  Once the file is uploaded by the internal user then it will be shared the external user (Portal User)

Solution Write a trigger on the contentDocumentLink object once the file uploaded share the file to the external user

public without sharing class ContentDocumentLinkTriggerHandler {

        public static final String ALL_USERS_SHARING = 'AllUsers';

public void runTrigger(){

// Method will be called to handle After Insert ContentDocumentLink

if(Trigger.isAfter && Trigger.isInsert) {

onAfterInsert((list<ContentDocumentLink>)trigger.new,(map<id,ContentDocumentLink>) trigger.newMap);

}

// Method will be called to handle After Update ContentDocumentLink

if(Trigger.isAfter && Trigger.isUpdate) {

onAfterUpdate((list<ContentDocumentLink>)trigger.new,(map<id,ContentDocumentLink>) trigger.newMap);

}

// Method will be called to handle After Update ContentDocumentLink

if(Trigger.isAfter && Trigger.isDelete) {

onAfterDelete((list<ContentDocumentLink>)trigger.old,(map<id,ContentDocumentLink>) trigger.oldMap);

}

}

    

    // Method will be called to handle After Insert ContentDocumentLink

    @TestVisible

    private void onAfterInsert(list<ContentDocumentLink> listTriggerNew,Map<id,ContentDocumentLink> newMap) {

        shareFilesToExtUser(listTriggerNew);

    }

    // Method will be called to handle After Update ContentDocumentLink

    @TestVisible

    private void onAfterUpdate(list<ContentDocumentLink> listTriggerNew,Map<id,ContentDocumentLink> newMap) {

  

    }

    // Method will be called to handle After Update ContentDocumentLink

    @TestVisible

    private void onAfterDelete(list<ContentDocumentLink> listTriggerOld,Map<id,ContentDocumentLink> oldMap) {

    }  

 

    /**

*  share the files to external user when fill upload under the Sobject(Parent Object)

*  @name shareFilesToExtUser

*  @param list<ContentDocumentLink> listTriggerNew

*  @return Void

*/   

    @TestVisible

    private void shareFilesToExtUser(list<ContentDocumentLink> listTriggerNew) {

        

        set<Id> setCntDocIds = new set<Id>();

        set<Id> setCntDocLinkIds = new set<Id>();

        set<Id> setofParentObjecIds = new set<Id>();

        

        list<ContentDocumentLink> updatelistofCDL = new list<ContentDocumentLink>();      

        for(ContentDocumentLink clIterator : listTriggerNew) {

             if(clIterator.LinkedEntityId.getSObjectType().getDescribe().getName() == 'SObjectApiName'){

                setofParentObjecIds.add(clIterator.LinkedEntityId);

                setCntDocIds.add(clIterator.ContentDocumentId);

                setCntDocLinkIds.add(clIterator.Id);

            }

        }

        

if(!setofParentObjecIds.isEmpty() && !setCntDocIds.isEmpty() && !setCntDocLinkIds.isEmpty()){

            for(ContentDocumentLink objCDL:[SELECT Id, LinkedEntityId, ShareType, ContentDocumentID, ContentDocument.Title, 

                                            Visibility FROM ContentDocumentLink where LinkedEntityId IN:setofParentObjecIds

                                            and ContentDocumentID IN:setCntDocIds and Id IN:setCntDocLinkIds]){

                                                objCDL.Visibility = ALL_USERS_SHARING;

                                                updatelistofCDL.add(objCDL);

                                            }

        }

        if(!updatelistofCDL.isEmpty()){

            update updatelistofCDL;

        }

    } 

   

}


Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page