Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.10 KB | None | 0 0
  1. public with sharing class T_ApprovalUnderConditionClass {
  2. public string comments{get;set;}
  3. public Opportunity oppty {get;set;}
  4. private string id;
  5. private List<Id> prosInsIds = new List<Id>();
  6. public List<ProcessInstance> prosIns{get;set;}
  7. private List<ProcessInstanceWorkitem> prosInsWrkItms = new List<ProcessInstanceWorkitem>();
  8. public List<ProcessInstance > proInstsLst{get; set;}
  9.  
  10. public T_ApprovalUnderConditionClass(ApexPages.StandardController controller) {
  11. id=ApexPages.currentPage().getParameters().get('id');
  12. oppty=[select id,Name,StageName,T_Status__c from Opportunity where id=:id];
  13. prosIns = new List<ProcessInstance>();
  14. prosIns = [SELECT Id, Status, TargetObject.Id, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending' and TargetObject.Type='Opportunity' and TargetObject.Id=:id];
  15. for(ProcessInstance prIns: prosIns){
  16. prosInsIds.add(prIns.Id);
  17. }
  18. prosInsWrkItms = [SELECT Id, ProcessInstance.Id, ProcessInstance.Status, Actor.Id, Actor.Name, Actor.Type FROM ProcessInstanceWorkitem where ProcessInstance.Id in : prosInsIds];
  19.  
  20.  
  21. }
  22.  
  23. public void appRej(String appRej){
  24.  
  25. for(ProcessInstance pi : getProsInsts()){
  26.  
  27. for(ProcessInstanceWorkitem pwi : prosInsWrkItms){
  28.  
  29. if(pi.Id == pwi.ProcessInstance.Id){
  30.  
  31. Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
  32. string usercommand='"Approve (Under Condition)" '+ Comments;
  33. req.setComments(usercommand);
  34. req.setAction(appRej);
  35. if(appRej == 'Approve')
  36. req.setNextApproverIds(new Id[] {pwi.Actor.id});
  37. List<ProcessInstanceWorkitem> procWrkItms = new List<ProcessInstanceWorkitem>();
  38. procWrkItms = [Select Id from ProcessInstanceWorkitem where ProcessInstance.Id =: pi.Id];
  39.  
  40. if((!(procWrkItms.isEmpty()) && procWrkItms[0].Id!=null)){
  41. req.setWorkitemId(procWrkItms[0].Id);
  42. //Submit the request for approval
  43. Approval.ProcessResult result2 = Approval.process(req);
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. public List<ProcessInstance> getProsInsts() {
  51.  
  52. if(proInstsLst== null) {
  53. proInstsLst = new List<ProcessInstance>();
  54. for(ProcessInstance p : [SELECT Id, TargetObject.Id, status, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending' and TargetObject.Id=:id]) {
  55. proInstsLst.add(p);
  56. }
  57. }
  58. return proInstsLst;
  59. }
  60.  
  61.  
  62. public pagereference approveAll(){
  63. appRej('Approve');
  64. Pagereference pg = new PageReference('/' + id);
  65. pg.setredirect(true);
  66. return pg;
  67.  
  68. }
  69. public pagereference rejectAll(){
  70. appRej('Reject');
  71. Pagereference pg = new PageReference('/' + id);
  72. pg.setredirect(true);
  73. return pg;
  74. }
  75.  
  76. public pagereference ApproveUnderCon(){
  77. appRej('Approve');
  78. Pagereference pg = new PageReference('/' + id);
  79. pg.setredirect(true);
  80. Opportunity opptyupdatestatus=[select id,T_Status__c from Opportunity where id=:id];
  81. if(opptyupdatestatus.T_Status__c=='Approved - DC-VF Controller'){
  82. system.debug('IfPart------->'+opptyupdatestatus.T_Status__c);
  83. opptyupdatestatus.T_Status__c='Approved Under Conditions - DC-VF Controller';
  84. }
  85. else{
  86. system.debug('elsepart------->'+opptyupdatestatus.T_Status__c);
  87. opptyupdatestatus.T_Status__c='Approved Under Conditions - DC-VF Controlling Director';
  88. }
  89. update opptyupdatestatus;
  90.  
  91. return pg;
  92.  
  93. }
  94. }
  95.  
  96. > Below is test class for the above, which shows error "NO_APPLICABLE_PROCESS, No applicable approval process was found"
  97.  
  98. @isTest
  99. public class T_ApprovalUnderConditionClass_Test {
  100. static testMethod void approvetest(){
  101. Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('T-OPP-DCVF-RT').getRecordTypeId();
  102. Account acc = new Account();
  103. acc.Name = 'ramesh';
  104. acc.montant__c=0.2;
  105. insert acc;
  106.  
  107. opportunity opp=New opportunity();
  108. opp.Name = 'Test678';
  109. opp.StageName = 'Proposal to Customer';
  110. opp.RecordTypeId=devRecordTypeId ;
  111. opp.T_Status__c='Not Started';
  112. opp.CloseDate = system.today();
  113. insert opp;
  114. opp.StageName = 'Draft';
  115. update opp;
  116.  
  117. Profile p = [select id from Profile where profile.name='T_DCVF User' limit 1];
  118.  
  119. User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test7778@gmail.com', Alias='test',Email='test7778@gmail.com', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US');
  120. Insert u;
  121. System.runAs(u) {
  122. // The following code runs as user 'u'
  123. System.debug('Current User: ' + UserInfo.getUserName());
  124. System.debug('Current Profile: ' + UserInfo.getProfileId());
  125. }
  126.  
  127. ApexPages.currentPage().getParameters().put('id',opp.id);
  128. ApexPages.StandardController sc = new ApexPages.standardController(opp);
  129. T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
  130. rp.appRej('Approve');
  131. rp.approveAll();
  132. rp.ApproveUnderCon();
  133. rp.getProsInsts();
  134. rp.rejectAll();
  135.  
  136.  
  137. List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
  138. System.assertEquals(processInstances.size(),0);
  139.  
  140.  
  141. }
  142. }
  143.  
  144. @isTest
  145.  
  146. Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
  147.  
  148. User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test7778@gmail.com', Alias='test',Email='test7778@gmail.com', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US');
  149. Insert u;
  150. System.runAs(u) {
  151.  
  152. // The following code runs as user 'u'
  153. System.debug('Current User: ' + UserInfo.getUserName());
  154. System.debug('Current Profile: ' + UserInfo.getProfileId());
  155. opportunity opp=New opportunity();
  156. opp.Name = 'Test678';
  157. opp.StageName = 'Draft';
  158. opp.RecordTypeId=devRecordTypeId ;
  159. opp.T_Status__c='Not Started';
  160. opp.CloseDate = system.today();
  161.  
  162. insert opp;
  163.  
  164. Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
  165. req.setComments('Submitting request for approval.');
  166. req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
  167. req.setObjectId(opp.Id);
  168.  
  169. Approval.ProcessResult resu = Approval.process(req);
  170.  
  171. // Verify the result
  172. System.assert(resu.isSuccess());
  173.  
  174. System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
  175. system.debug('status for approval::'+resu.getInstanceStatus());
  176. List<Id> newWorkItemIds = resu.getNewWorkitemIds();
  177. system.debug('newWorkItemIds ::'+newWorkItemIds );
  178.  
  179. Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
  180. req2.setComments('Approving request.');
  181. req2.setAction('Approve');
  182. req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
  183. system.debug('req2::'+req2);
  184. // Use the ID from the newly created item to specify the item to be worked
  185. req2.setWorkitemId(newWorkItemIds.get(0));
  186. system.debug('req3::'+req2);
  187. // Submit the request for approval
  188. Approval.ProcessResult result2 = Approval.process(req2);
  189.  
  190. // Verify the results
  191. System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
  192.  
  193. System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
  194.  
  195. ApexPages.currentPage().getParameters().put('id',opp.id);
  196. ApexPages.StandardController sc = new ApexPages.standardController(opp);
  197. T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
  198. rp.appRej('Approve');
  199. rp.approveAll();
  200. rp.ApproveUnderCon();
  201. rp.getProsInsts();
  202. rp.rejectAll();
  203.  
  204. }
  205.  
  206. }
  207.  
  208. @isTest
  209.  
  210. Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
  211.  
  212. User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test7778@gmail.com', Alias='test',Email='test7778@gmail.com', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US');
  213. Insert u;
  214. System.runAs(u) {
  215.  
  216. // The following code runs as user 'u'
  217. System.debug('Current User: ' + UserInfo.getUserName());
  218. System.debug('Current Profile: ' + UserInfo.getProfileId());
  219. opportunity opp=New opportunity();
  220. opp.Name = 'Test678';
  221. opp.StageName = 'Draft';
  222. opp.RecordTypeId=devRecordTypeId ;
  223. opp.T_Status__c='Not Started';
  224. opp.CloseDate = system.today();
  225.  
  226. insert opp;
  227.  
  228. Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
  229. req.setComments('Submitting request for approval.');
  230. req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
  231. req.setObjectId(opp.Id);
  232.  
  233. Approval.ProcessResult resu = Approval.process(req);
  234.  
  235. // Verify the result
  236. System.assert(resu.isSuccess());
  237.  
  238. System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
  239. system.debug('status for approval::'+resu.getInstanceStatus());
  240. List<Id> newWorkItemIds = resu.getNewWorkitemIds();
  241. system.debug('newWorkItemIds ::'+newWorkItemIds );
  242.  
  243. Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
  244. req2.setComments('Approving request.');
  245. req2.setAction('Approve');
  246. req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
  247. system.debug('req2::'+req2);
  248. // Use the ID from the newly created item to specify the item to be worked
  249. req2.setWorkitemId(newWorkItemIds.get(0));
  250. system.debug('req3::'+req2);
  251. // Submit the request for approval
  252. Approval.ProcessResult result2 = Approval.process(req2);
  253.  
  254. // Verify the results
  255. System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
  256.  
  257. System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
  258.  
  259. ApexPages.currentPage().getParameters().put('id',opp.id);
  260. ApexPages.StandardController sc = new ApexPages.standardController(opp);
  261. T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
  262. rp.appRej('Approve');
  263. rp.approveAll();
  264. rp.ApproveUnderCon();
  265. rp.getProsInsts();
  266. rp.rejectAll();
  267.  
  268. }
  269.  
  270. }
  271.  
  272. @isTest
  273. private class T_ApprovalUnderConditionClass_Test {
  274. static testMethod void approvetest(){
  275. devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('T-OPP-DCVF-RT').getRecordTypeId();
  276.  
  277. Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
  278.  
  279. User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test7778@gmail.com', Alias='test',Email='test7778@gmail.com', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US');
  280. Insert u;
  281. System.runAs(u) {
  282.  
  283. // The following code runs as user 'u'
  284. System.debug('Current User: ' + UserInfo.getUserName());
  285. System.debug('Current Profile: ' + UserInfo.getProfileId());
  286. opportunity opp=New opportunity();
  287. opp.Name = 'Test678';
  288. opp.StageName = 'Draft';
  289. opp.RecordTypeId=devRecordTypeId ;
  290. opp.T_Status__c='Not Started';
  291. opp.CloseDate = system.today();
  292.  
  293. insert opp;
  294.  
  295. Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
  296. req.setComments('Submitting request for approval.');
  297. req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
  298. req.setObjectId(opp.Id);
  299.  
  300. Approval.ProcessResult resu = Approval.process(req);
  301.  
  302. // Verify the result
  303. System.assert(resu.isSuccess());
  304.  
  305. System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
  306. system.debug('status for approval::'+resu.getInstanceStatus());
  307. List<Id> newWorkItemIds = resu.getNewWorkitemIds();
  308. system.debug('newWorkItemIds ::'+newWorkItemIds );
  309.  
  310. Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
  311. req2.setComments('Approving request.');
  312. req2.setAction('Approve');
  313. req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
  314. system.debug('req2::'+req2);
  315. // Use the ID from the newly created item to specify the item to be worked
  316. req2.setWorkitemId(newWorkItemIds.get(0));
  317. system.debug('req3::'+req2);
  318. // Submit the request for approval
  319. Approval.ProcessResult result2 = Approval.process(req2);
  320.  
  321. // Verify the results
  322. System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
  323.  
  324. System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
  325.  
  326. ApexPages.currentPage().getParameters().put('id',opp.id);
  327. ApexPages.StandardController sc = new ApexPages.standardController(opp);
  328. T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
  329. rp.appRej('Approve');
  330. rp.approveAll();
  331. rp.ApproveUnderCon();
  332. rp.getProsInsts();
  333. rp.rejectAll();
  334.  
  335. }
  336.  
  337. }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement