Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. public with sharing class KYCEditRedirect {
  2.  
  3.   /**
  4.   * Variable Store the KYC
  5.   */
  6.   KYC__c MyKyc;
  7.  
  8.   /**
  9.   * Variable Store the controller
  10.   */
  11.   ApexPages.Standardcontroller MyController;
  12.  
  13.   /**
  14.     *  Constructor
  15.     */  
  16.   public KYCEditRedirect(ApexPages.StandardController controller){
  17.    
  18.     MyController = controller;
  19.     MyKYC = (KYC__c)MyController.getRecord();
  20.  
  21.   }
  22.    
  23.     public PageReference redirect() {
  24.      
  25.       KYC__c KYCWithFields;  
  26.       String returl;
  27.       String rectype;
  28.      
  29.       pageReference pr;
  30.      
  31.       //if the action is edit
  32.       if (MyKYC.Id != null){
  33.         KYCWithFields = [SELECT TechKYCLocked__c, RecordTypeId  FROM KYC__c WHERE Id =: MyKYC.Id];
  34.         if ((KYCWithFields != null) && (KYCWithFields.RecordTypeId != null))
  35.           rectype = String.valueOf(KYCWithFields.RecordTypeId);
  36.       }
  37.       else{
  38.         //if the action is new and the user has access to more than one record type
  39.         if (ApexPages.CurrentPage().getParameters().get('RecordType') != null)
  40.           rectype = ApexPages.CurrentPage().getParameters().get('RecordType');
  41.         else{
  42.           //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
  43.           List<String> recordTypeList = new List<string>();
  44.           Schema.DescribeSObjectResult r = KYC__c.SObjectType.getDescribe();
  45.           List<Schema.RecordTypeInfo> rt = r.getRecordTypeInfos();
  46.           for (Schema.RecordTypeInfo recordtype : rt){
  47.             if ((recordType.isAvailable()) && (recordType.Name != 'Master'))
  48.               recordTypeList.add(String.valueOf(recordType.Name));
  49.           }
  50.           if (recordTypeList.size() == 1){
  51.             List<RecordType> rectypeResult = new List<RecordType>();
  52.             rectypeResult = [SELECT Id FROM RecordType WHERE SobjectType = 'KYC__c' and Name =: recordTypeList.get(0)];
  53.             if (rectypeResult.size() != 0)
  54.               rectype = recTypeResult.get(0).Id;
  55.            
  56.           }
  57.         }
  58.        
  59.       }
  60.      
  61.       if (!String.isBlank(rectype))
  62.         rectype = rectype.substring(0,15);
  63.      
  64.       //verify if the record is pending for approval
  65.       if ((KYCWithFields != null) && ( KYCWithFields.TechKYCLocked__c )) {
  66.             Apexpages.addMessage(new ApexPages.Message(Apexpages.Severity.INFO,system.Label.Label_ErrorKYCLocked));
  67.             return null;
  68.       }
  69.       //redirect user to the specific page based on record type
  70.       else{
  71.         if (rectype == system.label.RTId_KYC_THEAM_model){
  72.           if (MyKYC.Id != null)
  73.             pr = new PageReference('/apex/KYCTHEAMModel?id=' + MyKYC.Id);
  74.           else
  75.             pr = new PageReference('/apex/KYCTHEAMModel');
  76.         }
  77.         else{
  78.           if (rectype == system.label.RTId_KYC_Model){
  79.             if (MyKYC.Id != null)
  80.               pr = new PageReference('/apex/KYCModel?id=' + MyKYC.Id);
  81.             else
  82.               pr = new PageReference('/apex/KYCModel');
  83.           }
  84.           else{
  85.             if (rectype == system.label.RTId_KYC_CG_model){
  86.               if (MyKYC.Id != null)  
  87.                 pr = new PageReference('/apex/KYCCGModel?id=' + MyKYC.Id);
  88.               else
  89.                 pr = new PageReference('/apex/KYCCGModel');
  90.             }
  91.             else{
  92.               if (rectype == system.label.RTId_KYC_FQ_model){
  93.                 if (MyKYC.Id != null)
  94.                   pr = new PageReference('/apex/KYCFQModel?id=' + MyKYC.Id);
  95.                 else
  96.                   pr = new PageReference('/apex/KYCFQModel');
  97.               }
  98.               else{
  99.                 if (MyKYC.Id != null)
  100.                   pr = new PageReference('/' + MyKYC.Id + '/e');
  101.                 else
  102.                   pr = new pageReference('/a0a/e');
  103.               }
  104.             }
  105.           }
  106.         }
  107.        
  108.         //add all parameters  
  109.         for (String param : ApexPages.CurrentPage().getParameters().keySet()){
  110.           if (param != 'save_new')
  111.             pr.getParameters().put(param, ApexPages.CurrentPage().getParameters().get(param));
  112.         }
  113.         if ((!pr.getParameters().containsKey('RecordType')) && (!String.isBlank(rectype)))
  114.           pr.getParameters().put('RecordType', rectype);
  115.          
  116.         pr.getParameters().put('nooverride', '1');
  117.         pr.setRedirect(true);
  118.         system.debug('pr:' + pr);  
  119.         return pr;
  120.       }
  121.        
  122.     }
  123.    
  124.     public pageReference back(){
  125.      
  126.       return  (new PageReference('/' + MyKYC.Id)).setRedirect(true);
  127.      
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement