0

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. enter image description here

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;
        
    }
4
  • Which part isn't working? Do you get any errors? You need to be more specific about your problem.
    – Nick Cook
    Feb 2, 2021 at 3:53
  • @NickCook I don’t get any errors, the code is just not doing what I need it to do. I need the field values from the Check_In_Date__c and Check_Out_Dates__c on my hasRecordId(the record the lighting component is on) to populate on the same fields of my selected records (checkboxes) on my lightning component.
    – Sophia
    Feb 2, 2021 at 4:12
  • More to the point, is your apex class not working? Is it reaching the apex class? if not, where is it getting to? If so, which parts of the apex method are working the way you want? You really need to narrow down the problem.
    – Nick Cook
    Feb 2, 2021 at 5:32
  • @NickCook Sorry I am very new to coding, I'm not 100% sure of the issue.
    – Sophia
    Feb 2, 2021 at 23:53

2 Answers 2

1

force:hasRecordId only provides the record id, i.e Id field of the record that you're viewing. If you need additional fields you need to fetch it explicitly. I've modified your Apex controller to fetch Check in and Check out dates for the hasRecordId record. Let me know if this works out for you.

@AuraEnabled
public static void updateRecord(List <String> lstRecordId, String oppRecordId) {

   Set<String> oppoIds = new Set<String>(lstRecordId);
   oppoIds.add(oppRecordId);
   Map<Id,Opportunity> oppoMap = new Map<Id,Opportunity>([SELECT Id, Name, Check_In_Date__c, Check_Out_Date__c, Total_Amount__c, Deposit_Amount__c, SyncedQuoteId 
                       FROM Opportunity 
                       WHERE Id IN : oppoIds]);

   Opportunity referenceOppo = oppoMap.get(oppRecordId);

   List<Opportunity> lstUpdate = new List<Opportunity>();

   for(Opportunity Opp : oppoMap.values()){
       if(Opp.Id==oppRecordId) continue;
       Opp.Check_In_Date__c = referenceOppo.Check_In_Date__c;
       Opp.Check_Out_Date__c = referenceOppo.Check_Out_Date__c;
       lstUpdate.add(Opp);
   }

   if(lstUpdate.size() > 0){
       UPDATE lstUpdate;
   } 
}
5
  • Hi Jay, Thank you. I just tried this and it says "Invalid initializer type List<String> found for Set<Id>: expected a List or a Set of type: Id" and that the "Variable does not exist: lstUpdate". I do have other strings in my apex code, this is just the section i needed help with, but it doesn't have seemed to work.
    – Sophia
    Feb 2, 2021 at 23:57
  • @Sophia update code in the answer. Had to change Set<Id> to Set<String>. Please try now.
    – Jay Rathod
    Feb 3, 2021 at 3:23
  • Thank you. I am still getting the error that my variable lstUpdate does not exist. The other error has been fixed though
    – Sophia
    Feb 3, 2021 at 3:26
  • @Sophia please add lstUpdate declaration and initialisation. See updated answer.
    – Jay Rathod
    Feb 3, 2021 at 5:07
  • Thank you! That works perfect, I really appreciate your help.
    – Sophia
    Feb 3, 2021 at 23:45
0

Can you please provide the HTML?

In the Callback, I don't see the value of an Attribute being Set, so I don't see any update to the data on the page.

The Apex Controller's return type is Void so you don't return data to your LWC but you update the Database with a DML Update call. This Update will not show up unless you re-query the Database for it or you pass back the new value in the Callback.

1
  • Thanks for your response Christopher. I am very new to coding hence why I am having issues with my lightning aura component. Please help me with this and let me know if you need me to post anymore code.
    – Sophia
    Feb 3, 2021 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.