0

Bear with me if this seems pretty basic, i'm starting out in lwc, so have probably missed something obvious. I have a lwc which i am trying to pass some data into an apex method and have it return the created record/id for example.

I have two versions of the apex method, and two lwc methods. The first method passes two strings and returns the record fine, but the version where i try pass the contact object fails with the following error : {"ok":false,"status":400,"statusText":"Bad Request","body":{"message":"An internal server error has occurred\nError ID: 858882331-69267 (1757134078)"}}

The apex methods are as below :

@AuraEnabled
    public static Contact submitUserRegistration(Contact cont) {
        Account acc = new Account(Name='Test1',First_Name__c = 'Chris1',Last_Name__c = 'Coates1') ;
        insert acc ;

        Contact c = new Contact(Firstname=cont.FirstName,LastName=cont.LastName, AccountID = acc.Id) ;

        Contact cr = (Contact)WithoutSharingMethods.insertSObject(c) ;

        return cr ;


    }

    @AuraEnabled
    public static Contact submitSimpleUserRegistration(String FirstName, String LastName) {
        Account acc = new Account(Name='Test1',First_Name__c = 'Chris1',Last_Name__c = 'Coates1') ;
        insert acc ;

        Contact c = new Contact(Firstname=FirstName,LastName=LastName, AccountID = acc.Id) ;

        Contact cr = (Contact)WithoutSharingMethods.insertSObject(c) ;

        return cr ;


    }

the without sharing methods basically allows the insert of the contact with the account id as the account has already been assigned to a dfifferent user (due to the new community settings).

below is the lwc code :

import { LightningElement, track } from 'lwc';
import submitUserRegistration from '@salesforce/apex/userRegistrationController.submitUserRegistration'  ;
import submitSimpleUserRegistration from '@salesforce/apex/userRegistrationController.submitSimpleUserRegistration'  ;

export default class UserRegistration extends LightningElement {
    @track res ;
    res = 'test' ;
    @track recordId ;


submitComplexRegistration() {
     let contactrecord = { 'sobjectType' : 'Contact' };
     contactrecord.FirstName = 'Chris' ;
     contactrecord.LastName = 'Coates' ;

     console.log(contactrecord) ;

     submitUserRegistration({cont : contactrecord}).then((resp) => {
                                               this.recordId = resp.Id ;
                                               console.log(JSON.stringify(resp)) ;
                                           }).catch((err) => {
                                               console.log(JSON.stringify(err)) ;
                                               this.res = JSON.stringify(err) ;
                                           }) ;

}


submitSimpleRegistration() {
    submitSimpleUserRegistration({FirstName : 'Christopher', LastName : 'Coates'})
    .then((resp) => {
        this.recordId = resp.Id ;
        console.log(JSON.stringify(resp)) ;
    }).catch((err) => {
        console.log(JSON.stringify(err)) ;
        this.res = JSON.stringify(err) ;
    }) ;


}
}

I've looked at similar examples here, and cut the code down as simple as possible. The submit simple registration works fine, yet the submit complex registration fails with the aforementioned error. I'm sure i'm missing something obvious? I've tested both apex methods outside of the component (in execute anonymous) and they both work fine.

1
  • try changing argument type as Contact submitUserRegistration(sObject cont) in function
    – User6670
    Feb 12, 2020 at 11:51

1 Answer 1

0

ok, so the basics i missed? - needing to give the profile which the community guest user is on the ability to insert/read the contact object. Now i feel somewhat dumb, but at least the poc code works.

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.