Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SampleController {
- private ApexPages.StandardController con;
- public Parent2__c parent2 {get;set;}
- public Parent1__c parent1qry {get;set;}
- public List<wrapper1> wrapList1 {get;set;}
- public List<wrapper2> wrapList2 {get;set;}
- public List<Parent2__c> parent2list {get;set;}
- public List<Child2__c> child2list {get;set;}
- public Parent2__c NewParent2 {get;set;}
- public SampleController(ApexPages.StandardController controller) {
- con = controller;
- wrapList1 = new List<wrapper1>();
- parent2 = new Parent2__c();
- parent1 = new Parent1__c();
- parent2list = new List<Parent2__c>();
- child2list = new List<Child2__c>();
- }
- public void init() {
- List<Child1__c> items = new List<Child1__c>();
- List<wrapper1> tempWrap = new List<wrapper1>();
- if(parent2.Parent1__c != null) {
- parent1qry = [SELECT Id, Name, Account__c, Account__r.Name, Date__c, Total_Amount__c FROM Parent1__c WHERE Id=:parent2.Parent1__c];
- items = [SELECT Id, Parent1__c, Item__c, Item__r.Name, Quantity__c, Unit_Price__c, Total_Price__c FROM Child1__c WHERE Parent1__c =: parent2.Parent1__c];
- }
- for(Child1__c liloop : items) {
- tempWrap.add(new wrapper1(false,liloop));
- }
- if(tempWrap != null) {
- wrapper1 = tempWrap;
- }
- }
- public PageReference save() {
- List<Parent2__c> ListParent2 = new List<Parent2__c>();
- List<wrapper2> LineItemList = new List<wrapper2>();
- List<Child1__c> litem = new List<Child1__c>();
- Parent2__c NewParent2 = new Parent2__c();
- NewParent2.Date_of_Return__c = parent2.Date_of_Return__c;
- NewParent2.Customer_Name__c = parent1qry.Account__c;
- Set<Id> newIds = new Set<Id>();
- for(wrapper1 wloop : wrapList1) {
- if(wloop.IsSelected) {
- NewParent2.Child1__c = wloop.line.Id;
- if(wloop.line.Parent1__c != null) {
- NewParent2.Parent1__c = wloop.line.Parent1__c;
- }
- ListParent2.add(NewParent2);
- }
- }
- if(ListParent2.size() > 0) {
- insert ListParent2;
- Set<Id> ids = new Set<Id>();
- for(Parent2__c idloop : ListParent2) {
- ids.add(idloop.Id);
- }
- List<Parent2__c> parent2qry = [SELECT Id, Name, Customer_Name__c, Date_of_Return__c, Parent1__c, Child1__c FROM Parent2__c WHERE ID IN : ids];
- parent2list = parent2qry;
- }
- PageReference pageref = new PageReference('/' + NewParent2.Id);
- pageref.setRedirect(true);
- return pageref;
- }
- public class wrapper1 {
- public Child1__c line {get;set;}
- public Boolean IsSelected {get;set;}
- public wrapper1(Boolean s, Child1__c li) {
- IsSelected = s;
- line = li;
- }
- }
- public class wrapper2 {
- public Child2__c c2 {get;set;}
- public Integer index {get;set;}
- }
- }
- map<Schema.SObjectField,Schema.SObjectField> fromFldToFldMap =
- new map<Schema.SObjectField,SChema.SObjectField> {
- Child1__c.Foo__c => Child2__c.Foo__c,
- Child1__c.Bar__c => Child2__c.Bar__c,
- ... note Child2 field names don't have to be same as Child1
- .. but types must be same};
- Wrapper2[] wrapList2 = new List<Wrapper2>();
- for (Wrapper1 w1 : wrapList1) { //copies all Child1s to Child2s
- Wrapper2 w2 = new Wrapper2();
- w2.c2 = new Child2__c(Parent2__c = parent2.Id); // parent of ea Child2
- w2.index = wrap2List.size();
- wrap2List.add(w2); // add to list of all Child2
- // Copy values from Child1 obj to Child2 obj using our map of source name to target name
- // Relies on SObject class methods get(..) and put(..)
- for (Schema.SObjectField fldToken : fromFldToFldMap.keySet())
- w2.c2.put(fromFldToFldMap.get(fldToken), // name of fld in Child2
- w1.line.get(fldToken); // value (as Object) of corresp fld in Child1
- }
Add Comment
Please, Sign In to add comment