• Manish Shahi
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hey all,

Something is wrong with the code below - it only works (updated Discovery Call Note datetime) if role = Account_executive

How can I update it to also work for Strategic_Account_Executive? 

public class ContentDocumentLinkTriggerHandler {
    /*

    */
    public static void OnAfterInsert(List<ContentDocumentLink> newContentDocumentLink, List<ContentDocumentLink> oldContentDocumentLink, Map<ID, ContentDocumentLink> newContentDocumentLinkMap, Map<ID, ContentDocumentLink> oldContentDocumentLinkMap){
        /*

        */
        UserRole accountExecutiveRole = [Select Id From UserRole WHERE DeveloperName = 'Account_Executive' OR DeveloperName = 'Strategic_Account_Executive' LIMIT 1];
        List<User> AEUserIdList = [SELECT Id FROM User WHERE UserRoleId = :accountExecutiveRole.Id];
        Map<Id, ContentDocument> contentDocumentMap = new Map<Id, ContentDocument>([SELECT OwnerId, Description FROM ContentDocument WHERE OwnerId IN :AEUserIdList]);
        Set<Id> opportunityIdSet = new Set<Id>();
        Map<Id, ContentDocumentLink> opportunityContentDocumentLinkMap = new Map<Id, ContentDocumentLink>();
        for(Id ContentDocumentLinkId: newContentDocumentLinkMap.keySet()){
            if(!RecursionCheck.IsRecordIdInSet(ContentDocumentLinkId, 'After')){
                if(String.ValueOf(newContentDocumentLinkMap.get(ContentDocumentLinkId).LinkedEntityId).StartsWith('006') && ContentDocumentMap.KeySet().contains(newContentDocumentLinkMap.get(ContentDocumentLinkId).ContentDocumentId)){
                    opportunityIdSet.add(newContentDocumentLinkMap.get(ContentDocumentLinkId).LinkedEntityId);
                    opportunityContentDocumentLinkMap.put(newContentDocumentLinkMap.get(ContentDocumentLinkId).LinkedEntityId, newContentDocumentLinkMap.get(ContentDocumentLinkId));
                }
                RecursionCheck.AddRecordIdToSet(ContentDocumentLinkId, 'After');
            }
        }
        //List<ContentDocument> cdList = [SELECT Id, Title, CreatedDate, Description, ParentId, LatestPublishedVersionId FROM ContentDocumentLink WHERE Id = :];

        Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([Select Id, StageName, RecordTypeId, Discovery_Call_Date__c, Use_Case__c, Type, Amount, Discovery_Call_Note_Datetime__c FROM Opportunity WHERE Id IN :OpportunityIdSet]);
        for(Id oppId: opportunityMap.keySet()){
            if(opportunityMap.get(oppId).StageName == 'Qualify'){
                opportunityMap.get(oppId).Discovery_Call_Note_Datetime__c = System.Now();
            }
        }
        // TODO: should I use this or the regular update?
        Update opportunityMap.values();
    }
}