0

I have 2 methods in my apex class one is fetching the record and another one is for updating

public with sharing class UpdateCaseRecord {

@AuraEnabled(cacheable=true)
public static Case fetchCase(Id caseId){ 
    return [SELECT Id,EngineeringReqNumber__c FROM Case where id=:caseId];
}

@AuraEnabled
public static void updateCase(Case con, Id recTypId){
    System.debug(con);
    system.debug('============>'+recTypId);
    update con;
  }
}

So

I have to pass 2 independent record Id to both the method. As you can see in updateCase method I am passing recTypId which I decorated in @wire but its giving me null value

import getCaseRec from "@salesforce/apex/UpdateCaseRecord.fetchCase";
import updateCaseRecord from '@salesforce/apex/UpdateCaseRecord.updateCase';

@api selectedValue;
@api recordId;
 
@wire (getCaseRec , { caseId: '$recordId'})       
fetchCase( resp){
       this.wiredCase = resp;
       this.realFormData = {... this.wiredCase.data};
}

@wire (updateCaseRecord , { recTypId: '$selectedValue'})
saveRecord(event){
    updateCaseRecord({con : this.realFormData}).then(()=>{
        console.log('Refresh Apex called');
        refreshApex(this.wiredCase);
    });

}

Note

In above code @api selectedValue; contains the Id of RecordTypes, It will pass the dynamic id value of that RecordType based upon the value selected from the dropdown

3
  • Try calling updateCaseRecord without @wire
    – Badbaxx
    Commented Jul 18, 2020 at 7:05
  • For a start you forgot to pass the case back in the update record call. Next, why are you writing your own apex instead of the uiRecordApi services?
    – Phil W
    Commented Jul 18, 2020 at 8:05
  • @PhilW there is some issue with uiRecordApi which is overriding some of our functionality. that's why I prefer this way
    – Ranjan
    Commented Jul 18, 2020 at 8:51

1 Answer 1

0

Is this a combobox where the different values can be selected? Do you have a function that is handling the change event of the selectedValue? Have you tried using console.log to check if selectedValue is null before it is being sent to apex? Also it could be worth trying {recTypId: this.selectedValue} instead of {recTypId: '$selectedValue'} Also your apex method is expecting 2 arguments. You are only sending 1 argument which might be causing unexpected behavior.

Handling the change event:

handleChange(event){
    this.selectedValue = event.detail.value;
    console.log(event.detail.value);
}

Calling apex method:

@wire (updateCaseRecord , { recTypId: this.selectedValue})
saveRecord(event){
    updateCaseRecord({con : this.realFormData}).then(()=>{
        console.log('Refresh Apex called');
        refreshApex(this.wiredCase);
    });

}

Edit: you should refactor to invoke the the update imperatively. Here is some info on how to do this and the situations when @wire should and should not be used. https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex

4
  • Hi @Jemat I tried using {recTypId: this.selectedValue} instead of {recTypId: '$selectedValue'} now its working
    – Ranjan
    Commented Jul 18, 2020 at 9:21
  • This code makes no sense and is going to pointlessly call updateCaseRecord as a wire then again within the response processing function. Additionally this will cause lots of errors since you cannot call a non-cached apex function using a wire. Please refactor to avoid the use of a wire for the call to update, and instead use API property setter methods to determine when to invoke the update imperatively. Because the approach is quite wrong I have not choice but to down vote.
    – Phil W
    Commented Jul 18, 2020 at 10:40
  • @PhilW I agree that this is pointlessly calling updateCaseRecord as a wire and then again in the response processing function. They should consider refactoring this. I was just providing a solution for passing the Id without receiving a null value. No other refactors were taken into consideration in my answer.
    – Jordan
    Commented Jul 18, 2020 at 12:13
  • 1
    @Ryan as Phil mentioned you should refactor to invoke the update imperatively. Here is some info on how to do this and the situations when wire should and should not be used. developer.salesforce.com/docs/component-library/documentation/…
    – Jordan
    Commented Jul 18, 2020 at 12:30

You must log in to answer this question.

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