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
case commentscase comments 

test class help

In my Contact object, I have a lookup field to the User table.  Every user has a corresponding contact record.

I have a trigger, that pulls in the Contact ID of the current User and passes that information to the Case Contact field.

 

The trigger works, as I'm able to create cases with this information being populated correctly.  I'm having problems figuring out how to write the test code as the system user wouldn't have a contact or user to match up against.

    sObject s = [SELECT ID FROM CONTACT WHERE user__c =: Userinfo.getUserId()];
    ID myid = s.id;

 

            if((caseObj.Type == 'Drupal Site Launch' || caseObj.Type == 'Classic Site Launch') && caseObj.ParentId == NULL){
                Case JDLaunchCase = new Case();
                JDLaunchCase.Subject = caseObj.Subject + ' JD Launch Work';
                JDLaunchCase.RecordTypeId = '012A000000177IL';
                JDLaunchCase.ParentId = caseObj.ID;
                JDLaunchCase.Type = caseObj.Type;
                JDLaunchCase.Request_Type__c = 'Request New Development';
                JDLaunchCase.Requested_Deliver_Date__c = caseObj.Estimated_Launch_Date__c;
                JDLaunchCase.ContactId = myid;
                relatedCases.add(JDLaunchCase);

 

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97

use the runAs() method to run as a specific user:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm

 

or create a contact in the test class, as it is a test class DML's will not be comitted to the database.