본문 바로가기

개발

[Salesforce] LWC System.LimitException: Too many DML statements: 1

728x90

 

문제 당시

LWC JS

 

import { LightningElement, api, track, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import checkConfirm from '@salesforce/apex/className.checkSubmit';

export default class absentAsManagement extends LightningElement {
 	@api recordId;
    @track record;
    @track error;
    
    /// 중간생략 ///
    
    submitButton() {
        checkSubmit({
            rId : recordId
            , cnt : cnt
        })
        .then(() => {
            refreshApex(this.record)
                .then(() => {
                    // do something with the refreshed data in this.opptiesOverAmount
                });
        })
        .catch((error) => {
            this.message = 'Error received: code' + error.errorCode + ', ' +
                'message ' + error.body.message;
    });
    }
}

Apex class

    @AuraEnabled(cacheable=true)
    public static Obj__c checkSubmit(String rId, Integer cnt) {
        Obj__c target = [SELECT ID FROM Obj__c WHERE Id = :rId LIMIT 1];
        update target;
        return target;
    }

 

원인

- @AuraEnabled(cacheable=true)

사실 수정을 여러가지 과정을 거쳐서 시도했는데 결국 원인은 저게 문제였다

(리턴값을 변경해보고, 리스트로 가져오는 값을 오브젝트 자체 단일값으로 바꿔보기도 하고 등등..)

JS단도 여러가지 수정을 했지만 어쨌든 가장 크리티컬한 오류는 저 친구여서 수정은 단순했다.

 

중간단계에서 알게된 개념

- (cacheable=true) 얘랑 @wire 얘는 셋트

 

- @wire

  • 세일즈포스 데이터를 가져온다의 개념이었지.. 그다음 어떻게 사용하는지 몰랐던..나^^..
  • 얘는 Property에 붙일수도 있고 Funtion에도 붙일 수 있습니다..
    • Property 
      • HTML 사이드로 움직인다
      • 리턴데이터 심플하다 
    • Funtion
      • 리턴데이터로 뭔가 가공해야한다~

- Imperative call

얘도 나중에 따로 공부 후 포스팅 예정...

 

최종 수정 (단순)

    @AuraEnabled
    public static Obj__c checkSubmit(String rId, Integer cnt) {
        Obj__c target = [SELECT ID FROM Obj__c WHERE Id = :rId LIMIT 1];
        update target;
        return target;
    }

 

참고

https://developer.salesforce.com/forums/?id=9062I000000IE4kQAG 

 

DeveloperForce

Welcome to Support! Search for an answer or ask a question of the zone or Customer Support. Need help? Dismiss + Start a Discussion

dfc-org-production.force.com

https://tempflip.medium.com/3-ways-to-call-an-apex-method-from-a-lighting-web-component-b2d2f56e447b

728x90