Could someone please help me with updating the selected Opportunity records with the desired field values from hasRecordId. I have created a lightning component on an opportunity record to display all the related records using a checkbox table. I need these selected records to update with the field values for Check_In_Date__c and Check_Out_Date__c from the hasrecordId on the click of the "Update" button. I have been stuck on this for a while, any help would be appreciated.
This is my apex and controller code I have so far but it is not successfully updating my selected checkbox records.
APEX:
@AuraEnabled
public static void updateRecord(List <String> lstRecordId, String oppRecordId) {
List<Opportunity> lstUpdate = new List<Opportunity>();
for(Opportunity Opp : [SELECT Id, Name, Check_In_Date__c, Check_Out_Date__c, Total_Amount__c, Deposit_Amount__c, SyncedQuoteId
FROM Opportunity
WHERE Id IN : lstRecordId]){
lstUpdate.add(Opp);
}
if(lstUpdate.size() > 0){
UPDATE lstUpdate;
}
CONTROLLER:
updateFields: function(component, event, helper) {
var updateId = [];
var getAllId = component.find("boxPack");
var recordId = component.get('v.recordId');
if(! Array.isArray(getAllId)){
if (getAllId.get("v.value") == true) {
updateId.push(getAllId.get("v.text"));
}
}else{
for (var i = 0; i < getAllId.length; i++) {
if (getAllId[i].get("v.value") == true) {
updateId.push(getAllId[i].get("v.text"));
}
}
}
var action = component.get('c.updateRecord');
action.setParams({
"lstRecordId":updateId,
"oppRecordId":recordId});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log(state);
$A.get('e.force:refreshView').fire();
$A.get("e.force:closeQuickAction").fire();
}
});
$A.enqueueAction(action);
},
});
UPDATE/FURTHER EXPLANATION:
I have created an aura lightning component on my 'Lead PAX' opportunity record type which shows the related 'Child PAX' opportunity records (lstRecordId) in a checkbox list. I need the fields Check_In_Date__c, Check_Out_Date__c, Total_Amount__c, Deposit_Amount__c to populate with the field values from hasRecordId (oppRecordId) which is the record with my lightning component on it. How do I change the code below so that instead of populating the values I have written into my code, it populates the values from oppRecordId?
@AuraEnabled
public static void updateRecord(List <String> lstRecordId, String oppRecordId) {
List<Opportunity> lstUpdate = new List<Opportunity>();
for(Opportunity Opp : [SELECT Id, Name, Check_In_Date__c, Check_Out_Date__c, Total_Amount__c, Deposit_Amount__c, SyncedQuoteId
FROM Opportunity
WHERE Id IN : lstRecordId]){
Opp.Check_In_Date__c = date.parse('03/02/2021');
Opp.Check_Out_Date__c = date.parse('03/02/2021');
Opp.Total_Amount__c = decimal.valueof('2281.90');
Opp.Deposit_Amount__c = decimal.valueof('200.00');
lstUpdate.add(Opp);
}
if(lstUpdate.size() > 0){
update lstUpdate;
}