function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sunil Kumar 545Sunil Kumar 545 

using test class...this code have to implement in @isTest ..how to do can anyone help this code?

@RestResource(urlMapping='/PortalUser/*')
Global Class rolesException extends Exception {
    @HttpPost   
    global static String CreateNewUser()  
        {             
     
         RestRequest req = RestContext.request;    
         System.debug('RestRequest___: '+ req);     
         RestResponse res = RestContext.response;   
         System.debug('RestResponse___: '+ res);    
         Blob b = req.Requestbody;                 
         Wrapperclss reg = rolesException.parse(b.tostring());
            Savepoint sp = Database.setSavepoint();

         Try{    
               if(reg.firstname==null || reg.firstname=='')
                {
                    throw new rolesException ('enter firstname');
                }
                if(reg.lastname==null || reg.lastname=='')
                {
                    throw new rolesException ('enter lastname');        
                }
                //if(Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}',reg.Email))
                //{                        
                  //     throw new rolesException ('Please enter valid emailid');
                //} 
                
                 if(reg.role==null || reg.role=='')
                 {
                    throw new rolesException('role is required');  
                 }        
                 /*list<contact> sr =[SELECT  lastName, Role__c from Contact where  Role__c=:reg.Role];
                 system.debug('enter@@@'+sr);
                 if(sr.size()<=0)
                 {
                    throw new rolespostportalException('role is requiredee');        
                 }*/
                 
                 list<Role__c>role=[select Name,Type__c from Role__c where type__c =:reg.Role];
                 system.debug('Enter__'+role);
                 if(role[0].type__c == 'Manager')
               {
                   List<Contact> con = [SELECT Id, Name FROM Contact WHERE accountId=:reg.AccountID AND Role__c=:role[0].Id];
                   if(con.size()>0)

                   {
                       throw new rolesException('Manager already exist');
                   }
               }
               if(reg.username!=null || reg.username!='')

                   {

                    List<User> u = [SELECT username FROM User WHERE username =:reg.username];

                    if(u.size()>0)

                   {

                       throw new rolesException('User already exist');
     }

                    }
            
            Contact C1 = new Contact( AccountID =reg.AccountID, firstName = reg.Firstname, lastName =reg.lastname ,email =reg.Email, role__c = role[0].id);
            insert C1;
                User newUser = new User();           
                newUser.Username =reg.username;     
                newUser.LastName = reg.lastname;
                newUser.Firstname = reg.Firstname;
                newUser.Email = reg.Email;      
                newUser.ContactId =C1.id ;
                newUser.Alias =reg.Alias;
                newUser.TimeZoneSidKey = reg.TimeZoneSidKey;
                newUser.LocaleSidKey = reg.LocaleSidKey;
                newUser.EmailEncodingKey = reg.EmailEncodingKey;
                newUser.LanguageLocaleKey = reg.LanguageLocaleKey;
                newUser.CommunityNickname= reg.CommunityNickname;
                newUser.ProfileId= reg.ProfileId;         
                insert newUser;        
                return 'User Created! Id Is: ' + newUser.Id; 
            }
               catch(Exception e)
               {
                    Database.rollback( sp );
                    system.debug('error____:enter valid ID'+e);
                    return e.getMessage();
               }
        }
               global static Wrapperclss parse(string reg) 
               {       
                   return(Wrapperclss)System.json.deserialize(reg,Wrapperclss.class);  
               }   
               global class Wrapperclss  
                {      
                    global String username{get;set;} 
                    global String Firstname{get;set;} 
                    global String LastName{get;set;}     
                    global String Email{get;set;}    
                    global String ContactId{get;set;}   
                    global String Role{get;set;}
                    global String Alias{get;set;}   
                    global String TimeZoneSidKey{get;set;}   
                    global String EmailEncodingKey{get;set;} 
                    global String LanguageLocaleKey{get;set;}
                    global String LocaleSidKey{get;set;}
                    global String CommunityNickname{get;set;}
                    global String ProfileId{get;set;}   
                    global String AccountID{get;set;}   

                }    
          
    }
cvuyyurucvuyyuru

Hi Sunil,
I appreciate if you can try developing test class and post the errors rather than posting the whole apex class :)

 

Sunil Kumar 545Sunil Kumar 545
I need test code for this requirement ..i unable to do this...can anyone help for requirement ...have to write in test class ..