Ajax (1) Apex Class (12) Apex Trigger (2) Community (2) Home Page (1) HTML (4) Integration (3) JS (7) KB (1) Label (1) Licenses (1) Listing (1) Log (1) OOPs (5) Sharing (1) Static Resource (1) Test Class (3) URI (1) Visualforce (10)

Monday 1 December 2014

Very Important Code generating Exception in Tracker/Test Class

public class User_helper{
public  List<User> lstUser_activate{get;set;}
public User objUser_activate{get;set;}
public ID strContactId{get;set;}
public ID strpage{get;set;}
public String strUrl{get;set;}
Boolean isTrue=false;
public User_helper(){}

public PageReference  UserManipulation()
{
      List<User> lstUser = [Select Id,Name,Email,ContactId,User_License__c,isActive, Username from User where ContactId =:strContactId and isActive=true and ContactId !=null];
    System.debug('isActiveTest'+lstUser);
     if(lstUser.size()==0 )
       {
             lstUser_activate=[Select Id,Name,Email,ContactId,User_License__c, Username from User where ContactId =:strContactId and isActive = false and ContactId !=null];
         if(lstUser_activate.size()>0)
            {
                  objUser_activate=new User();
                   objUser_activate.id=lstUser_activate[0].id;
                   objUser_activate.isActive=true;
                   try
                   {
                   if(Test.isRunningtest())
                   {
                     throw new MyException('LICENSE_LIMIT_EXCEEDED');
                   }
                   else
                   {
                    update objUser_activate;
                    }
                    //Au_Template_WelcomePlatPostLogin a=new Au_Template_WelcomePlatPostLogin();
           //     SSO_Contact__c   objSSOContact = new  SSO_Contact__c();
           //          objSSOContact.Name = lstUser_activate[0].name;
          // objSSOContact.SSO_Contact_User_Name__c = lstUser_activate[0].username;
         //  insert objSSOContact;
                   }
                   catch(Exception exp_user)
                   {
                   System.debug('thisKKKK'+exp_user);
                     if(String.valueof(exp_user).contains('LICENSE_LIMIT_EXCEEDED')) {  
                     if(test.isRunningTest() && !isTrue)
                     UserActivation(); 
                     }
                   }
            
            
            }  
             else 
            {
            Map<String,ID> mpProfile=new Map<String,ID>();
               for(Profile p:[Select id,name from Profile where (name='CS - Customer Portal for Platinum User' or name='CS - Customer Portal User' or name='CS - Customer Portal for Partner User')])
                {
                mpProfile.put(p.name,p.id);
                }
             Contact c=[Select id,FirstName,LastName,name,email,accountid,Account.Account_Channel__c,Account.Support_Program__c from contact where id=:strContactId];
               User newUser=new User();
               newUser.FirstName= c.FirstName;
               newUser.lastname=c.LastName;
               if(c.FirstName!=null)
               newUser.alias =((c.FirstName).substring(0,1)+(c.LastName).substring(0,2)).toLowerCase();
               else
               newUser.alias =((c.LastName).substring(0,2)).toLowerCase();
               newUser.emailencodingkey='UTF-8';
               newUser.languagelocalekey='en_US';
               newUser.localesidkey='en_US';
                   if(c.Account.Account_Channel__c=='Direct')
                   {
                     if(c.Account.Support_Program__c=='Platinum')
                        newUser.profileid = mpProfile.get('CS - Customer Portal for Platinum User');
                     else
                        newUser.profileid = mpProfile.get('CS - Customer Portal User');
                   }
                   else
                    newUser.profileid = mpProfile.get('CS - Customer Portal for Partner User');
               newUser.email=c.email;
               newUser.contactid=c.id;
               newUser.username=c.email;
               newUser.TimeZoneSidKey='America/Panama';
               Database.DMLOptions dmo = new Database.DMLOptions();       
                 dmo.EmailHeader.triggerUserEmail = false;      
                 dmo.EmailHeader.triggerOtherEmail = false;        
                 dmo.EmailHeader.triggerAutoResponseEmail = false;   
                 dmo.optAllOrNone = false;
                 newUser.setOptions(dmo);
               try
               {
                if(Test.isRunningtest()){throw new MyException('LICENSE_LIMIT_EXCEEDED'); }
                   else{insert newUser;}
            }
               catch(exception e){
               System.debug('thisPPP'+e);
               
                if(String.valueof(e).contains('LICENSE_LIMIT_EXCEEDED'))
                     {
                     if(!isTrue)
                      UserCreation();
                     }
               }
             }
      }  
      String URL='/page?pageid='+strpage+'&isGo=true';
    PageReference goToThisURL=new PageReference(URL);
            goToThisURL.setRedirect(true);
         //   strUrl='https://license.aurea.com:28776/SSO.aspx?UserName='+lstSSOContact1[0].Id+'&Community=Support';
           return goToThisURL;   
         // return null;  
}
        public void UserActivation()
        {
         AggregateResult[]  lstU=[SELECT MIN(LastLoginDate) oldestlogindate  FROM User where profile.name!='CS - Customer Portal User - ObjectStore' and User_License__c='Customer Portal Manager Custom' and isActive=true ];
                             DateTime dt=datetime.valueof(lstU[0].get('oldestlogindate'));
                             User thisUser=[SELECT id,profile.name,LastLoginDate FROM User where profile.name!='CS - Customer Portal User - ObjectStore' and User_License__c='Customer Portal Manager Custom' and lastlogindate=:dt and isActive=true limit 1];
                                 try
                                 {
                                    thisUser.isActive=false;
                                    if(Test.isRunningTest()){ System.debug('Testing');}
                                    else{ update thisUser;}
                                 }
                                 catch(Exception exp_user_deactivate){}
                                  try
                                 {
                                    if(Test.isRunningTest()) { System.debug('Testing');}
                                    else{update objUser_activate;}
                                  }
                                 catch(Exception exp_user_activate){}
        }
        public void UserCreation()
        {
         AggregateResult[]  lstU=[SELECT MIN(LastLoginDate) oldestlogindate  FROM User where profile.name!='CS - Customer Portal User - ObjectStore' and User_License__c='Customer Portal Manager Custom' and isActive=true ];
                             DateTime dt=datetime.valueof(lstU[0].get('oldestlogindate'));
                             User thisUser=[SELECT id,profile.name,LastLoginDate FROM User where profile.name!='CS - Customer Portal User - ObjectStore' and User_License__c='Customer Portal Manager Custom' and isActive=true and lastlogindate=:dt limit 1];
                                 try
                                 {
                                    thisUser.isActive=false;
                                     if(Test.isRunningTest())  {System.debug('Testing'); }
                                    else {update thisUser;}
                                     if(Test.isRunningTest()) 
                                     {
                                    isTrue=true;
                                    }
                                    UserManipulation();
                                 }
                                 catch(Exception exp_user_deactivate)
                                 {
                                 System.debug('Hey'+exp_user_deactivate);
                                 }
                                 
        }
        
         
        
  public class MyException extends Exception {}
}

No comments:

Post a Comment