0 votes

I have a component where, when the user click on Save it fires an apex class that is supposed to update the records

The way it work is that onClick on save it creates a list of dict with the relevant data that need to be updated for example [{id:1234, name:'recordName'}, {id:1234566, name:'record2'}]

I tried to set up my Apex class but it seems that there is something I do not get in Apex JavaScript:

handleSave() {
        this.toggleSaveLabel = 'Saving...'
        let toSaveList = [];
        this.myList.forEach((element, index) => {
            if (element.changed === true){
        saveOutreachProperty({records : toSaveList})
        .then(() => {
            this.toggleSaveLabel = 'Saved';
                new ShowToastEvent({
                    title : 'Success',
                    message : `Records saved succesfully!`,
                    variant : 'success',
            this.isEdited = false;
            this.error = undefined;
        .catch(error => {
            this.error = error;
            this.record = undefined;
            console.log("Error in Save call back:", this.error);
        .finally(() => {
            setTimeout(() => {
                this.toggleSaveLabel = 'Save';
            }, 3000);



    public static void saveOutreachProperty(List<Lead> records){
        if(records.size()>0 && records != null){     
            for(integer i = 0; i < records.size(); i++) {
                Lead PropToMigrate = [SELECT Id, Name, Status, LeadSource, Number_of_Keys__c, Neighbourhood__c 
                              FROM lead 
                              WHERE Id = records[i].id
                              LIMIT 1]; 
                PropToMigrate.Name = records[i].Name;
                update PropToMigrate;

edited ApexClass:

    public static void saveOutreachProperty(List<Lead> records) {
        if(records != null) {
            update records;

Could you please help me to have my records updated and eplxplain me what am I doing wrong ?

1 Answer

0 votes
Best answer

In order to execute a DML, you must remove cacheable=trueDocs:

To set cacheable=true, a method must only get data, it can’t mutate (change) data.

Beware: the if condition is wrong. In your code, if records is null, you'll get a NullPointerException because you first check if it's empty calling a method on records, then you check if it's null. The null check should always be the first one.
I.E. if (records != null && !record.isEmpty())

Finally the method isn't bulkified. You have both a SOQL and a DML inside your for-loop, so you're going to hit governor limits if the input list contains more than 100 records.

Let's read your code: for each Lead in records you're retrieving itselft (query by id) and updating its own name. Since you already have both the Id and the new value for Name in records, you don't need the SOQL, you can simply update it.

public static void saveOutreachProperty(List<Lead> records) {
    if(records != null) {
        update records;
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.