Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class T_ApprovalUnderConditionClass {
- public string comments{get;set;}
- public Opportunity oppty {get;set;}
- private string id;
- private List<Id> prosInsIds = new List<Id>();
- public List<ProcessInstance> prosIns{get;set;}
- private List<ProcessInstanceWorkitem> prosInsWrkItms = new List<ProcessInstanceWorkitem>();
- public List<ProcessInstance > proInstsLst{get; set;}
- public T_ApprovalUnderConditionClass(ApexPages.StandardController controller) {
- id=ApexPages.currentPage().getParameters().get('id');
- oppty=[select id,Name,StageName,T_Status__c from Opportunity where id=:id];
- prosIns = new List<ProcessInstance>();
- prosIns = [SELECT Id, Status, TargetObject.Id, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending' and TargetObject.Type='Opportunity' and TargetObject.Id=:id];
- for(ProcessInstance prIns: prosIns){
- prosInsIds.add(prIns.Id);
- }
- prosInsWrkItms = [SELECT Id, ProcessInstance.Id, ProcessInstance.Status, Actor.Id, Actor.Name, Actor.Type FROM ProcessInstanceWorkitem where ProcessInstance.Id in : prosInsIds];
- }
- public void appRej(String appRej){
- for(ProcessInstance pi : getProsInsts()){
- for(ProcessInstanceWorkitem pwi : prosInsWrkItms){
- if(pi.Id == pwi.ProcessInstance.Id){
- Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
- string usercommand='"Approve (Under Condition)" '+ Comments;
- req.setComments(usercommand);
- req.setAction(appRej);
- if(appRej == 'Approve')
- req.setNextApproverIds(new Id[] {pwi.Actor.id});
- List<ProcessInstanceWorkitem> procWrkItms = new List<ProcessInstanceWorkitem>();
- procWrkItms = [Select Id from ProcessInstanceWorkitem where ProcessInstance.Id =: pi.Id];
- if((!(procWrkItms.isEmpty()) && procWrkItms[0].Id!=null)){
- req.setWorkitemId(procWrkItms[0].Id);
- //Submit the request for approval
- Approval.ProcessResult result2 = Approval.process(req);
- }
- }
- }
- }
- }
- public List<ProcessInstance> getProsInsts() {
- if(proInstsLst== null) {
- proInstsLst = new List<ProcessInstance>();
- for(ProcessInstance p : [SELECT Id, TargetObject.Id, status, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending' and TargetObject.Id=:id]) {
- proInstsLst.add(p);
- }
- }
- return proInstsLst;
- }
- public pagereference approveAll(){
- appRej('Approve');
- Pagereference pg = new PageReference('/' + id);
- pg.setredirect(true);
- return pg;
- }
- public pagereference rejectAll(){
- appRej('Reject');
- Pagereference pg = new PageReference('/' + id);
- pg.setredirect(true);
- return pg;
- }
- public pagereference ApproveUnderCon(){
- appRej('Approve');
- Pagereference pg = new PageReference('/' + id);
- pg.setredirect(true);
- Opportunity opptyupdatestatus=[select id,T_Status__c from Opportunity where id=:id];
- if(opptyupdatestatus.T_Status__c=='Approved - DC-VF Controller'){
- system.debug('IfPart------->'+opptyupdatestatus.T_Status__c);
- opptyupdatestatus.T_Status__c='Approved Under Conditions - DC-VF Controller';
- }
- else{
- system.debug('elsepart------->'+opptyupdatestatus.T_Status__c);
- opptyupdatestatus.T_Status__c='Approved Under Conditions - DC-VF Controlling Director';
- }
- update opptyupdatestatus;
- return pg;
- }
- }
- > Below is test class for the above, which shows error "NO_APPLICABLE_PROCESS, No applicable approval process was found"
- @isTest
- public class T_ApprovalUnderConditionClass_Test {
- static testMethod void approvetest(){
- Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('T-OPP-DCVF-RT').getRecordTypeId();
- Account acc = new Account();
- acc.Name = 'ramesh';
- acc.montant__c=0.2;
- insert acc;
- opportunity opp=New opportunity();
- opp.Name = 'Test678';
- opp.StageName = 'Proposal to Customer';
- opp.RecordTypeId=devRecordTypeId ;
- opp.T_Status__c='Not Started';
- opp.CloseDate = system.today();
- insert opp;
- opp.StageName = 'Draft';
- update opp;
- Profile p = [select id from Profile where profile.name='T_DCVF User' limit 1];
- 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');
- Insert u;
- System.runAs(u) {
- // The following code runs as user 'u'
- System.debug('Current User: ' + UserInfo.getUserName());
- System.debug('Current Profile: ' + UserInfo.getProfileId());
- }
- ApexPages.currentPage().getParameters().put('id',opp.id);
- ApexPages.StandardController sc = new ApexPages.standardController(opp);
- T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
- rp.appRej('Approve');
- rp.approveAll();
- rp.ApproveUnderCon();
- rp.getProsInsts();
- rp.rejectAll();
- List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
- System.assertEquals(processInstances.size(),0);
- }
- }
- @isTest
- Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
- 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');
- Insert u;
- System.runAs(u) {
- // The following code runs as user 'u'
- System.debug('Current User: ' + UserInfo.getUserName());
- System.debug('Current Profile: ' + UserInfo.getProfileId());
- opportunity opp=New opportunity();
- opp.Name = 'Test678';
- opp.StageName = 'Draft';
- opp.RecordTypeId=devRecordTypeId ;
- opp.T_Status__c='Not Started';
- opp.CloseDate = system.today();
- insert opp;
- Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
- req.setComments('Submitting request for approval.');
- req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
- req.setObjectId(opp.Id);
- Approval.ProcessResult resu = Approval.process(req);
- // Verify the result
- System.assert(resu.isSuccess());
- System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
- system.debug('status for approval::'+resu.getInstanceStatus());
- List<Id> newWorkItemIds = resu.getNewWorkitemIds();
- system.debug('newWorkItemIds ::'+newWorkItemIds );
- Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
- req2.setComments('Approving request.');
- req2.setAction('Approve');
- req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
- system.debug('req2::'+req2);
- // Use the ID from the newly created item to specify the item to be worked
- req2.setWorkitemId(newWorkItemIds.get(0));
- system.debug('req3::'+req2);
- // Submit the request for approval
- Approval.ProcessResult result2 = Approval.process(req2);
- // Verify the results
- System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
- System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
- ApexPages.currentPage().getParameters().put('id',opp.id);
- ApexPages.StandardController sc = new ApexPages.standardController(opp);
- T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
- rp.appRej('Approve');
- rp.approveAll();
- rp.ApproveUnderCon();
- rp.getProsInsts();
- rp.rejectAll();
- }
- }
- @isTest
- Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
- 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');
- Insert u;
- System.runAs(u) {
- // The following code runs as user 'u'
- System.debug('Current User: ' + UserInfo.getUserName());
- System.debug('Current Profile: ' + UserInfo.getProfileId());
- opportunity opp=New opportunity();
- opp.Name = 'Test678';
- opp.StageName = 'Draft';
- opp.RecordTypeId=devRecordTypeId ;
- opp.T_Status__c='Not Started';
- opp.CloseDate = system.today();
- insert opp;
- Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
- req.setComments('Submitting request for approval.');
- req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
- req.setObjectId(opp.Id);
- Approval.ProcessResult resu = Approval.process(req);
- // Verify the result
- System.assert(resu.isSuccess());
- System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
- system.debug('status for approval::'+resu.getInstanceStatus());
- List<Id> newWorkItemIds = resu.getNewWorkitemIds();
- system.debug('newWorkItemIds ::'+newWorkItemIds );
- Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
- req2.setComments('Approving request.');
- req2.setAction('Approve');
- req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
- system.debug('req2::'+req2);
- // Use the ID from the newly created item to specify the item to be worked
- req2.setWorkitemId(newWorkItemIds.get(0));
- system.debug('req3::'+req2);
- // Submit the request for approval
- Approval.ProcessResult result2 = Approval.process(req2);
- // Verify the results
- System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
- System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
- ApexPages.currentPage().getParameters().put('id',opp.id);
- ApexPages.StandardController sc = new ApexPages.standardController(opp);
- T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
- rp.appRej('Approve');
- rp.approveAll();
- rp.ApproveUnderCon();
- rp.getProsInsts();
- rp.rejectAll();
- }
- }
- @isTest
- private class T_ApprovalUnderConditionClass_Test {
- static testMethod void approvetest(){
- devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('T-OPP-DCVF-RT').getRecordTypeId();
- Profile p = [select id from Profile where profile.name='T DCVF User' limit 1];
- 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');
- Insert u;
- System.runAs(u) {
- // The following code runs as user 'u'
- System.debug('Current User: ' + UserInfo.getUserName());
- System.debug('Current Profile: ' + UserInfo.getProfileId());
- opportunity opp=New opportunity();
- opp.Name = 'Test678';
- opp.StageName = 'Draft';
- opp.RecordTypeId=devRecordTypeId ;
- opp.T_Status__c='Not Started';
- opp.CloseDate = system.today();
- insert opp;
- Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
- req.setComments('Submitting request for approval.');
- req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
- req.setObjectId(opp.Id);
- Approval.ProcessResult resu = Approval.process(req);
- // Verify the result
- System.assert(resu.isSuccess());
- System.assertEquals('Pending', resu.getInstanceStatus(),'Instance Status'+resu.getInstanceStatus());
- system.debug('status for approval::'+resu.getInstanceStatus());
- List<Id> newWorkItemIds = resu.getNewWorkitemIds();
- system.debug('newWorkItemIds ::'+newWorkItemIds );
- Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
- req2.setComments('Approving request.');
- req2.setAction('Approve');
- req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
- system.debug('req2::'+req2);
- // Use the ID from the newly created item to specify the item to be worked
- req2.setWorkitemId(newWorkItemIds.get(0));
- system.debug('req3::'+req2);
- // Submit the request for approval
- Approval.ProcessResult result2 = Approval.process(req2);
- // Verify the results
- System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
- System.assertEquals( 'Approved', result2.getInstanceStatus(),'Instance Status'+result2.getInstanceStatus());
- ApexPages.currentPage().getParameters().put('id',opp.id);
- ApexPages.StandardController sc = new ApexPages.standardController(opp);
- T_ApprovalUnderConditionClass rp = new T_ApprovalUnderConditionClass(sc);
- rp.appRej('Approve');
- rp.approveAll();
- rp.ApproveUnderCon();
- rp.getProsInsts();
- rp.rejectAll();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement