1

The JS in my LWC is showing it has data in the params I'm passing from JS to my Apex method. The Apex method doesn't seem to be connection though.

Apex Note: in Dev Console, in the log, these debug statements never print. The logs do show: CODE_UNIT_STARTED[EXTERNAL]|apex://RfqSendVendorEmailsController/ACTION$insertJunctionRecords

@AuraEnabled
public static String insertJunctionRecords(String[] accountIDs, ID rfqID){

    System.debug('*** insertJunctionRecords accountIDs: ' + accountIDs);
    List<VendorRFQ__c> junctionRecords = new List<VendorRFQ__c>();

    for(ID id : accountIDs) {
        VendorRFQ__c vRfq = new VendorRFQ__c(Vendor__c = id, Request_for_Quote__c = rfqID);
        junctionRecords.add(vRfq);
    }

    System.debug('*** junctionRecords: ' + junctionRecords);
    try {
        insert junctionRecords;
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }

    return 'Emails have been sent.';
}

Javascript Note: the console logs show there is data for both variables. this.vendorsSelected is an array

handleClickSendEmails(event) {
        var vendorsStringified = JSON.stringify(this.vendorsSelected);
        console.log('*** vendorsStringified: ' + vendorsStringified);

    
    var vendorsSelectedIds = [];
    for (let i = 0; i < this.vendorsSelected.length; i++) {
        console.log('*** this.vendorsSelected[i].value: ' + vendorsStringified);
        vendorsSelectedIds.push(this.vendorsSelected[i].value);
    }
    
    console.log('*** vendorsSelectedIds: ' + vendorsSelectedIds);

    if (vendorsSelectedIds) {
        insertJunctionRecords({accountIDs : vendorsSelectedIds, 
                            rfqID : this.recordId})
            .then(results => {
                    console.log('*** results: ' + results);

                    this.sendEmailsMessage(results);
                })
                .catch(error => {
                    this.error = error;
                });
    } else {
        console.log('*** vendorsSelectedIds was empty');
    }
}
1
  • What does this.vendorsSelected show in the debug log? How is it set?
    – sfdcfox
    Dec 13, 2022 at 6:19

2 Answers 2

1

You are passing in String accountIDs.

You can't iterate a string.

It needs to be a list in the Apex method.

Either pass in an Array in the javascript code and recieve a String[] accountIDs

Or pass the string, receive String accountIDs and parse it to a list

Id[] IDs = (Id[])JSON.deserialize(accountIDs,Id[].class);

Prior to calling insertJunctionRecords , send just the ids after extracting them:

let vendorIds = this.vendorsSelected.map( vendor => { vendor.value});
//assuming that is a property on the vendor object
7
  • Thanks Caspar. I went with your first option, but same result as before. I've updated my main post with my latest code. Dec 13, 2022 at 6:20
  • In JS, vendorsSelected is an array Dec 13, 2022 at 6:21
  • Actually, I'm realizing it's an array of objects... hmm Dec 13, 2022 at 6:25
  • I think all I need to do is convert this Object array to ID array. My first attempt at doing this with a JS loop didn't work....[{"value":"0013k00003C7AQsAAN","label":"Car","isVisible":true,"selected":true},{"value":"0013k00002xs223AAA","label":"CGI","isVisible":true,"selected":true}] Dec 13, 2022 at 6:57
  • I updated original post, JS code now has a loop to extract "value" to an ID array Dec 13, 2022 at 6:58
-1

It looks like the insertJunctionRecords method in your Apex code is not being called from the JavaScript code. This could be because of several reasons:

The insertJunctionRecords method might not be marked as @AuraEnabled, which is necessary for it to be callable from the JavaScript code. The method might not be static, which is necessary for it to be callable from the JavaScript code. The method might not be in a controller class, which is necessary for it to be callable from the JavaScript code. To fix this issue, make sure that the insertJunctionRecords method is marked as @AuraEnabled, is static, and is in a controller class.

Here's an example of how the insertJunctionRecords method might look like after making these changes:

public with sharing class RfqSendVendorEmailsController {

@AuraEnabled
public static String insertJunctionRecords(String[] accountIDs, ID rfqID){
    System.debug('*** insertJunctionRecords accountIDs: ' + accountIDs);
    List<VendorRFQ__c> junctionRecords = new List<VendorRFQ__c>();

    for(ID id : accountIDs) {
        VendorRFQ__c vRfq = new VendorRFQ__c(Vendor__c = id, Request_for_Quote__c = rfqID);
        junctionRecords.add(vRfq);
    }

    System.debug('*** junctionRecords: ' + junctionRecords);
    try {
        insert junctionRecords;
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }

    return 'Emails have been sent.';
}

Once the insertJunctionRecords method is callable from the JavaScript code, you should see the debug statements in the method print in the log in the Developer Console.

You must log in to answer this question.

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