function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vanitha ManiVanitha Mani 

How to detach article from case using aura

Hi,

I am trying to detach a article from case when clicking the button in lightning component..how to achieve this.

Apex class:
@AuraEnabled
    public static String detachArticleToCases(String caseId, String articleId){
       List<CaseArticle> searchCaseArticleList =[select Id from CaseArticle where CaseID =:caseId];
          
String message = 'SUCCESS';
  if (searchCaseArticleList.size() > 0){
      CaseArticle ca = new CaseArticle(); 
      ca.CaseID = caseId; 
      ca.KnowledgeArticleId = articleId; 
      delete ca; 
  } 

  
return message;

    }

Aura component:
<aura:iteration items="{!v.Knowledge__kav}" var="Knowledge__kav">
                <tr>  
                  
                    <td><div class="slds-truncate" title="{!Knowledge__kav.ArticleNumber}">{!Knowledge__kav.ArticleNumber}</div></td>   
                    <td><div class="slds-truncate" title="{!Knowledge__kav.Title}">{!Knowledge__kav.Title}</div></td>
                    
                    <td><div class="slds-truncate" title="{!Knowledge__kav.System__r.Name}">{!Knowledge__kav.System__r.Name}</div></td>
                    <td><div class="slds-truncate" title="{!Knowledge__kav.Product__r.Name}">{!Knowledge__kav.Product__r.Name}</div></td>
                     <td><div class="slds-truncate" title="{!Knowledge__kav.LastPublishedDate}">{!Knowledge__kav.LastPublishedDate}</div></td>
                    <td><div class="slds-truncate" title="{!Knowledge__kav.Version__c}">{!Knowledge__kav.Version__c}</div></td>
                    <lightning:button variant="brand-outline"  label="Attach" onclick="{!c.handleClick}" value="{!Knowledge__kav.KnowledgeArticleId}"/> 
                    <lightning:button variant="brand-outline"  label="Detach" onclick="{!c.handleClick1}" value="{!Knowledge__kav.KnowledgeArticleId}"/>
                                    
                </tr>
            </aura:iteration>

Controller:
 handleClick1 : function(component, event, helper) {
        var action = component.get('c.detachArticleToCases');
       let recordId = component.get("v.recordId");
     var KnowledgeArticleId = event.getSource().get("v.value");
      
       action.setParams({
            caseId:recordId,  // Make sure your component implemented force:hasRecordId interface
           articleId: KnowledgeArticleId 
       });
       action.setCallback(this,function(a){
         var state = a.getState(); // get the response state
           if(state == 'SUCCESS') {
              var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
                        "title": "Success!",
                        "message": "Article detached from case",
                        "type": "success"
                    });
        toastEvent.fire();
            }
           else{
               var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
                        "title": "Error!",
                        "message": "error",
                        "type": "Error"
                    });
        toastEvent.fire();
           }
        });
        $A.enqueueAction(action);
       $A.get('e.force:refreshView').fire();
    },
SwethaSwetha (Salesforce Developers) 
HI Vanitha,
I see your question is answered in the post
https://trailblazers.salesforce.com/answers?id=9064S000000DXaQQAW

You needed to check the Permissions needed to detach an article from a case
https://help.salesforce.com/articleView?id=000339821&type=1&mode=1

Please mark this answer as best so that others facing the same issue will find this information useful. Thank you