1

I have created Custom List Button on Case object. That button calls VF page and then VF page calls LWC which updates the record. Once Record is updated I want to navigate back to Current List View. I have tried below code:

VF Page:

   <apex:page standardController="Case" recordSetVar="cs" extensions="PassSelectedIdsToFlowVFController">
    <apex:includeLightning />
    <div id="lightning" />
    <script>
        $Lightning.use("c:changeOwnerLWCOnListApp", function() {
             $Lightning.createComponent("c:changeOwnerLWCOnList", {
                recordListId: {!SelectedCaseIDsJSON}
                 },
              "lightning",
              function(cmp) {
                console.log("LWC component was created");
                // do some stuff
              }
          );
        });
    </script>
    
</apex:page>

Controller Class:

public without sharing class PassSelectedIdsToFlowVFController {
    public List<String> SelectedCaseIDs{get;set;}
    //This will give List of Records which is selected from List View
    public PassSelectedIdsToFlowVFController(ApexPages.StandardSetController listcontroller){
        SelectedCaseIDs = new List<String>();
        for(Case acc : (Case[])listcontroller.getSelected()){
            System.debug('Case Id:'+acc.Id);
            SelectedCaseIDs.add(acc.Id);
        }
        system.debug('SelectedCaseIDs:'+SelectedCaseIDs);
    }
    //THis method will update records and accept parameters from LWC
    @AuraEnabled
    public static string updateOwner(List<String> recordListIds){
        System.debug('recordListId is:'+recordListIds);
        //List<String> values = (List<String>) JSON.deserialize(recordListIds,List<String>); Tried to something like this but its not working
        List<Id> cseIds = new List<Id>();
        Map<Id,Case> mapToUpdate = new Map<Id,Case>();
        try{
            for(Case cse: [Select Id,ownerId from Case where Id IN:recordListIds]){
                system.debug('Current User:'+ userInfo.getUserId());
                cse.ownerId = userInfo.getUserId();
                mapToUpdate.put(cse.Id,cse);
            }
            if(!mapToUpdate.isEmpty()){
                system.debug('mapToUpdate size:'+mapToUpdate.size());
                update mapToUpdate.values();
                return 'success';
            }
            else{
                return 'Fail';
            }
        }
        catch(Exception ex){
            System.debug('Exception is:'+ex.getMessage());
            return 'Fail';
        }
        
    }
    
}

LWC JS File:

    import {
    LightningElement,
    track,
    api,
    wire
} from 'lwc';
import {
    ShowToastEvent
} from 'lightning/platformShowToastEvent';
import {
    NavigationMixin
} from 'lightning/navigation';
import updateOwner from '@salesforce/apex/PassSelectedIdsToFlowVFController.updateOwner';
export default class ChangeOwnerLWCOnList extends NavigationMixin(LightningElement) {
    @api recordListId = [];
    @track recordListIds = [];
    connectedCallback() {
        this.recordListIds = this.recordListId;
        console.log('recordListId:' + JSON.stringify(this.recordListId));
        console.log('recordListIds:' + this.recordListIds);
        updateOwner({
                recordListIds: this.recordListIds
            })
            .then(result => {
                    console.log('Result is:' + result);
                    if (result == 'Success') {
                        this[NavigationMixin.Navigate]({
                            type: 'standard__objectPage',
                            attributes: {
                                objectApiName: 'Case',
                                actionName: 'list'
                            },
                            state: {
                                filterName: 'Recent'
                            },
                        });
                        const evt = new ShowToastEvent({
                            title: 'Toast Success',
                            message: 'Owner is Changed Successfully',
                            variant: 'success',
                            mode: 'dismissable'
                        });
                        this.dispatchEvent(evt);

                        this.error = undefined;
                    } else if (result == 'Fail') {
                        const evt = new ShowToastEvent({
                            title: 'Toast Error',
                            message: 'Some unexpected error',
                            variant: 'error',
                            mode: 'dismissable'
                        });
                        this.dispatchEvent(evt);
                    }
                }

            )
            .catch(error => {
                this.error = error;
                const evt = new ShowToastEvent({
                    title: 'Toast Error',
                    message: 'Some unexpected error',
                    variant: 'error',
                    mode: 'dismissable'
                });
                this.dispatchEvent(evt);
                this.contacts = undefined;
            });
    }
}

When I checked debug logs then I am getting List of records id which I selected Like this: ([5006F00001lcGWGQA2, 5006F00001lcGWUQA2]) while it should display like this (5006F00001lcGWGQA2, 5006F00001lcGWUQA2)

I am not sure why bracket [] is appearing when I pass List of Records from LWC to Controller Class due to which I am getting common.apex.runtime.impl.ExecutionException: Invalid id: [5006F00001lcGWGQA2, 5006F00001lcGWUQA2]

Can someone please help?

1

In your Visualforce Page:

recordListId: '{!SelectedCaseIDs}'

This is a string, not an Array; that's your problem. You need to serialize this in your JavaScript:

// Pass this in as a List of ID values
recordListId: {!SelectedCaseIDsJSON}

...

public without sharing class PassSelectedIdsToFlowVFController {
  public List<String> SelectedCaseIDs{get;set;}
  // Serialize SelectedCaseIDs so it comes out as an object in JavaScript
  public String getSelectedCaseIDSJSON() {
    return JSON.serialize(SelectedCaseIDs);
  }
4
  • Thanks you so much ! This is working as expected but when I navigate back to list view then its not working. I have updated LWC file and added NavigationMixin code there but its not navigating back. Can you please tell me what I am doing wrong there? Mar 18 at 12:01
  • @DhananjayPatil NavigationMixin only works in proper Lightning, not a Visualforce page. You'd want to use window.location or window.history to navigate back to the list view, something like window.top.location = '/500/o'
    – sfdcfox
    Mar 18 at 12:06
  • Thanks Again @sfdcfox Mar 18 at 13:52
  • I was able to navigate to List View from VF which calls LWC however I could not see updated value in List View records. If i refresh the LIst view manually, I am able to see updated values. I tried with sforce.one.back(true) but its not working. Is this limitation or is there any workaround for this? Mar 19 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.