function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stéphane C.Stéphane C. 

How to update Child Records based on Parent Record

Hi,

I am writing an inline visualforce page to update parent records and thier child records.

The visualforce page have a controller :
 
public class inlineDetail {
public List<contact> contactList=new List<contact>();
public List<course__c> courseList=new List<course__c>();

public List<contact> getContacts() {
 contactList=[select name,accountId,phone,AssistantPhone,email, (select name, classroom__c, comments__c from Courses__r) from Contact];
    return contactList;
}

public void saveChanges() {
    update contactList;
    if(!courseList.isEmpty())
        update courseList;
}
    
}
I want to update the parent records and the "comments" child records when they change.

I can't see the way.

Any idea? Thank you!
 
Best Answer chosen by Stéphane C.
Stéphane C.Stéphane C.
Ok. here is one solution :
 
public class inlineDetail {

	public List<Contact> contactsList = new List<Contact>();   
    
    public List<Contact> getContacts() {
        contactsList = [SELECT Name,accountId,Phone,AssistantPhone,Email, (SELECT Name, Classroom__c, Comments__c from Courses__r) FROM Contact ORDER BY Name];
        return contactsList;
    }
    
    public void saveChanges() {
        List<Course__c> courses = new List<Course__c>();
        for (Contact c : contactsList) {
			courses.addAll(c.Courses__r);
		}
		update courses;
        update contactsList;
    }
    
}

Any comments will be appreciated.
 

All Answers

Stéphane C.Stéphane C.
Try some codes but not always working :
 
public class inlineDetail {

	public List<contact> contactsList = new List<contact>();
    
    public String sortOrder = 'Name';

    public void sortByName() {
        this.sortOrder = 'Name';
    }
    
    public List<Contact> getContacts() {
        contactsList = [SELECT Name,accountId,Phone,AssistantPhone,Email, (SELECT Name, Classroom__c, Comments__c from Courses__r) FROM Contact ORDER BY Name];
        return contactsList;
    }

    public List<course__c> coursesList = new List<course__c>();
    
    For(Contact c: coursesList){
        If(!c.comments.isEmpty()){
            coursesList.add(c);
        }
    } 
    
    public void saveChanges() {
        update contactsList;
        If(!coursesList.isEmpty())
            update coursesList;
    }
    
}

Any recommendations? Thank you.
Stéphane C.Stéphane C.
Ok. here is one solution :
 
public class inlineDetail {

	public List<Contact> contactsList = new List<Contact>();   
    
    public List<Contact> getContacts() {
        contactsList = [SELECT Name,accountId,Phone,AssistantPhone,Email, (SELECT Name, Classroom__c, Comments__c from Courses__r) FROM Contact ORDER BY Name];
        return contactsList;
    }
    
    public void saveChanges() {
        List<Course__c> courses = new List<Course__c>();
        for (Contact c : contactsList) {
			courses.addAll(c.Courses__r);
		}
		update courses;
        update contactsList;
    }
    
}

Any comments will be appreciated.
 
This was selected as the best answer