0

So, I have a situation where I'm using a junction object (JD_AOR__c) for many-to-many relationship between two other objects, Job_Decription__c and AOR__c. I'm making a lwc that sits on the Job_Description__c page - I want the user to be able to add a new AOR to the Job Description, which would require the backend to create a new AOR__c record, and then also create a JD_AOR__c record.

I'm pretty sure this would require apex, but I haven't been able to figure out exactly how to write it. Can someone here help me?

This is my HTML:

                    <lightning-record-edit-form object-api-name="AOR__c" onsubmit={handleSubmit_JDAOR} onsuccess={handleSuccess}>
                        <lightning-input-field field-name="Applies_To__c" value="Specific Roles" disabled></lightning-input-field>
                        <lightning-input-field field-name="Name"></lightning-input-field>
                        <lightning-input-field field-name="Description__c"></lightning-input-field>
                        <lightning-input-field field-name="Link_to_Documentation__c"></lightning-input-field>
                        <center>
                        <lightning-button 
                                          type="reset"
                                          label="Cancel"
                                          onclick={handleCancel_JDAOR}>
                        </lightning-button>
                        <lightning-button class="slds-p-left_small"
                                          type="submit"
                                          label="Submit"
                                          >
                        </lightning-button>
                        </center>
                    </lightning-record-edit-form>

This is my current JS, it only creates the AOR__c record though:

    handleSubmit_JDAOR(event) {
        event.preventDefault();
        const fields = event.detail.fields;
        createRecord({apiName: "AOR__c", fields})
            .then(() => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Record created',
                        variant: 'success',
                    }),
                );
                return refreshApex(this.wiredJDAORsResult);   
            }).catch(error => {
                console.log(error);
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating record',
                        message: error.body.message,
                        variant: 'error',
                    }),
                );
            });
0

The question I ask is, does this require additional input from the user? Assuming that the answer in this case is no, the simple answer is to create a record in the Trigger.isAfter && Trigger.isInsert logic of your trigger. This will automatically create this added record by default, and you can control when it is being created. For example, you do not want to create if field_a__c == 'some value' or DO want to create when field_a__c == 'some other value'

If this requires some additional input and you need to submit with additional input from your users, you can use separate lightning-record-edit-form and use an external button as seen HERE to submit both forms in succession by chaining the form submissions in the onsuccess events in the order that you want by allowing the user to fill all appropriate details in advance and submit 1 time. lightning-record-form can be used as well, however you cannot hide the buttons within the form.

| improve this answer | |
0

Apex methods can be invoked from LWC components either using the wire decorator or imperatively. Both the approaches require you to import the method (e.g. insertRecords) from apex class. Post that, you can use usual .then/.catch block (imperatively) or an if/else block (for wire) For wire usage additionally wire decorator needs to be imported and you can wire a method or a property in the JS controller to an apex method in an Apex class.

Approach 1 (Imperatively call Apex method)

import insertRecords from '@salesforce/apex/<yourClass.insertRecords>';
handleImperativeCall(){
  insertRecords({name : 'someName'}) //parameterized
   .then(result => {
     //process your result and show toastmessage
   })
   .catch(error => {
     //handle the error and show toastmessage
   });
}

Approach 2 (Wiring a method using Wire decorator)

import { wire } from 'lwc';
import insertRecords from '@salesforce/apex/<yourClass.insertRecords>';

@wire (insertRecords, {name : 'someName'}) //Parameterized
wiredMethod ({ error, data })//note : this is the method that is wired. This method should be called from HTML. You may note the arguments are not named but positional.
if(data){
  //process your result and show toastmessage
})
else if(error){
 //handle the error and show toastmessage
});

The apex class can be parameterized like :

public String insertRecords(String name){
 try{
   //Insert records
   return 'success';
 }
 catch(DMLExeption e){
   //Log errors
     return 'failure';
 }
}

References:

  1. Imperative way : https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.Apex
  2. Using Wire : https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_wire_service_about
  3. Named and positional arguments : https://2ality.com/2011/11/keyword-parameters.html
| improve this answer | |
  • Thanks for responding. I've invoked apex methods from lwc before, including passing in a parameter. What I can't figure out is the right syntax to use to pass the field values from the record edit form, like I literally don't know the right way to write that. – nfoley Apr 5 at 0:37
  • no problem :) is it fine now? – Arjee Apr 12 at 12:33
  • yeah I got it figured out eventually – nfoley Apr 13 at 5:10
0

I got it to work using the following:

JS:

    handleSubmit_JDAOR(event) {
        event.preventDefault();
        const fields = event.detail.fields;

        let aorToApex = {'sobjectType': 'AOR__c'};
        aorToApex.Name = event.detail.fields.Name;
        aorToApex.Description__c = event.detail.fields.Description__c;
        aorToApex.Applies_To__c = event.detail.fields.Applies_To__c;
        aorToApex.Link_to_Documentation__c = event.detail.fields.Link_to_Documentation__c;

        createJDAOR({ aor: aorToApex , jdId: this.recordId })
        .then(() => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: 'Record created',
                    variant: 'success',
                }),
            );
            return refreshApex(this.wiredJDAORsResult);   
        }).catch(error => {
            console.log(error);
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error creating record',
                    message: error.body.message,
                    variant: 'error',
                }),
            );
        });

    }

Apex class:

    @AuraEnabled
    public static String createJDAOR(AOR__c aor, string jdId) {
        insert aor;
        ID aorId = aor.Id;
        Job_Description_AOR__c jdaor = new Job_Description_AOR__c(
            AOR__c = aorId,
            Job_Description__c = jdId
        );
        insert jdaor;
        return jdaor.Id;
    }
| improve this answer | |

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.