+ Start a Discussion
Pramodh KumarPramodh Kumar 

edit parent and child in same vf page

I have requirement to edit parent and child on same page, I have four level deep on one page, like grand parent, parent, child and grand child.


Please help with any suggestions
Best Answer chosen by Pramodh Kumar
Mudasir WaniMudasir Wani
Hi Promod,

To write a test class for a wrapper you need to create the object data.
In your case create parent records and creste child records and associate child to the parents.
Then call the wrapper.
Let me know if you have any question.

Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 

All Answers

Mudasir WaniMudasir Wani
Hi Pramodh,

You can use wrapper class to achieve this.
Fetch all the objects and create a wrapper of those.
See the below example where they have a boolean added. But you can add different objects

https://developer.salesforce.com/page/Wrapper_Class
Let me know if you have any question.
Pramodh KumarPramodh Kumar
thanks for the reply but using wrapper class how you able to edit and save the child and grand child records..
Mudasir WaniMudasir Wani
Hi Promod,

Say you have following records

Parent__c
Child__c
 
Hi Promod,

Say you have following records

Parent__c
Child__c

//Fetch all childs 
List<Child__c> allChilds = [Select name,Id,lookupName__c from Child__c ];
//Parent Id set
Set<id> parentIdSet = new Set<id>();
//Create parent Id set 
for(Child__c childs :allChilds){
	parentIdSet.add(childs.lookupName__c);
}

//Fetch all associated parents
List<Parent__c> allAssocaiatedParents = [Select name from Parent__c where Id IN : parentIdSet];

//For loop to set data
for(Child__c childRec : allChilds){
	for(Parent__c parentRec :allAssocaiatedParents){
		if(parentRec.Id == childRec.lookupName__c){
			myWrapperClass wrapRec = new myWrapperClass();
			wrapRec.parentsToFetch = parentRec;
			wrapRec.childsToFetch = childRec;
			wrapperList.add(wrapRec);
		}
	}
}
//Wrapper list 
public List<myWrapperClass> wrapperList {get; set;}
//Your wrapper 
public class myWrapperClass{
	public Parent__c parentsToFetch{get;set;}
	public Child__c childsToFetch{get;set;}
	public Boolean selected {get; set;} 
	public myWrapperClass() { 
         selected = false; 
      } 
}


//Updating your records

public list<Parent__c> parentListToUpdate = new list<Parent__c>();
public list<Child__c> childsToUpdate = new list<Child__c>();


//Say you have an action from page as updateRecords
public void updateRecords(){
	//go for a for loop
	for(myWrapperClass wrapRecNew : wrapperList){
		// Only update selected records
		if(wrapRecNew.selected){
			parentListToUpdate.add(wrapRecNew.parentsToFetch);
			childsToUpdate.add(wrapRecNew.childsToFetch);
		}
	}
	update parentListToUpdate;
	update childsToUpdate;
}


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
Pramodh KumarPramodh Kumar
i dont have any checkbox to selecte the particulat object, but i have buttons to edit the page, 

here is the snapshot of my page
User-added image
Pramodh KumarPramodh Kumar
could you please share how to write test class for wrapper class.
Mudasir WaniMudasir Wani
Hi Promod,

To write a test class for a wrapper you need to create the object data.
In your case create parent records and creste child records and associate child to the parents.
Then call the wrapper.
Let me know if you have any question.

Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
This was selected as the best answer