Recently I wrote a trigger to auto populate Sampling_c off of my User Table which looks at RSM_c of the current user. And I can't logically think of how to create a trigger of a "artificial user". This following is my trigger, can anyone take a look at it and help me with the logic for my test class?

trigger SampleRVBAutoPop on Sampling__c(before insert, after insert) {
//Generating User data.
User userObj = [Select u.UserType, u.UserRoleId, u.LastName, u.Id, u.RSM__c, u.FirstName, u.ISR__c, UserRole.Name, UserRole.ParentRoleId From User u
    where u.id = : userinfo.getUserId()

Map < Id, Profile > profileMap = new Map < Id, Profile > ([select id, name from profile where name in ('Rep Partner', 'Disti Partner')]);

List < User > lstParentUserObj = new List < User > ([Select u.UserType, u.UserRoleId, u.LastName, u.Id, u.FirstName, UserRole.Name,
    UserRole.ParentRoleId From User u where u.UserRoleId = : userObj.UserRole.ParentRoleId

if (trigger.isAfter) {
    List < Sampling__c > lstUpdateSamp = new List < Sampling__c > ();
    for (Sampling__c s: Trigger.new) {
        Sampling__c sObj = new Sampling__c(Id = s.id, RSM__c = s.RSM__c);
        if (!profileMap.containsKey(userinfo.getProfileId())) {
            if (s.RSM__c == null && userObj.RSM__c != null) {
                system.debug('@@@@afterrsm' + s.Responsible_for_EVB_Build__c);
                s.RSM__c = userObj.RSM__c;
                system.debug('@@@@afterrsm' + s.RSM__c);
            } else {
                if (lstParentUserObj.size() > 0) {
                    User parentUserObj = lstParentUserObj.get(0);
    try {
        update lstUpdateSamp;
    } catch (Exception e) {
        System.debug('Error in updating Sampling' + e);
if (trigger.isBefore) {
    if (!profileMap.containsKey(userinfo.getProfileId())) {
        for (integer i = 0; i < Trigger.new.size(); i++) {
            if (Trigger.new[i].Responsible_for_EVB_Build__c == null && userObj.RSM__c != null) {
                Trigger.new[i].Responsible_for_EVB_Build__c = userObj.RSM__c;
                system.debug('@@@@rsm' + Trigger.new[i].RSM__c);

This is what I have my test class, but I believe this isn't even on the right track. Thanks for your help.

 User u = new User(RSM__c = 'A User',
    LastName = 'smtc-integration',
    Alias = 'smtc-int',
    Email ='sfdcintegration@semtech.com',
    Username = 'sfdcintegration',
    CommunityNickname = 'smtc-integration',   
    UserRoleId = 'Executive',
    EmailEncodingKey = 'Unicode (UTF-8)');

    insert u;

    Sampling__c s = new Sampling__c(Quantity_of_Samples__c = 2,
    //s.Date_Sample_Needed__c = (2014,3,18),
    Part__c = 'TEST Part,
    Override__c = 'No',
    Country__c = 'US',
    State_Province__c = 'CA',
    Zip_Postal_Code__c = '15547', 
    Contact__c = 'Boop Betty');

    insert s;

   try {
insert S;
} catch (DmlException e) {
system.assertEquals(true, e.getMessage().contains('my expected error'));

As your trigger logic depends on UserInfo.getUserId() your test needs to be able to control that User Id (perhaps what you are referring to as the "artificial user") and can through this System.runAs mechanism:

insert u;
Sampling__c s = new Sampling__c(...);
System.runAs(u) {
    insert s;
Sampling__c actual = [select ... from Sampling__c where Id = :s.Id];
System.assertEquals('...', actual.RSM__c);
// Other asserts

Your trigger appears to do several things, so using several separate test methods will probably work best. Look through the various branches the trigger logic takes, and make sure to setup and then assert each case.

(One way to avoid code duplication between test methods is to create an inner class that has methods that perform the common logic. But probably best to move on to that only once you have a couple of test methods working.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.