+ Start a Discussion
SFDC pvSFDC pv 

Hi, I have Custom object (Passenger) which is master-details of case. I need to insert the passenger records with CaseId and Case's Account ID via lightning component. How to do that

GulshanRajGulshanRaj
You can use lightning event and set default values as mentioned in this url:
https://developer.salesforce.com/docs/component-library/bundle/force:createRecord/documentation


Please let me know if you have any question.

Thanks
Gulshan Raj



 
SFDC pvSFDC pv
Hi Gulshan,

Thanks for you reply. I have search button once i search i am getting response from the thirt party and displaying as the list(I kept multiselect checkbox) so once they select it i need to insert the records with Case ID i.e parent ID and Case's account id. For this functionality , Does event will work ?  I tried with Force:createrecord , but it's not working. CaseId i got successfully only account id amn't getting. Please help am new to lightning. Please suggest if there is some other options to achieve this

In parent component am having lightning icon once they click on it am calling child component wherein i handled the above logic

public with sharing class SearchController {
    
    @auraEnabled
    Public static List<string> invokeWebServiceString ssnumber) {
        String [] arrayOfResponsestrings = new List<String>();

        for(String num : ssnumber.split(',')){          
            ResponseWrapper APIResponse = InvokeAPI.invokeRespAPI(num);
            arrayOfResponsestrings.add(JSON.serialize(APIResponse ));
            System.debug('arrayOfResponsestrings :'+arrayOfResponsestrings);
        }
        for(Integer i=0 ;i<arrayOfResponsestrings.size() ; i++) {
            system.debug('IterateoverResponseofStrings- 1 '+arrayOfResponsestrings[i]);
        }
        return arrayOfResponsestrings;
    } 

//Apex controller to get the passenger Info    
    @AuraEnabled
    public static void addParentCase(String AccountId ,String CaseId, List<String> lstOfFFIds){
        list<Passenger__C> lstPassengers = new list<Passenger__C>();
        for(string sFreqFlyerId : lstOfFFIds){
            Passenger__C FFrecs = new Passenger__C();
            FFrecs.Id = sFreqFlyerId;
            FFrecs.Case__c = CaseId;
            FFrecs.Account__c = AccountId;
            lstPassengers.add(FFrecs);
            system.debug('FreqFlyers'+lstPassengers);
        }
        insert lstPassengers;
    }
}
Component:-
                    <div class="slds-modal__footer">
                        <lightning:button label="Submit" class="slds-button_brand"  onclick="{!c.handleSelectedFFs}" />
                        <button class="slds-button slds-button--neutral" onclick="{!c.closeModal}" >Cancel</button>
                    </div>


Controller***
(
    closeModal: function(component, event, helper) {
        //For Close Modal, Set the "openModal" attribute to "false"  
        component.set("v.isOpen", false);
    },
    Search: function(component, event, helper) {
            helper.SearchHelper(component, event);
    },
        //Process the selected contacts
    handleSelectedFFs: function(component, event, helper) {
        var selectedFFs = [];
        var checkvalue = component.find("checkFreqFlyer");
        if(!Array.isArray(checkvalue)){
            console.log('IFCONDITION'); 
            if (checkvalue.get("v.value") == true) {
                selectedFFs.push(checkvalue.get("v.text"));
            }
        }else{
            for (var i = 0; i < checkvalue.length; i++) {
                console.log('For loop checkvalue'); 
                if (checkvalue[i].get("v.value") == true) {
                    console.log('For loop if checkvalue'+checkvalue[i].get("v.text"));
                    selectedFFs.push(checkvalue[i].get("v.text"));
                }
            }
        }
        helper.CreateHelper(component, event, selectedFFs);        
    },
    
    //Select all contacts
    handleSelectAllFF: function(component, event, helper) {
        var getID = component.get("v.freqFlyerList");
        var checkvalue = component.find("selectAll").get("v.value");        
        var checkFreqFlyer = component.find("checkFreqFlyer"); 
        if(checkvalue == true){
            for(var i=0; i<checkFreqFlyer.length; i++){
                checkFreqFlyer[i].set("v.value",true);
            }
        }
        else{ 
            for(var i=0; i<checkFreqFlyer.length; i++){
                checkFreqFlyer[i].set("v.value",false);
            }
        }
    },    
})
Helper : 
CreateHelper: function(component, event, childRecordsIds) {
        //call apex class method
        var action = component.get('c.addParentCase');
            console.log('Insidemethod');
            console.log('CaseId'+component.get("v.v.CaseId"));
            console.log('accid'+component.get("v.simpleRecord"));
        action.setParams({
            "CaseId": component.get("v.CaseId"),   // This id am getting from the parent and setting it in child component .. Here am getting the caseId value. Only account value am not getting
            "AccountId": component.get("v.simpleRecord").AccountId,
            "lstOfFFIds": childRecordsIds
        });
            
        action.setCallback(this, function(response) {
            var state = response.getState();
            console.log('state'+state);
            if (state === "SUCCESS") {
                // This standard 'e.force:refreshView' and 'e.force:showToast' event not work from lightning appliation
                
                // display SUCCESS message
                 var toastEvent = $A.get("e.force:showToast");
                      toastEvent.setParams({
                        "title": "Success!",
                        "message": "The Child record's has been added successfully."
                    });
                    toastEvent.fire();
              
                // refresh/reload the page view
                $A.get('e.force:refreshView').fire();
                
                // call init function again [clear selected checkboxes]
                this.SearchHelper(component,event);
                  
            }
        });
        $A.enqueueAction(action);   
    },
 
GulshanRajGulshanRaj
Can you please share what you are getting with console.log('accid'+component.get("v.simpleRecord")) which you are using in CreateHelper (helper).
SFDC pvSFDC pv
Hi , I'm getting accid undefined
GulshanRajGulshanRaj
That's the reason  "AccountId": component.get("v.simpleRecord").AccountId, is invalid. You have to populate simpleRecord first.
SFDC pvSFDC pv
Hi Gulshan, I have given the below snippet in the component . Could you please let me know what else i need to do populate. Your help would be greatly appreciated.
    <!-- Attributes - Lightning Data Service -->
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>
    <aura:attribute name="Case" type="Object"/>   

<force:recordData aura:id="recordLoader"
      recordId="{!v.recordId}"
      fields="AccountId"
      targetRecord="{!v.Case}"
      targetFields="{!v.simpleRecord}"
      targetError="{!v.recordError}"
      />
 
SFDC pvSFDC pv
Hi Gulshan, I have 2 component . In first component i have created lightning icon that is on the case object and on click of the icon i am calling child component which is Pop up window.

Since am placing this Lightning data service(above snippet) in the child component thats why it is not getting the record id and it's details i guess. Please correct me if i am wrong. 

Let me know if any other options to make this work. Thanks.