You need to sign in to do that
Don't have an account?
Vanitha 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;
}
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;
}
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. 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. 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.
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. 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.