1

I created a very simple method on a before insert trigger, that adds the number 11 to a value if a regular user creates a record, But it doesn't add anything if the service user creates it.

So for example, if I create a record and the Internal_Id__c is 100 the result should be 111. If the service user create the record with the same value, it should stay 100.

private static void changeInternalId() {
    List < Response__c > survList = [SELECT Id, createdBy.Id, createdBy.Name, Internal_Id__c FROM Response__c WHERE Id in: Trigger.New];
    for (Response__c surv: survList) {
        if (surv.createdBy.Id != '0053i000002IMP1AAO' && surv.Internal_Id__c != null) {
            if (Test.isRunningTest()) {
                surv.Internal_Id__c = surv.Internal_Id__c + 11;
            }
        }
    }
}


//test class
Profile p = [SELECT Id FROM Profile WHERE Name = 'Test Profile'];
User regularUser = new User(Alias = 'standt', Email = '[email protected]',
    EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US',
    LocaleSidKey = 'en_US', ProfileId = p.Id,
    TimeZoneSidKey = 'America/Los_Angeles', UserName = '[email protected]');

System.runAs(regularUser) {

    Response__c surv = new Response__c();
    surv.Internal_Id__c = 100;
    insert surv;

    surv = [SELECT Id, Internal_Id__c, CreatedBy.Id, CreatedBy.Name FROM Response__c WHERE Id =: surv.Id];
    System.debug(surv.CreatedBy.Id); //output: 0053B000004Cxz0QAC
    System.debug(surv.CreatedBy.Name); // output: Testing
    System.assertEquals(111, surv.Internal_Id__c); //failed!! actual value is 100 and not 111
}

User serviceUser = [SELECT Id FROM User WHERE id = '0053i000002IMP1AAO'];
System.runAs(serviceUser) {

    Response__c surv = new Response__c();
    surv.Internal_Id__c = 120;
    insert surv;

    surv = [SELECT Id, Internal_Id__c, CreatedBy.Id, CreatedBy.Name FROM Response__c WHERE Id =: surv.Id];
    System.debug(surv.CreatedBy.Id); //output: 0053B000004CxkvQAC
    System.debug(surv.CreatedBy.Name);//output: service user
    System.assertEquals(120, surv.Internal_Id__c); //works!! value remains 120
}
1
  • xxx.createdBy is an ID field and no need to get ID field from parent relationship
    – cropredy
    Oct 10, 2020 at 23:31

2 Answers 2

2
+50

Based on you last comment (after following the steps mentioned in answer by @manjit5190), I'm assuming that this last assertion statement is failing now: System.assertEquals(120, surv.Internal_Id__c);. If that's not the case, then update your question with latest snippets.

The problem lies with the following code: surv.createdBy.Id != '0053i000002IMP1AAO' [I guess you must have changed this to surv.createdById != '0053i000002IMP1AAO' now, where surv is an entry in Trigger.new]

In before triggers, system fields (ID, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, and SystemModStamp) will not be available and accessing their values will always return null. So null != '0053i000002IMP1AAO' will always evaluate to true in the apex method and the code inside the IF loop will always execute.

Since, you are interested in the user who is creating the record (in other words, current user), you should be using the following instead: UserInfo.getUserId() != '0053i000002IMP1AAO'

1
  • Thank you for your detailed response, it worked perfectly.
    – Json
    Oct 10, 2020 at 21:38
1

It looks like not the user or the test case issue but an issue with the code. You are querying the records from Database and updating the values there instead of manipulating the records in trigger which are going through save operation.

I am not sure when you method changeInternalId() is called but likely it is being called in before insert.

If it called in before insert ,then the list in the query will be empty because IDs are not generated yet and the for loop is not transvered.

As a fix you need simple iterate over Trigger.new

3
  • Thanks @manjit5190, you're right it is called from a before insert trigger but unfortunately looping over Trigger.New didn't resolve the error.
    – Json
    Oct 6, 2020 at 21:52
  • That would be likely because you are trying to use the parent object field which would be null. So you should also change the surv.createdBy.Id to surv.createdById in line 4 of changeInternalId method.
    – manjit5190
    Oct 6, 2020 at 22:09
  • Ok I'm looping over Trigger.New and using surv.createdById but now the error moved to the service user instead of the regularUser
    – Json
    Oct 6, 2020 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .