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
John Manager Training DepJohn Manager Training Dep 

Class called from aura component not getting executed in lightning page?

Hi Team,

In my org, I see there is a code to update the read unread checkbox(r_u_cbox) field to true when the current owner of a case opens the record on the salesforce page and refreshes it.

I was able to test the UpdateCaseCheckboxonRef class to cover 100% of the code but when I add the aura component to the lightning page layout, the class functionality does not seem to execute. Could anyone guide please?

Class is mentioned below,
public class UpdateCaseCheckboxonRef {

    @AuraEnabled
    public static boolean execFunc(string idOfCase) {
    
    string name;
    case currRec =    [Select id, r_u_cbox, status, OwnerId, Owner.Username FROM case where 
        id =:idOfCase];
    if(currRec.OwnerId == UserInfo.getUserId() && !currRec.r_u_cbox)
        {
            currRec.r_u_cbox = true;
            
             update currRec;
            return true;
             
        }
        else{return false;}
        
    }
}

The aura components are mentioned below,
.cmp aura
<aura:component controller="UpdateCaseCheckboxonRef" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:actionOverride" access="global"  >
    
    <lightning:workspaceAPI aura:id="workspace"/>
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
    
</aura:component>
.js controller aura
({
	doInit : function(component, event, helper) {
        
        var contactId = component.get("v.recordId");
        var action = component.get("c.execFunc");
        action.setParams({ 
            "Id": contactId});
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            var res = response.getReturnValue(); 
            console.log('res valuse'+JSON.stringify(res));
            if(res == "true"){
		var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
           var focusedTabId = response.tabId;
            workspaceAPI.refreshTab({
                      tabId: focusedTabId,
                      includeAllSubtabs: true
             });
        })
        .catch(function(error) {
            console.log(error);
        });
            }
                });
	}
})

helper.js aura
({
	helperMethod : function() {
		
	}
})




 
Best Answer chosen by John Manager Training Dep
Khan AnasKhan Anas (Salesforce Developers) 
Hi John,

Greetings to you!

+1 Maharajan
You need to use $A.enqueueAction(action); It adds the server-side controller action to the queue of actions to be executed. Please try below code:
({
    doInit : function(component, event, helper) {
        
        var contactId = component.get("v.recordId");
        var action = component.get("c.execFunc");
        action.setParams({ 
            "idOfCase" : contactId});
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            var res = response.getReturnValue(); 
            console.log('res valuse'+JSON.stringify(res));
            if(res == "true"){
                var workspaceAPI = component.find("workspace");
                workspaceAPI.getFocusedTabInfo().then(function(response) {
                    var focusedTabId = response.tabId;
                    workspaceAPI.refreshTab({
                        tabId: focusedTabId,
                        includeAllSubtabs: true
                    });
                })
                .catch(function(error) {
                    console.log(error);
                });
            }
        });
        $A.enqueueAction(action);
    }
})

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Maharajan CMaharajan C
The Problem is parameter you are setting in doInit . Change like below:

Param Name should be same as you defined in execFunc method:


({
    doInit : function(component, event, helper) {
        
        var caseId = component.get("v.recordId");
        var action = component.get("c.execFunc");
        action.setParams({ 
            "idOfCase" : caseId});
        action.setCallback(this, function(response) {
            var state = response.getState();
            var res = response.getReturnValue(); 
            console.log('res valuse'+JSON.stringify(res));
            if(res == "true"){
        var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
           var focusedTabId = response.tabId;
            workspaceAPI.refreshTab({
                      tabId: focusedTabId,
                      includeAllSubtabs: true
             });
        })
        .catch(function(error) {
            console.log(error);
        });
            }
                });
    }
})


Thanks,
Maharajan.C
John Manager Training DepJohn Manager Training Dep
Thanks for the response, but your suggestion did not work.
Additionally, also tried with below in the .cmp aura but no success.
<aura:handler event="force:refreshView" action="{!c.doInit}" />

Any constructive suggestion much appreciated!
Khan AnasKhan Anas (Salesforce Developers) 
Hi John,

Greetings to you!

+1 Maharajan
You need to use $A.enqueueAction(action); It adds the server-side controller action to the queue of actions to be executed. Please try below code:
({
    doInit : function(component, event, helper) {
        
        var contactId = component.get("v.recordId");
        var action = component.get("c.execFunc");
        action.setParams({ 
            "idOfCase" : contactId});
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            var res = response.getReturnValue(); 
            console.log('res valuse'+JSON.stringify(res));
            if(res == "true"){
                var workspaceAPI = component.find("workspace");
                workspaceAPI.getFocusedTabInfo().then(function(response) {
                    var focusedTabId = response.tabId;
                    workspaceAPI.refreshTab({
                        tabId: focusedTabId,
                        includeAllSubtabs: true
                    });
                })
                .catch(function(error) {
                    console.log(error);
                });
            }
        });
        $A.enqueueAction(action);
    }
})

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer