0

I created a LWC that calls an apex method from an apex class. The LWC is inside an aura component that is being used as a quick action to create a new record on another object based on the data from the record where the quick action is. Because is a LWC inside an aura component there is a modal that appears. I just added text based on the result of the process. One of the methods (the last one) works but the first one, which is the main one, is never called.

Note: I added the createProjectRecord inside the else just because I wanted to check if it would work but it is the same result :(.

Also the quick action only works on the second click.

I just want to know if i'm calling the method wrong or why won't it call it.

import { LightningElement, api, track} from "lwc";
import { NavigationMixin } from "lightning/navigation";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import createProjectRecord from '@salesforce/apex/CreateProjectController.createProjectRecord';
import searchForExistingProject from '@salesforce/apex/CreateProjectController.searchForExistingProject';
export default class createProject extends NavigationMixin(LightningElement) {
    @api projectSearchId;
    @api recordId;

    @api existingProject;
    @api existingProjectError;

    @api newProject;
    @api newProjectError;

    @track exists = false;
    @track creation = false;

    //handleClick(){
    connectedCallback() {
        // eslint-disable-next-line no-console
        console.log("Existing proposal Record: ", this.recordId);
        searchForExistingProject({
                searchKey: this.recordId
            })
            .then(result => {
                this.existingProject = result;
                this.existingProjectError = undefined;
                // eslint-disable-next-line no-console
                console.log('Existing project record: ', result);
                if (this.existingProject !== undefined) {
                    this.exists = true;
                    this.creation = false;
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: "Project record already exists",
                            message: "There is a project already associated with this proposal",
                            variant: "info",
                            duration: "3000",
                            mode: "pester"
                        })
                    )
                    this.navigateToRecordViewPage(this.existingProject);
                    console.log('Navigate to the existing project record');
                } else {
                    this.exists = false;
                    this.creation = true;
                    //this.createNewProjectRecord();
                    createProjectRecord({
                            proposalId: this.recordId
                        })
                        .then(result => {
                            this.newProject = result;
                            this.newProjectError = undefined;
                            // eslint-disable-next-line no-console
                            console.log('New project record: ', result);
                            if (this.newProject !== undefined) {
                                this.dispatchEvent(
                                    new ShowToastEvent({
                                        title: "Project record created",
                                        message: "A new project record was created successfully",
                                        variant: "success",
                                        duration: "3000",
                                        mode: "pester"
                                    })
                                )
                                this.navigateToRecordViewPage(this.newProject);
                                console.log('Navigate to the new project record');
                            }
                        })
                        .catch(error => {
                            this.newProjectError = error;
                            this.newProject = undefined;
                        });
                    // eslint-disable-next-line no-console
                    console.log("create new project record method activated");
                }
            })
            .catch(error => {
                this.existingProjectError = error;
                this.existingProject = undefined;
            });
    }


    navigateToRecordViewPage(returnedProjectId) {
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: returnedProjectId,
                actionName: 'view'
            }
        });
    }

    createNewProjectRecord() {
        createProjectRecord({
                proposalId: this.recordId
            })
            .then(result => {
                this.newProject = result;
                this.newProjectError = undefined;
                // eslint-disable-next-line no-console
                console.log('New project record: ', result);
                if (this.newProject !== undefined) {
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: "Project record created",
                            message: "A new project record was created successfully",
                            variant: "success",
                            duration: "3000",
                            mode: "pester"
                        })
                    )
                    this.navigateToRecordViewPage(this.newProject);
                    console.log('Navigate to the new project record');
                }
            })
            .catch(error => {
                this.newProjectError = error;
                this.newProject = undefined;
            });
    }
}
  • can you add console.log and see till where control is reaching? – Pranay Jaiswal Dec 9 '19 at 20:49
  • based on the ones I already added to the JS file, it is reaching everything but not calling never the main method – David Dec 9 '19 at 21:03
  • You should try changing the name of the method/function in JS so that it doesn't have a name conflict with the function name in the Apex controller. This name conflict was an issue in the past and could be the source of your pains here too. (i.e. change import createProjectRecord from to import createProjectRecordJS from and update the appropriate references in the LWC. – Mark Pond Dec 9 '19 at 21:06
  • Sadly,no change :( – David Dec 9 '19 at 21:13

Your Answer

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

Browse other questions tagged or ask your own question.