Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class KYCEditRedirect {
- /**
- * Variable Store the KYC
- */
- KYC__c MyKyc;
- /**
- * Variable Store the controller
- */
- ApexPages.Standardcontroller MyController;
- /**
- * Constructor
- */
- public KYCEditRedirect(ApexPages.StandardController controller){
- MyController = controller;
- MyKYC = (KYC__c)MyController.getRecord();
- }
- public PageReference redirect() {
- KYC__c KYCWithFields;
- String returl;
- String rectype;
- pageReference pr;
- //if the action is edit
- if (MyKYC.Id != null){
- KYCWithFields = [SELECT TechKYCLocked__c, RecordTypeId FROM KYC__c WHERE Id =: MyKYC.Id];
- if ((KYCWithFields != null) && (KYCWithFields.RecordTypeId != null))
- rectype = String.valueOf(KYCWithFields.RecordTypeId);
- }
- else{
- //if the action is new and the user has access to more than one record type
- if (ApexPages.CurrentPage().getParameters().get('RecordType') != null)
- rectype = ApexPages.CurrentPage().getParameters().get('RecordType');
- else{
- //if the action is new and the user has access to only onre record type. In this case the record type id is not passed in the URL
- List<String> recordTypeList = new List<string>();
- Schema.DescribeSObjectResult r = KYC__c.SObjectType.getDescribe();
- List<Schema.RecordTypeInfo> rt = r.getRecordTypeInfos();
- for (Schema.RecordTypeInfo recordtype : rt){
- if ((recordType.isAvailable()) && (recordType.Name != 'Master'))
- recordTypeList.add(String.valueOf(recordType.Name));
- }
- if (recordTypeList.size() == 1){
- List<RecordType> rectypeResult = new List<RecordType>();
- rectypeResult = [SELECT Id FROM RecordType WHERE SobjectType = 'KYC__c' and Name =: recordTypeList.get(0)];
- if (rectypeResult.size() != 0)
- rectype = recTypeResult.get(0).Id;
- }
- }
- }
- if (!String.isBlank(rectype))
- rectype = rectype.substring(0,15);
- //verify if the record is pending for approval
- if ((KYCWithFields != null) && ( KYCWithFields.TechKYCLocked__c )) {
- Apexpages.addMessage(new ApexPages.Message(Apexpages.Severity.INFO,system.Label.Label_ErrorKYCLocked));
- return null;
- }
- //redirect user to the specific page based on record type
- else{
- if (rectype == system.label.RTId_KYC_THEAM_model){
- if (MyKYC.Id != null)
- pr = new PageReference('/apex/KYCTHEAMModel?id=' + MyKYC.Id);
- else
- pr = new PageReference('/apex/KYCTHEAMModel');
- }
- else{
- if (rectype == system.label.RTId_KYC_Model){
- if (MyKYC.Id != null)
- pr = new PageReference('/apex/KYCModel?id=' + MyKYC.Id);
- else
- pr = new PageReference('/apex/KYCModel');
- }
- else{
- if (rectype == system.label.RTId_KYC_CG_model){
- if (MyKYC.Id != null)
- pr = new PageReference('/apex/KYCCGModel?id=' + MyKYC.Id);
- else
- pr = new PageReference('/apex/KYCCGModel');
- }
- else{
- if (rectype == system.label.RTId_KYC_FQ_model){
- if (MyKYC.Id != null)
- pr = new PageReference('/apex/KYCFQModel?id=' + MyKYC.Id);
- else
- pr = new PageReference('/apex/KYCFQModel');
- }
- else{
- if (MyKYC.Id != null)
- pr = new PageReference('/' + MyKYC.Id + '/e');
- else
- pr = new pageReference('/a0a/e');
- }
- }
- }
- }
- //add all parameters
- for (String param : ApexPages.CurrentPage().getParameters().keySet()){
- if (param != 'save_new')
- pr.getParameters().put(param, ApexPages.CurrentPage().getParameters().get(param));
- }
- if ((!pr.getParameters().containsKey('RecordType')) && (!String.isBlank(rectype)))
- pr.getParameters().put('RecordType', rectype);
- pr.getParameters().put('nooverride', '1');
- pr.setRedirect(true);
- system.debug('pr:' + pr);
- return pr;
- }
- }
- public pageReference back(){
- return (new PageReference('/' + MyKYC.Id)).setRedirect(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement