3

I have an Aura Enabled method in my class that accepts a Case record and a list of a related object records and inserts both of them. The code works fine when I'm logged in as the admin, but the actual user profile that will be leveraging it receives an Internal Server Error when calling the method.

The user profile is a Community user (Customer Community Plus license) and I am testing with the Community in Preview.

I've verified that:

  • The profile has full CRUD access on Case
  • The profile has full CRUD access on Subject__c
  • The profile has all field level security visibility needed for fields on Case and Subject__c
  • The profile has access to the Apex class

Are there any other areas I should be looking? These are new records to be inserted so I do not think sharing should be a part of the issue.

My class looks like this:

public with sharing CaseWithSubjectServive {

    @AuraEnabled
    public static Id createCaseWithSubjects(Case newCase, List<Subject__c> subjects) {
        System.debug('Entered method'); //this will not get hit
        try {
            insert newCase;
            insert subjects;
        } catch(Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }

}

My component HTML:

<template>

    <lightning-record-edit-form if:true={caseRecordTypeId} object-api-name="Case" record-type-id={caseRecordTypeId} onsubmit={handleSubmit} density="comfy">
        <lightning-messages></lightning-messages>

        <h1 class="slds-text-heading_large">Create a Case</h1>

        <div class="slds-grid slds-wrap">
            <div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-2">
                <lightning-input-field required field-name="Company_Name__c"></lightning-input-field>
            </div>
        </div>

        <h2 class="slds-text-heading_large">Associated Individuals/Companies</h2>
        <c-associated-individuals-companies-list></c-associated-individuals-companies-list>

        <div class="slds-grid slds-wrap slds-grid_align-end submit-container">
            <div class="slds-col">
                <lightning-button
                    class="slds-m-top_small"
                    type="submit"
                    name="save"
                    label={label.submit}>
                </lightning-button>
            </div>
        </div>

    </lightning-record-edit-form>

    <div if:true={showSpinner}>
        <lightning-spinner alternative-text="Saving..." style="position: fixed;"></lightning-spinner>
        <div style="position: fixed;" class="slds-backdrop slds-backdrop_open"></div>
    </div>

</template>

My component JS:

import { LightningElement, track, wire } from 'lwc';
import createCaseWithSubjects from '@salesforce/apex/CaseWithSubjectServive .createCaseWithSubjects';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { NavigationMixin } from 'lightning/navigation';
import CASE_OBJECT from '@salesforce/schema/Case';

// custom labels
import submit from '@salesforce/label/c.Submit';

export default class CaseWithSubjects extends NavigationMixin(LightningElement) {
    label = {
        submit,
    };
    caseRecordTypeId;

    showSpinner;

    @wire(getObjectInfo, { objectApiName: CASE_OBJECT })
    objectInfo({error, data}) {
        if(data) {
            const recordTypes = data.recordTypeInfos;
            this.caseRecordTypeId = Object.keys(recordTypes).find(
                t => recordTypes[t].name === 'Request Form'
            );
        } else if(error) {
            console.error(error);
        }
    }

    get caseRecordTypeId() {
        return this.caseRecordTypeId;
    }

    toggleSpinner() {
        this.showSpinner = !this.showSpinner;
    }

    handleSubmit(event) {
        event.preventDefault();

        const newCase = event.detail.fields;
        newCase.RecordTypeId = this.caseRecordTypeId;

        const individuals = this.template.querySelector('c-associated-individuals-companies-list').getIndividuals(); 
        const companies = this.template.querySelector('c-associated-individuals-companies-list').getCompanies();

        if(!this.hasValidCompanies(companies) && !this.hasValidIndividuals(companies)) {
            this.fireErrorToast('Please fill in all required fields on your Subjects');
            return;
        }

        const allAssociated = [...individuals, ...companies];

        this.toggleSpinner();

        createCaseWithSubjects ({
            newCase: newCase,
            subjects: allAssociated
        })
        .then(caseId => {
            this.toggleSpinner();
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: caseId,
                    objectApiName: 'Case',
                    actionName: 'view',
                }
            });
        }) 
        .catch(error => {
            //INTERNAL SERVER ERROR HITS CATCH
            this.toggleSpinner();
            console.error(error);
            this.fireErrorToast(error.body.message);
        });

    }

    fireErrorToast(msg) {
        const evt = new ShowToastEvent({
            title: 'Error',
            message: msg,
            variant: 'error',
        });
        this.dispatchEvent(evt);
    }

    hasValidIndividuals(individuals) {
        const invalidIndividuals = individuals.filter(v => {
            if(!v.First_Name__c || !v.Last_Name__c) {
                return v;
            }
        });

        return (invalidIndividuals.length === 0);
    }

    hasValidCompanies(companies) {
        const invalidCompanies = companies.filter(v => {
            if(!v.Company_Name__c) {
                return v;
            }
        });

        return (invalidCompanies.length === 0);
    }

}
2
  • Does your user profile has access to class?
    – User6670
    Mar 26, 2020 at 2:22
  • Yes the profile has access to the class
    – nbrown
    Mar 26, 2020 at 12:44

3 Answers 3

5

One way to figure whats the actual error is by handling the AuraEnabled exception . This would reveal why it is failing and will not give a GACK.

public with sharing CaseWithSubjectServive {

  @AuraEnabled
  public static Id createCaseWithSubjects(Case newCase, List<Subject__c> subjects) {
    try {
       System.debug('Entered method'); //this will not get hit
       insert newCase;
       insert subjects;    
    } catch(ex) {
        throw new AuraHandledException(ex.getMessage());
    }

  }
}
1

Try these two:

  • Can you please remove public with sharing to public without sharing in apex class.

  • Add some console.logs in Java script and check Line by Line why its failing (trace it).

1

I figured it out. The Subject__c object had a Deployment Status of "In Development". Once I changed it to "Deployed" everything worked.

You must log in to answer this question.

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