How to create Draft Knowledge Article with related records from Published Knowledge Article using Trigger in Salesforce?

How to create Draft Knowledge Article with related records from Published Knowledge Article using Trigger in Salesforce?

KnowledgeArticle is parent entity.
KnowledgeArticleVersion is child entity.
When a new Knowledge Article is created, it creates an entry in KnowledgeArticle and also in KnowledgeArticleVersion.
When a new Knowledge Article is created as Draft(Edit As Draft), it creates an entry in KnowledgeArticleVersion. It will be child to the KnowledgeArticle with status Draft.
Note:
In Salesforce, actions like changing the publication status of a KnowledgeArticeVersion(__kav) record such as Publish and Archive, do not fire Apex triggers.
Reference Article:
Below trigger can be used to create child records(related records), when an article is created using Edit As Draft quick action. This trigger creates new related records.
Sample Trigger:
 
trigger KnowledgeTrigger on Knowledge__kav ( after insert ) {
 
    Set < Id > setKAIds = new Set < Id >();
 
    for ( Knowledge__kav objKnowledge : trigger.new ) {
    
        if ( objKnowledge.PublishStatus == ‘Draft’ ) {
            
            /* 
                Getting the KnowledgeArticleId
                Each KnowledgeArticleVersion will be tied to one KnowledgeArticle
            */
            setKAIds.add( objKnowledge.KnowledgeArticleId );
        
        }
    
    }
    
    if ( setKAIds.size() > 0 ) {
    
        Map < Id, Knowledge__kav > mapArticleIdKnowledge = new Map < Id, Knowledge__kav > ();
        List < Process__c > listProcesses = new List < Process__c >();
        List < Follow_Up__c > listFollowUps = new List < Follow_Up__c >();
        
        for (  Knowledge__kav objKnowledge : 
            [ SELECT Id, KnowledgeArticleId, 
            ( SELECT Name FROM Processes__r ), 
            ( SELECT Name FROM Follow_Ups__r )
            FROM Knowledge__kav
            WHERE KnowledgeArticleId IN: setKAIds AND PublishStatus = ‘Online’ ]
        ) {
            
            /*
                This map will hold the master KAV from which the draft is created
            */
            mapArticleIdKnowledge.put( objKnowledge.KnowledgeArticleId, objKnowledge );
        
        }
        
        for ( Knowledge__kav objKnowledge : trigger.new ) {
    
            if ( objKnowledge.PublishStatus == ‘Draft’ ) {
            
                if ( mapArticleIdKnowledge.containsKey( objKnowledge.KnowledgeArticleId ) ) {
                
                    Knowledge__kav tempKnowledge = mapArticleIdKnowledge.get( objKnowledge.KnowledgeArticleId );
                
                    /*
                        If the Master KAV had Process related records, then new records are created with the Draft KAV
                    */
                    if ( tempKnowledge.Processes__r.size() > 0 ) {
                        
                        for ( Process__c objProcess : tempKnowledge.Processes__r ) {
                        
                            Process__c tempProcess = objProcess.clone( false, false, false, false );
                            tempProcess.Knowledge__c = objKnowledge.Id;
                            listProcesses.add( tempProcess );
                        
                        }
                        
                    }
                
                
                    /*
                        If the Master KAV had Follow Up related records, then new records are created with the Draft KAV
                    */
                    if ( tempKnowledge.Follow_Ups__r.size() > 0 ) {
                        
                        for ( Follow_Up__c objFollowUp : tempKnowledge.Follow_Ups__r ) {
                        
                            Follow_Up__c tempFollowUp = objFollowUp.clone( false, false, false, false );
                            tempFollowUp.Knowledge__c = objKnowledge.Id;
                            listFollowUps.add( tempFollowUp );
                        
                        }
                        
                    }
                
                }
            
            }
        
        }
        
        if ( listProcesses.size() > 0 ) {
            
            insert listProcesses;
            
        }
        
        if ( listFollowUps.size() > 0 ) {
            
            insert listFollowUps;
            
        }
    
    }
    
}

Leave a Reply