+ Start a Discussion
Abel PalmaAbel Palma 

Test class with id

I´m trying to make the test class of this, but I having problems, I have an error......System.StringException: Invalid id: Colaborador__c:{Id=a0Of4000003bHFxEAM}

this is my apex class
public class ActionColocarCls {
 @AuraEnabled
    public static Colaborador__c getAccountData (Id accountId){        
        Colaborador__c searchAcc = new Colaborador__c();
        searchAcc = [SELECT Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__C WHERE Id =: accountId];
        system.debug('Account: ' + searchAcc);
        return searchAcc;
    }
}

This is the test class that I´m doing 
@isTest
global class ActionColocarClsTest {
	
    
	@testSetup
    static void setup()	{
        Profile profileAdmin = [SELECT Id FROM Profile WHERE name = 'System Administrator'];
        UserRole roleAdmin=[SELECT Id FROM UserRole WHERE PortalType = 'None' Limit 1];
        User user_test=new User(UserRoleId = roleAdmin.Id, ProfileId = profileAdmin.Id,
                                Username='contactAdmin@deloitte.com',
                                Alias='Admin', Email = 'contactAdmin@deloitte.com',
                                
                                EmailEncodingKey = 'UTF-8', Firstname = 'test',
                                Lastname = 'user', languagelocalekey= 'en_US',
                                LocaleSidKey = 'en_Us', TimeZoneSidKey = 'America/Chicago');
        
        insert user_test;
        User userAdmin=[SELECT id FROM User WHERE email = 'contactAdmin@deloitte.com' LIMIT 1];
        System.runAs(userAdmin){
            
            Colaborador__c con = new Colaborador__c();               
                con.Name='Joe';   
				con.ApellidoMaterno__c='Vargas';
				con.ApellidoPaterno__c='Medina';            
                con.Empresa__c='Deloitte'; 
                con.Sector__c='Digital'; 
                con.Capability__c='SalesForce';
                con.Nivel__c='Analista';  
                
            insert con;                                                    
            Colaborador__c objCol =[SELECT id,Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__C];                 
               }
    }
     static testmethod void tesActionColocar(){
         Test.startTest();
         Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
         String accountId=String.valueOf(objCol2) ;              
         system.debug('resumen' + objCol2);
         ActionColocarCls.getAccountData(accountId);
         Colaborador__c objColaborador = [SELECT Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__c WHERE Id =:accountId];                                              
         system.debug('Taer records de colaboradores' +objColaborador);                                       
         Test.stopTest();
            }
}

 
Best Answer chosen by Abel Palma
Raj VakatiRaj Vakati
Use the below code . Below code was wrong 
Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
         String accountId=String.valueOf(objCol2.Id) ;


 
@isTest
global class ActionColocarClsTest {
	
    
	@testSetup
    static void setup()	{
        Profile profileAdmin = [SELECT Id FROM Profile WHERE name = 'System Administrator'];
        UserRole roleAdmin=[SELECT Id FROM UserRole WHERE PortalType = 'None' Limit 1];
        User user_test=new User(UserRoleId = roleAdmin.Id, ProfileId = profileAdmin.Id,
                                Username='contactAdmin@deloitte.com',
                                Alias='Admin', Email = 'contactAdmin@deloitte.com',
                                
                                EmailEncodingKey = 'UTF-8', Firstname = 'test',
                                Lastname = 'user', languagelocalekey= 'en_US',
                                LocaleSidKey = 'en_Us', TimeZoneSidKey = 'America/Chicago');
        
        insert user_test;
        User userAdmin=[SELECT id FROM User WHERE email = 'contactAdmin@deloitte.com' LIMIT 1];
        System.runAs(userAdmin){
            
            Colaborador__c con = new Colaborador__c();               
                con.Name='Joe';   
				con.ApellidoMaterno__c='Vargas';
				con.ApellidoPaterno__c='Medina';            
                con.Empresa__c='Deloitte'; 
                con.Sector__c='Digital'; 
                con.Capability__c='SalesForce';
                con.Nivel__c='Analista';  
                
            insert con;                                                    
            Colaborador__c objCol =[SELECT id,Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__C];                 
               }
    }
     static testmethod void tesActionColocar(){
         Test.startTest();
         Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
         String accountId=String.valueOf(objCol2.Id) ;              
         system.debug('resumen' + objCol2);
         ActionColocarCls.getAccountData(accountId);
         Colaborador__c objColaborador = [SELECT Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__c WHERE Id =:accountId];                                              
         system.debug('Taer records de colaboradores' +objColaborador);                                       
         Test.stopTest();
            }
}

 

All Answers

Raj VakatiRaj Vakati
Use the below code . Below code was wrong 
Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
         String accountId=String.valueOf(objCol2.Id) ;


 
@isTest
global class ActionColocarClsTest {
	
    
	@testSetup
    static void setup()	{
        Profile profileAdmin = [SELECT Id FROM Profile WHERE name = 'System Administrator'];
        UserRole roleAdmin=[SELECT Id FROM UserRole WHERE PortalType = 'None' Limit 1];
        User user_test=new User(UserRoleId = roleAdmin.Id, ProfileId = profileAdmin.Id,
                                Username='contactAdmin@deloitte.com',
                                Alias='Admin', Email = 'contactAdmin@deloitte.com',
                                
                                EmailEncodingKey = 'UTF-8', Firstname = 'test',
                                Lastname = 'user', languagelocalekey= 'en_US',
                                LocaleSidKey = 'en_Us', TimeZoneSidKey = 'America/Chicago');
        
        insert user_test;
        User userAdmin=[SELECT id FROM User WHERE email = 'contactAdmin@deloitte.com' LIMIT 1];
        System.runAs(userAdmin){
            
            Colaborador__c con = new Colaborador__c();               
                con.Name='Joe';   
				con.ApellidoMaterno__c='Vargas';
				con.ApellidoPaterno__c='Medina';            
                con.Empresa__c='Deloitte'; 
                con.Sector__c='Digital'; 
                con.Capability__c='SalesForce';
                con.Nivel__c='Analista';  
                
            insert con;                                                    
            Colaborador__c objCol =[SELECT id,Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__C];                 
               }
    }
     static testmethod void tesActionColocar(){
         Test.startTest();
         Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
         String accountId=String.valueOf(objCol2.Id) ;              
         system.debug('resumen' + objCol2);
         ActionColocarCls.getAccountData(accountId);
         Colaborador__c objColaborador = [SELECT Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__c WHERE Id =:accountId];                                              
         system.debug('Taer records de colaboradores' +objColaborador);                                       
         Test.stopTest();
            }
}

 
This was selected as the best answer
Steven NsubugaSteven Nsubuga
I suggest you remove line 37 and then change line 39 to
ActionColocarCls.getAccountData(objCol2.id);

The test code becomes
@isTest
global class ActionColocarClsTest {
	
    
	@testSetup
    static void setup()	{
        Profile profileAdmin = [SELECT Id FROM Profile WHERE name = 'System Administrator'];
        UserRole roleAdmin=[SELECT Id FROM UserRole WHERE PortalType = 'None' Limit 1];
        User user_test=new User(UserRoleId = roleAdmin.Id, ProfileId = profileAdmin.Id,
                                Username='contactAdmin@deloitte.com',
                                Alias='Admin', Email = 'contactAdmin@deloitte.com',
                                
                                EmailEncodingKey = 'UTF-8', Firstname = 'test',
                                Lastname = 'user', languagelocalekey= 'en_US',
                                LocaleSidKey = 'en_Us', TimeZoneSidKey = 'America/Chicago');
        
        insert user_test;
        User userAdmin=[SELECT id FROM User WHERE email = 'contactAdmin@deloitte.com' LIMIT 1];
        System.runAs(userAdmin){
            
            Colaborador__c con = new Colaborador__c();               
                con.Name='Joe';   
				con.ApellidoMaterno__c='Vargas';
				con.ApellidoPaterno__c='Medina';            
                con.Empresa__c='Deloitte'; 
                con.Sector__c='Digital'; 
                con.Capability__c='SalesForce';
                con.Nivel__c='Analista';  
                
            insert con;                                                    
            Colaborador__c objCol =[SELECT id,Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__C];                 
               }
    }
     static testmethod void tesActionColocar(){
         Test.startTest();
         Colaborador__c objCol2 = [SELECT id FROM Colaborador__C LIMIT 1];         
                       
         system.debug('resumen' + objCol2);
         ActionColocarCls.getAccountData(objCol2.Id);
         Colaborador__c objColaborador = [SELECT Name, Empresa__c, Sector__c, Capability__c,Nivel__c FROM Colaborador__c WHERE Id =:objCol2.Id];                                              
         system.debug('Taer records de colaboradores' +objColaborador);                                       
         Test.stopTest();
            }
}