Guest User

Untitled

a guest
Jan 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. public class SampleController {
  2.  
  3. private ApexPages.StandardController con;
  4. public Parent2__c parent2 {get;set;}
  5. public Parent1__c parent1qry {get;set;}
  6. public List<wrapper1> wrapList1 {get;set;}
  7. public List<wrapper2> wrapList2 {get;set;}
  8. public List<Parent2__c> parent2list {get;set;}
  9. public List<Child2__c> child2list {get;set;}
  10. public Parent2__c NewParent2 {get;set;}
  11.  
  12. public SampleController(ApexPages.StandardController controller) {
  13. con = controller;
  14.  
  15. wrapList1 = new List<wrapper1>();
  16. parent2 = new Parent2__c();
  17. parent1 = new Parent1__c();
  18. parent2list = new List<Parent2__c>();
  19. child2list = new List<Child2__c>();
  20. }
  21.  
  22. public void init() {
  23.  
  24. List<Child1__c> items = new List<Child1__c>();
  25.  
  26. List<wrapper1> tempWrap = new List<wrapper1>();
  27.  
  28. if(parent2.Parent1__c != null) {
  29. parent1qry = [SELECT Id, Name, Account__c, Account__r.Name, Date__c, Total_Amount__c FROM Parent1__c WHERE Id=:parent2.Parent1__c];
  30.  
  31. 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];
  32.  
  33. }
  34.  
  35. for(Child1__c liloop : items) {
  36. tempWrap.add(new wrapper1(false,liloop));
  37. }
  38.  
  39.  
  40. if(tempWrap != null) {
  41. wrapper1 = tempWrap;
  42. }
  43. }
  44.  
  45. public PageReference save() {
  46.  
  47. List<Parent2__c> ListParent2 = new List<Parent2__c>();
  48. List<wrapper2> LineItemList = new List<wrapper2>();
  49. List<Child1__c> litem = new List<Child1__c>();
  50. Parent2__c NewParent2 = new Parent2__c();
  51.  
  52. NewParent2.Date_of_Return__c = parent2.Date_of_Return__c;
  53. NewParent2.Customer_Name__c = parent1qry.Account__c;
  54.  
  55.  
  56. Set<Id> newIds = new Set<Id>();
  57.  
  58. for(wrapper1 wloop : wrapList1) {
  59. if(wloop.IsSelected) {
  60. NewParent2.Child1__c = wloop.line.Id;
  61. if(wloop.line.Parent1__c != null) {
  62. NewParent2.Parent1__c = wloop.line.Parent1__c;
  63.  
  64. }
  65. ListParent2.add(NewParent2);
  66. }
  67. }
  68.  
  69. if(ListParent2.size() > 0) {
  70. insert ListParent2;
  71.  
  72. Set<Id> ids = new Set<Id>();
  73.  
  74. for(Parent2__c idloop : ListParent2) {
  75. ids.add(idloop.Id);
  76.  
  77. }
  78.  
  79. 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];
  80.  
  81.  
  82. parent2list = parent2qry;
  83.  
  84.  
  85. }
  86.  
  87. PageReference pageref = new PageReference('/' + NewParent2.Id);
  88. pageref.setRedirect(true);
  89. return pageref;
  90. }
  91.  
  92. public class wrapper1 {
  93. public Child1__c line {get;set;}
  94. public Boolean IsSelected {get;set;}
  95.  
  96. public wrapper1(Boolean s, Child1__c li) {
  97.  
  98. IsSelected = s;
  99. line = li;
  100.  
  101. }
  102.  
  103. }
  104.  
  105. public class wrapper2 {
  106. public Child2__c c2 {get;set;}
  107. public Integer index {get;set;}
  108.  
  109. }
  110.  
  111.  
  112. }
  113.  
  114. map<Schema.SObjectField,Schema.SObjectField> fromFldToFldMap =
  115. new map<Schema.SObjectField,SChema.SObjectField> {
  116. Child1__c.Foo__c => Child2__c.Foo__c,
  117. Child1__c.Bar__c => Child2__c.Bar__c,
  118. ... note Child2 field names don't have to be same as Child1
  119. .. but types must be same};
  120.  
  121. Wrapper2[] wrapList2 = new List<Wrapper2>();
  122. for (Wrapper1 w1 : wrapList1) { //copies all Child1s to Child2s
  123. Wrapper2 w2 = new Wrapper2();
  124. w2.c2 = new Child2__c(Parent2__c = parent2.Id); // parent of ea Child2
  125. w2.index = wrap2List.size();
  126. wrap2List.add(w2); // add to list of all Child2
  127.  
  128. // Copy values from Child1 obj to Child2 obj using our map of source name to target name
  129. // Relies on SObject class methods get(..) and put(..)
  130. for (Schema.SObjectField fldToken : fromFldToFldMap.keySet())
  131. w2.c2.put(fromFldToFldMap.get(fldToken), // name of fld in Child2
  132. w1.line.get(fldToken); // value (as Object) of corresp fld in Child1
  133. }
Add Comment
Please, Sign In to add comment