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
StaciStaci 

Test method help with hard coding Ids

I've got a test method that I have hard coded a few Ids(Record Type, User and Account) into (I'm not very good at writing these).  I need to dynamically assign these but at a loss as to how. 

If I try to create a new user, there are fields that are required that I can't find field names for.  Accounts when we create them are big to-do's so I wouldn't even know where to begin to create that in a test method and then record types, again at a loss.  Can someone please help me?  I have google searched some examples for the user but my test run fails saying the user is invalid.

Here's my test method.  Any help would be greatly appreciated!
@isTest

private class CW_Validations_Status_UpdatesTest {
 
 
static testMethod void CWStatusValidationTest() {



Profile p = [SELECT Id FROM Profile WHERE Name='CW MineStar Support - Tier 2']; 
        
        
 User u1 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='stacitestuser@testorg.com');
            insert u1;

//creates a case record
Case cs1 = new Case();
cs1.RecordTypeId = '01230000000v4Ss';
cs1.AccountId = '00119000003QPkD';
cs1.OwnerId = 'u1';
cs1.Reason = 'Incident';
cs1.CW_Type__c = 'Product Support';
cs1.Impact__c = 'Site Production Down';
cs1.Urgency__c = 'Normal';
cs1.Status = 'Closed - Resolved';
cs1.Resolution_Detail__c = 'This case is resolved';
cs1.Defect_Type_Multi__c = 'Hardware';
cs1.Subject = 'This is my case';
cs1.Description = 'This is my description';
cs1.Incident_Start__c=Date.today();
cs1.Product__c = 'Fleet';
cs1.Application__c = '--';
cs1.Subsystem__c = 'Office';
cs1.Component__c = 'Database';
insert cs1;

cs1.Status = 'Closed - Duplicate';
cs1.Resolution_Detail__c = 'This case is a duplicate of 1234';
update cs1;

cs1.Status = 'Closed - Declined';
cs1.Resolution_Detail__c = 'This case has been declined by development';
cs1.Declined_Reason__c = 'Product is end of life';
cs1.Defect_Type_Multi__c = 'Hardware';
update cs1;

cs1.Status = 'Pending - Change';
cs1.Resolution_Detail__c = 'This case is awaiting a change';
cs1.Change_Number__c = 'aadsfaf23432';
cs1.Change_Tracking_Link__c = 'http://www.yahoo.com';
update cs1;

cs1.Status = 'Closed - No Response';
cs1.Resolution_Detail__c = 'This is closing no response';
cs1.Defect_Type_Multi__c = 'Hardware';
update cs1;


cs1.Status = 'Closed - Cancelled';
cs1.Resolution_Detail__c = 'This case is being cancelled, no longer needed';
update cs1;


cs1.Status = 'Closed - Unable to Reproduce';
cs1.Resolution_Detail__c = 'Closing case as we are not able to reproduce issue';
update cs1;


cs1.Status = 'Pending - RCA';
cs1.Workaround_ICA__c = 'This is a workaround';
update cs1;


cs1.Status = 'Pending - Dealer Validation';
cs1.Workaround_ICA__c = 'This is a workaround';
update cs1;


cs1.Status = 'Pending - Customer Validation';
cs1.Workaround_ICA__c = 'This is a workaround';
update cs1;


cs1.Status = 'Pending - NPI';
cs1.Workaround_ICA__c = 'This is a workaround';
cs1.NPI_Numbertext__c = 'fwefw2e3fr2w';
cs1.NPI_Tracking_Link__c = 'http://www.yahoo.com';
update cs1;

cs1.Status = 'Pending - CPI';
cs1.Workaround_ICA__c = 'This is a workaround';
cs1.CPI_Numbertext__c = '232r2df23';
cs1.CPI_Tracking_Link__c = 'http://www.yahoo.com';
update cs1;


cs1.Status = 'Recurring Incident';
cs1.Resolution_Detail__c = 'This is recurring incident';
cs1.ParentId = '50019000000dXO9';
update cs1;

cs1.Status = 'Pending - Evaluation';
cs1.Resolution_Detail__c = 'This is recurring incident';
update cs1;

}

static testMethod void CWUserValidationTest() {
//creates a case record, Owner Tier 2 Peoria user 
Case cs2 = new Case();
cs2.RecordTypeId = '01230000000uzis';
cs2.AccountId = '00119000003QPkD';
cs2.Reason = 'Incident';
cs2.CW_Type__c = 'Product Support';
cs2.Impact__c = 'Site Production Down';
cs2.Urgency__c = 'Normal';
cs2.Product__c = 'Fleet';
cs2.Application__c = '--';
cs2.Subsystem__c = 'Office';
cs2.Status = 'New';
cs2.Subject = 'This is my case';
cs2.Description = 'This is my description';
cs2.Incident_Start__c=Date.today();
cs2.OwnerId = 'u1';
insert cs2;

//updates owner to T3 Americas user
cs2.OwnerId = 'u2';
update cs2;

//updates owner to T2 APD user
cs2.OwnerId = 'u1';
update cs2;

//creates a case record, Owner Tier 2 queue
Case cs3 = new Case();
cs3.RecordTypeId = '01230000000uzis';
cs3.AccountId = '00119000003QPkD';
cs3.Reason = 'Incident';
cs3.CW_Type__c = 'Product Support';
cs3.Impact__c = 'Site Production Down';
cs3.Urgency__c = 'Normal';
cs3.Product__c = 'Fleet';
cs3.Application__c = '--';
cs3.Subsystem__c = 'Office';
cs3.Status = 'New';
cs3.Subject = 'This is my case';
cs3.Description = 'This is my description';
cs3.Incident_Start__c=Date.today();
cs3.OwnerId = '00G30000001oUSL';
insert cs3;

//update owner to T3 queue
cs3.OwnerId = '00G30000002jFoz';
update cs3;

//creates a case record, Owner Tier 2 queue
Case cs4 = new Case();
cs4.RecordTypeId = '01230000000uzis';
cs4.AccountId = '00119000003QPkD';
cs4.Reason = 'Incident';
cs4.CW_Type__c = 'Product Support';
cs4.Impact__c = 'Site Production Down';
cs4.Urgency__c = 'Normal';
cs4.Product__c = 'Fleet';
cs4.Application__c = '--';
cs4.Subsystem__c = 'Office';
cs4.Status = 'New';
cs4.Subject = 'This is my case';
cs4.Description = 'This is my description';
cs4.Incident_Start__c=Date.today();
cs4.OwnerId = '00G30000001oUSL';
insert cs4;

//update user to T2 user
cs4.OwnerId = 'u1';
update cs4;


//creates a case record, Owner Tier 2 user
Case cs1 = new Case();
cs1.RecordTypeId = '01230000000uzis';
cs1.AccountId = '00119000003QPkD';
cs1.Reason = 'Incident';
cs1.CW_Type__c = 'Product Support';
cs1.Impact__c = 'Site Production Down';
cs1.Urgency__c = 'Normal';
cs1.Product__c = 'Fleet';
cs1.Application__c = '--';
cs1.Subsystem__c = 'Office';
cs1.Status = 'New';
cs1.Subject = 'This is my case';
cs1.Description = 'This is my description';
cs1.Incident_Start__c=Date.today();
cs1.OwnerId = 'u1';
insert cs1;

//updates owner to T3 queue
cs1.OwnerId = '00G30000002jFoz';
update cs1;


//creates a case record, Owner is T2 queue
Case cs8 = new Case();
cs8.RecordTypeId = '01230000000uzis';
cs8.AccountId = '00119000003QPkD';
cs8.Reason = 'Incident';
cs8.CW_Type__c = 'Product Support';
cs8.Impact__c = 'Site Production Down';
cs8.Urgency__c = 'Normal';
cs8.Product__c = 'Fleet';
cs8.Application__c = '--';
cs8.Subsystem__c = 'Office';
cs8.Status = 'New';
cs8.Subject = 'This is my case';
cs8.Description = 'This is my description';
cs8.Incident_Start__c=Date.today();
cs8.OwnerId = '00G30000001oUSL';
insert cs8;

//update user to T3 user
cs8.OwnerId = '00530000004CgXz';
update cs8;


}

static testMethod void CWSubStatusUpdateTest(){
//creates a case record, T2 User is Owner
Case cs1 = new Case();
cs1.AccountId = '00119000003QPkD';
cs1.OwnerId = 'u1';
cs1.Reason = 'Incident';
cs1.CW_Type__c = 'Product Support';
cs1.Impact__c = 'Site Production Down';
cs1.Urgency__c = 'Normal';
cs1.Status = 'New';
cs1.Subject = 'This is my case';
cs1.Description = 'This is my description';
cs1.Incident_Start__c=Date.today();
cs1.Product__c = 'Fleet';
cs1.Application__c = '--';
cs1.Subsystem__c = 'Office';
insert cs1;

//change owner to Dealer user
cs1.OwnerId = '00519000000Ikmj';
update cs1;


//creates a case record, T2 queue as Owner
Case cs2 = new Case();
cs2.AccountId = '00119000003QPkD';
cs2.OwnerId = '00G30000001oUSL';
cs2.Reason = 'Incident';
cs2.CW_Type__c = 'Product Support';
cs2.Impact__c = 'Site Production Down';
cs2.Urgency__c = 'Normal';
cs2.Status = 'New';
cs2.Subject = 'This is my case';
cs2.Description = 'This is my description';
cs2.Incident_Start__c=Date.today();
cs2.Product__c = 'Fleet';
cs2.Application__c = '--';
cs2.Subsystem__c = 'Office';
insert cs2;

//change owner to T2 user
cs2.OwnerId = 'usr1';
update cs2;

//creates a case record, CW Admin queue as Owner
Case cs3 = new Case();
cs3.AccountId = '00119000003QPkD';
cs3.OwnerId = '00G30000002ljqO';
cs3.Reason = 'Incident';
cs3.CW_Type__c = 'Product Support';
cs3.Impact__c = 'Site Production Down';
cs3.Urgency__c = 'Normal';
cs3.Status = 'New';
cs3.Subject = 'This is my case';
cs3.Description = 'This is my description';
cs3.Incident_Start__c=Date.today();
cs3.Product__c = 'Fleet';
cs3.Application__c = '--';
cs3.Subsystem__c = 'Office';
insert cs3;

//changes user to CW Admin user
cs3.OwnerId = '005a0000007VK2P';
update cs3;

//creates a case record, T2 as Owner
Case cs5 = new Case();
cs5.AccountId = '00119000003QPkD';
cs5.OwnerId = 'u1';
cs5.Reason = 'Incident';
cs5.CW_Type__c = 'Product Support';
cs5.Impact__c = 'Site Production Down';
cs5.Urgency__c = 'Normal';
cs5.Status = 'New';
cs5.Subject = 'This is my case';
cs5.Description = 'This is my description';
cs5.Incident_Start__c=Date.today();
cs5.Product__c = 'Fleet';
cs5.Application__c = '--';
cs5.Subsystem__c = 'Office';
insert cs5;

//change case owner to customer user
cs5.OwnerId = '00519000000Jnwt';
update cs5;


//creates a case record, T2 user as Owner
Case cs6 = new Case();
cs6.AccountId = '00119000003QPkD';
cs6.OwnerId = 'u1';
cs6.Reason = 'Incident';
cs6.CW_Type__c = 'Product Support';
cs6.Impact__c = 'Site Production Down';
cs6.Urgency__c = 'Normal';
cs6.Status = 'New';
cs6.Subject = 'This is my case';
cs6.Description = 'This is my description';
cs6.Incident_Start__c=Date.today();
cs6.Product__c = 'Fleet';
cs6.Application__c = '--';
cs6.Subsystem__c = 'Office';
insert cs6;

//change case owner to T3 user
cs6.OwnerId = 'u2';
update cs6;

//creates a case record, T2 user as Owner
Case cs7 = new Case();
cs7.AccountId = '00119000003QPkD';
cs7.OwnerId = 'u1';
cs7.Reason = 'Incident';
cs7.CW_Type__c = 'Product Support';
cs7.Impact__c = 'Site Production Down';
cs7.Urgency__c = 'Normal';
cs7.Status = 'Closed - Resolved';
cs7.Subject = 'This is my case';
cs7.Description = 'This is my description';
cs7.Incident_Start__c=Date.today();
cs7.Product__c = 'Fleet';
cs7.Application__c = '--';
cs7.Subsystem__c = 'Office';
cs7.Component__c = 'Client';
cs7.Resolution_Detail__c = 'this is the resolution detail';
cs7.Defect_Type_Multi__c = 'Configuration';
insert cs7;

}}

 
Best Answer chosen by Staci
Suneel#8Suneel#8
Below are the SOQLs to get Queue and Record Type Ids 

Id qId=[select id,name from group where type='Queue' and name='YOUR_QUEUE_NAME'].id;

Id rtId=[select id,name from recordtype where name='RECORD_TYPE_NAME'].id;

Kindly mark this as Solved if your queries are answered

All Answers

Suneel#8Suneel#8
You can try getting user details as below

User u=[select id,alias from user where alias='YOUR_USER_ALIAS' limit 1];

I see that you are asssigning owner as cs1.OwnerId = 'u1'; Surrounding u1 with single quotes makes it a string where as u1 is an object and you can access its id like u1.id.So syntax is as below

cs1.OwnerId = u1.id;

You can use annotation @isTest(SeeAllData=true) if you want to use data that is already existing in the organization.

You can read following links for better understanding of test classes

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm
 
StaciStaci
thank you @Suneel#8  I've got the user and account part working.  I have some cases assigned to queues, how would I accomplish that?  And also the record types?
Suneel#8Suneel#8
Below are the SOQLs to get Queue and Record Type Ids 

Id qId=[select id,name from group where type='Queue' and name='YOUR_QUEUE_NAME'].id;

Id rtId=[select id,name from recordtype where name='RECORD_TYPE_NAME'].id;

Kindly mark this as Solved if your queries are answered
This was selected as the best answer
StaciStaci
Thank you so much @Suneel#8!