Advertisement
Guest User

ContactPrimaryCheck

a guest
May 17th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. public with sharing class opportunityContactEntryExtension {
  2.  
  3. public Opportunity theOpp {get;set;}
  4. public String searchString {get;set;}
  5. public Opportunity_Contact_Role__c[] chosencontacts {get;set;}
  6. public contact[] availablecontacts {get;set;}
  7. public Account theAccount {get;set;}
  8.  
  9. public String toSelect {get; set;}
  10. public String toUnselect {get; set;}
  11. public Decimal Total {get;set;}
  12.  
  13. public Boolean overLimit {get;set;}
  14.  
  15. private Opportunity_Contact_Role__c[] forDeletion = new Opportunity_Contact_Role__c[]{};
  16.  
  17.  
  18. public opportunityContactEntryExtension(ApexPages.StandardsetController controller) {
  19.  
  20. Id opptyId = ApexPages.currentPage().getParameters().get('id');
  21.  
  22. // Get information about the Opportunity being worked on
  23. theOpp = [select Id, AccountId, Account.Name from Opportunity where Id = :opptyId limit 1];
  24.  
  25. // If products were previously selected need to put them in the "selected products" section to start with
  26. chosencontacts = [
  27. select Id, Contact__c, Contact_Name__c, Opportunity__c, Account_Manager_Notes__c, Date_Demo_Completed__c,
  28. Delay_Followup_Until__c, Demo_Status__c, Demo_Time_zone__c, Demo_Tracked_On__c, PreDemo_Info_Quality__c,
  29. Primary__c, Role__c, Sales_Scientist_Notes__c, Time_Demo_Requested__c
  30. from Opportunity_Contact_Role__c
  31. where Opportunity__c = :theOpp.Id
  32. ];
  33.  
  34. //show associated account
  35. theAccount = theOpp.Account;
  36. updateAvailableList();
  37.  
  38.  
  39. }
  40.  
  41. // this is the 'action' method on the page
  42. public PageReference AccountCheck(){
  43. //if there is only one Account we go with it and save the opp
  44. if(theOpp.AccountId != theAccount.Id){
  45. try{
  46. theOpp.AccountId = theAccount.Id;
  47. update(theOpp);
  48. }
  49. catch(Exception e){
  50. ApexPages.addMessages(e);
  51. }
  52. }
  53.  
  54. return null;
  55.  
  56. }
  57.  
  58.  
  59. public void updateAvailableList() {
  60.  
  61. // We dynamically build a query string and exclude items already in the shopping cart
  62. String qString = 'select Id, AccountId, name, firstname, lastname, email, phone from contact where AccountId = \'' + theAccount.Id + '\'';
  63.  
  64. // note that we are looking for the search string entered by the user in the First Name OR Last Name
  65. // modify this to search other fields if desired
  66. if(searchString!=null){
  67. qString+= ' and (contact.FirstName like \'%' + searchString + '%\' or contact.LastName like \'%' + searchString + '%\')';
  68. }
  69.  
  70. Set<Id> selectedEntries = new Set<Id>();
  71. for(Opportunity_Contact_Role__c d:chosencontacts){
  72. selectedEntries.add(d.contact__c);
  73. }
  74.  
  75. if(selectedEntries.size()>0){
  76. String tempFilter = ' and Id not in (';
  77. for(Id i : selectedEntries){
  78. tempFilter+= '\'' + (String)i + '\',';
  79. }
  80. String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
  81. extraFilter+= ')';
  82.  
  83. qString+= extraFilter;
  84. }
  85.  
  86. qString+= ' order by Contact.Name';
  87. qString+= ' limit 101';
  88.  
  89. system.debug('qString:' +qString);
  90. availablecontacts = database.query(qString);
  91.  
  92. // We only display up to 100 results... if there are more than we let the user know (see vf page)
  93. if(availablecontacts.size()==101){
  94. availablecontacts.remove(100);
  95. overLimit = true;
  96. }
  97. else{
  98. overLimit=false;
  99. }
  100. }
  101.  
  102. public void addTochosencontacts(){
  103.  
  104. // This function runs when a user hits "select" button next to a contact
  105.  
  106. for(contact d : availablecontacts){
  107. if((String)d.Id==toSelect){
  108. chosencontacts.add(new Opportunity_Contact_Role__c(Opportunity__c=theOpp.Id, contact__c=d.id, role__c='Decision Maker'));
  109. break;
  110. }
  111. }
  112.  
  113. updateAvailableList();
  114. }
  115.  
  116.  
  117. public PageReference removeFromchosencontacts(){
  118.  
  119. // This function runs when a user hits "remove" on an item in the "Chosen Contacts" section
  120.  
  121. Integer count = 0;
  122.  
  123. for(Opportunity_Contact_Role__c d : chosencontacts){
  124. if((String)d.contact__c==toUnselect){
  125.  
  126. if(d.Id!=null)
  127. forDeletion.add(d);
  128.  
  129. chosencontacts.remove(count);
  130. break;
  131. }
  132. count++;
  133. }
  134.  
  135. updateAvailableList();
  136.  
  137. return null;
  138. }
  139.  
  140. public PageReference onSave(){
  141.  
  142. // If previously selected contacts are now removed, we need to delete them
  143. if(forDeletion.size()>0)
  144. delete(forDeletion);
  145.  
  146. // Previously selected contacts may have new quantities and amounts, and we may have new products listed, so we use upsert here
  147. try{
  148. if (chosencontacts.size() > 0)
  149. {
  150. // go through choosenContacts and check if any has primary__c set to true
  151. Boolean primaryFound = false;
  152. for (Opportunity_Contact_Role__c ocr:chosencontacts)
  153. {
  154. if (ocr.Primary__c == true) {
  155. primaryFound = true;
  156.  
  157. }
  158. }
  159.  
  160. // if there's a primary upsert em
  161.  
  162. if (PrimaryFound = true){
  163. upsert(chosencontacts);
  164. }
  165. // otherwise show an error
  166. else {
  167. ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'YOUR ERROR MESSAGE HERE'));
  168. return null;
  169. }
  170. }
  171. }
  172.  
  173. catch(Exception e){
  174. ApexPages.addMessages(e);
  175. return null;
  176. }
  177.  
  178. // After save return the user to the Opportunity
  179. return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
  180. }
  181.  
  182. public PageReference onCancel(){
  183.  
  184. // If user hits cancel we commit no changes and return them to the Opportunity
  185. return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
  186. }
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement