0

I need to assign the child of a parent record that is in the same method. Would I have to insert it before assigning the Id to a parent?

        //Add row method for both lists
    public PageReference add_record(){
        //Recipient fields
        Recipient__c anotherRecipient = new Recipient__c();
        anotherRecipient.Name = rec.Name;
        anotherRecipient.Last_Name__c = rec.Last_Name__c;
        anotherRecipient.School__c = rec.School__c;
        anotherRecipient.Specialty__c = rec.Specialty__c;

        
    
        //Scholarship fields
        Scholarship_Award__c anotherScholarship = new Scholarship_Award__c();
        anotherScholarship.Award__c = sch.Award__c;
        anotherScholarship.Year__c = sch.Year__c;
        anotherScholarship.School__c = sch.School__c;
        
        //Tried this - isn't going to work
        //anotherScholarship.Recipient__c = rec.Id;

        //Add to lists
        recList.add(anotherRecipient);
        anotherScholarship.Recipient__c = rec.Id;
        anotherScholarship.School__c = rec.School__c;
        schList.add(anotherScholarship);
        
        return null;
    }

BTW I have finally solved the issue I was having displaying 2 objects in the same pageBlockTable by just having 2 tables side by side - thank you to everyone who answered, learnt a lot anyway.

0

It is possible to do them together but you need to use an External Id field. A good place to start is probably here: https://developer.salesforce.com/blogs/engineering/2012/02/cascade-insert-with-external-id-fields.html

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.