0

Parent Object = Guest_Master__c
Child Object = Payment_Master__c
On the child object I have created master-detail record of parent object Payer_Details__c

I want to create Payment_Master__c & Guest_Master__c record on click of submit button. Data should be gone into the database of Payment_Master__c & Guest_Master__c From LWC to table.

Error is

EXCEPTION_THROWN [16]|System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Payer Details]: [Payer Details]

Code

public with sharing class paymentInfoApex {
    @AuraEnabled
    public static Payment_Master__c paymentEntry(integer ObjCardNo ,integer ObjExpMonth ,integer ObjExpYear,integer ObjCvv){

        Payment_Master__c paymentObj = new Payment_Master__c(Payer_Details__c = 'Name');
        paymentObj.Card_Number__c = ObjCardNo;
        paymentObj.Expiry_Month__c = ObjExpMonth;
        paymentObj.Expiry_Year__c = ObjExpYear;
        paymentObj.CVV__c = ObjCvv;
        
        Guest_Master__c guestReference = new Guest_Master__c(External_Field__c='LWC111111');
        paymentObj.Payer_Details__c = guestReference.Id;
        
        Guest_Master__c parentObj =  new Guest_Master__c(Name = 'Name', First_Name__c = 'ABC', Last_Name__c = 'ABC', Email_Id__c = '[email protected]', Phone_Number__c = '9637963758', External_Field__c = 'LWC111111');
  
        Database.SaveResult[] results = Database.insert(new SObject[]{parentObj,paymentObj});
        
        for (Integer i = 0; i < results.size(); i++) {
            if(results[i].isSuccess()){
                System.debug('Successfully created ID: '+ results[i].getId());
            }else{
                System.debug('Error:could not create sobject for array element '+ i +'.');
                System.debug('The error reported was: '+ results[i].getErrors()[0].getMessage() + '\n');
            }
        }

        return paymentObj;
    }
}
2
  • Can you try with: paymentObj.Payer_Details__c = guestReference instead of guestReference .Id? Commented Feb 27, 2022 at 10:59
  • I tried but it's giving error Illegal assignment from Guest_Master__c to Id Commented Feb 27, 2022 at 11:14

1 Answer 1

0

why don't you try separate insert statements with database.savepoint

Guest_Master__c parentObj =  new Guest_Master__c(Name = 'Name', First_Name__c = 'ABC', Last_Name__c = 'ABC', Email_Id__c = '[email protected]', Phone_Number__c = '9637963758', External_Field__c = 'LWC111111');

Payment_Master__c paymentObj = new Payment_Master__c(Payer_Details__c = 'Name');
        paymentObj.Card_Number__c = ObjCardNo;
        paymentObj.Expiry_Month__c = ObjExpMonth;
        paymentObj.Expiry_Year__c = ObjExpYear;
        paymentObj.CVV__c = ObjCvv;

System.SavePoint dbsaveState = Database.setSavepoint();
try{    
  insert parentObj;
  paymentObj.Payer_Details__c = parentObj.Id;
   insert paymentObj;

}catch(Exception ex){
  Database.rollback(dbsaveState);
  throw new AuraHandledException(e.getMessage());
} 

You must log in to answer this question.

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