0

I am calling apex method on lightning:tabClosed event to update a record on closing the tab. I can see the record Id being passed to controller method but query is throwing an error saying

{"exceptionType":"System.QueryException","isUserDefinedException":false,"message":"List has no rows for assignment to SObject","stackTrace":"Class.AccountViewController.updateCustomerInteraction: line 13, column 1"}

Has anyone worked on this kind of functionality before and know why this is happening?

Component:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable" access="global" controller="AccountViewController" >
     <lightning:utilityBarAPI aura:id="Accountbar" />
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="interactionId" type="String" />
    <aura:attribute name="updatedInteractions" type="vlocity_ins__CustomerInteraction__c" />
    <lightning:workspaceAPI aura:id="workspace" />
    <aura:handler event="lightning:tabClosed" action="{! c.onTabClosed }"/> 
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
</aura:component>

JSController:

({
    doInit : function(component, event, helper) {
        console.log("Tab Focused");
        //SVP-7878 --> START
        var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
            var focusedTabId = response.tabId;
            component.set("v.interactionId",JSON.stringify(response.pageReference.state.vlocity_ins__interactionId));
            console.log("Interaction ID params: "+component.get("v.interactionId"));
        })
        .catch(function(error) {
            console.log(error);
        });
        //SVP-7878 --> END
        
    },
    
    // SVP-7878 POC /START
    onTabClosed : function(component, event, helper) {
        var currentTabId = event.getParam('tabId');
        //console.log("Tab closed: " +currentTabId);
        var interIDD = component.get('v.interactionId');
        console.log("InteractionId Id in Tab CLosed:: "+interIDD);
        var action = component.get("c.updateCustomerInteraction");
        action.setParams({"interactionId": interIDD});

        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state == "SUCCESS"){
                console.log("IT WORKED!!");
            } else {
                console.log('There was a problem : '+JSON.stringify(response.getError()));
            }
        });
        $A.enqueueAction(action);
    } 
    // SVP-7878 POC /END
})

APEX CLASS:

public without sharing class AccountViewController {
    
    @AuraEnabled
    public static void updateCustomerInteraction(String interactionId){
        system.debug('::UPDATECUSTOMERINTERACTION:: ' +interactionId);
        vlocity_ins__CustomerInteraction__c CI = [SELECT Id, vlocity_ins__Status__c 
                                                   FROM vlocity_ins__CustomerInteraction__c 
                                                   WHERE Id = :interactionId];
        CI.vlocity_ins__Status__c = 'Completed';
        update CI;
    }
}

debug statement in the apex class is getting value of interactionId but query is failing.enter image description here

UPDATE:

This is working if i pass recordId in the tabClosed event but not a url param. Though the param Id getting passed to Apex method but SOQL is returning empty.

1 Answer 1

0

By default SOQL in salesforce return list of SObjects. But when you try to do this : vlocity_ins__CustomerInteraction__c CI = [SELECT Id, vlocity_ins__Status__c FROM vlocity_ins__CustomerInteraction__c WHERE Id = :interactionId];

and when no data is found, it is not able to fetch the first record from the list and it throws an error List has no rows for assignment to SObject

Always try to return a list

List<vlocity_ins__CustomerInteraction__c> vlocityCustomerInteractions = [SELECT Id, vlocity_ins__Status__c FROM vlocity_ins__CustomerInteraction__c WHERE Id = :interactionId];
if(!vlocityCustomerInteractions.isEmpty()){
    // Then run your logic of updating
}
3
  • List is still returning empty.
    – SunnyG
    Commented Apr 6, 2021 at 14:49
  • then it might be sharing issue for the record. Commented Apr 6, 2021 at 15:40
  • I just added an update to my question, please see.
    – SunnyG
    Commented Apr 7, 2021 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .