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 

Conditions for table

Hi,

I have a search table when i clicked attach button in each row it attaches the article to the case.But my requirement is to the attached article should be removed from table once it is attached to case and also only one article should be allowed to attach to case..If any other row is selected to attach then old row which is attched should be removed from case and newly added article should get attached to CaseArticle.Can anyone help me with modification of this code??
component:
<lightning:input type="text" name="searchKey" label="Enter" aura:id="searchKey" onchange="{!c.searchKeyChange}" placeholder="Search" />          
    <table class="slds-table slds-table_bordered slds-table_striped slds-table_cell-buffer slds-table_fixed-layout">
        <thead>
            <tr class="slds-text-heading_label">              
                <th scope="col"><div class="slds-truncate" title="Title">Article Number</div></th>
                <th scope="col"><div class="slds-truncate" title="Title">Article Title</div></th>
                <th scope="col"><div class="slds-truncate" title="Title">Description</div></th>
               <th scope="col"><div class="slds-truncate" title="Title">System</div></th>
                <th scope="col"><div class="slds-truncate" title="Title">Product</div></th>
                <th scope="col"><div class="slds-truncate" title="Title">LastPublishedDate</div></th>
                <th scope="col"><div class="slds-truncate" title="Title">Version</div></th>
               
                            
            </tr>
        </thead>
        <tbody>
            <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.Description_c}">{!Knowledge__kav.Description__c}</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}"/>                 
                </tr>
            </aura:iteration>
        </tbody>
    </table> 
                                
Controller:
searchKeyChange: function(component, event) {
        var searchKey = component.find("searchKey").get("v.value");
        console.log('searchKey:::::'+searchKey);
        var action = component.get("c.findByTitle");
        action.setParams({
            "searchKey": searchKey
        });
        action.setCallback(this, function(a) {
            component.set("v.Knowledge__kav", a.getReturnValue());
        });
        $A.enqueueAction(action);
    },   

handleClick : function(component, event, helper) {
        var action = component.get('c.attachArticleToCase');
       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 attached to 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();
    },

Apex:

@AuraEnabled
    public static String attachArticleToCase(String caseId, String articleId){
       
           String message = 'SUCCESS';
          
         
             CaseArticle ca = new CaseArticle();
             ca.CaseId = caseId;
              ca.KnowledgeArticleId = articleId;
               insert ca;
          

       return message;
    }

 
SubratSubrat (Salesforce Developers) 
Hello ,

To achieve the requirement of attaching only one article to the case at a time and removing the previously attached article from the table, you need to make some modifications to your Lightning component and Apex code. Here's how you can do it:

Update the Lightning component markup:
Add a new attribute to store the currently attached article Id and modify the lightning:button to check whether an article is already attached or not. Also, add a new attribute to keep track of the attached article's row index in the iteration.
<aura:component>
    <!-- ... Existing code ... -->
    <aura:attribute name="attachedArticleId" type="String" default="" />
    <aura:attribute name="attachedRowIndex" type="Integer" default="-1" />
    <!-- ... Existing code ... -->
    <tbody>
        <aura:iteration items="{!v.Knowledge_kav}" var="Knowledge_kav" indexVar="index">
            <tr>
                <!-- ... Existing code ... -->
                <lightning:button variant="brand-outline"
                                  label="Attach"
                                  onclick="{!c.handleClick}"
                                  value="{!Knowledge__kav.KnowledgeArticleId}"
                                  disabled="{!v.attachedArticleId != '' && v.attachedRowIndex != index}" />
            </tr>
        </aura:iteration>
    </tbody>
</aura:component>
Modify the controller to handle the article attachment and detachment:
Update the handleClick function to check if an article is already attached and handle the detachment if needed.
({
    handleClick: function (component, event, helper) {
        var attachedArticleId = component.get("v.attachedArticleId");
        var attachedRowIndex = component.get("v.attachedRowIndex");
        var clickedRowIndex = event.getSource().get("v.name");

        if (attachedArticleId && attachedRowIndex === clickedRowIndex) {
            // If the clicked article is already attached, remove the attachment
            helper.detachArticleFromCase(component, attachedArticleId);
        } else {
            // If another article is selected, detach the previous article and attach the new one
            var KnowledgeArticleId = event.getSource().get("v.value");
            helper.attachArticleToCase(component, KnowledgeArticleId);
        }
    },

    // Rest of the controller code...
})
Create a helper function to handle attachment and detachment:
Create a new helper function to attach and detach the article from the case. This function will be called from the handleClick function.
 
({
    attachArticleToCase: function (component, articleId) {
        // Your existing code for attaching the article to the case...
        // Make sure to update the attachedArticleId and attachedRowIndex attributes accordingly.
    },

    detachArticleFromCase: function (component, articleId) {
        var action = component.get('c.detachArticleFromCaseApex');
        action.setParams({
            caseId: component.get("v.recordId"),
            articleId: articleId
        });
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.attachedArticleId", "");
                component.set("v.attachedRowIndex", -1);
                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 detaching article from case",
                    "type": "error"
                });
                toastEvent.fire();
            }
        });
        $A.enqueueAction(action);
    }
})
Update the Apex method to detach the article from the case:
In your Apex class, create a new method to handle article detachment from the case.
public with sharing class YourApexClass {
    @AuraEnabled
    public static void detachArticleFromCaseApex(String caseId, String articleId) {
        List<CaseArticle> caseArticles = [
            SELECT Id
            FROM CaseArticle
            WHERE CaseId = :caseId AND KnowledgeArticleId = :articleId
        ];

        if (!caseArticles.isEmpty()) {
            delete caseArticles;
        }
    }

    // Rest of your existing code...
}
With these modifications, your component will now allow only one article to be attached to the case at a time. When a new article is attached, the previous one will be automatically detached and removed from the table.

Thank you.