1

I have this bizarre problem. I could create a Contact Record from UI as well as from Anonymous window in Dev Console. However, when I try to do the same from within the test class, it throws an exception (null pointer) from one of the managed package trigger. I don't have any control over the managed package code and therefore I can't debug that error.

I have tried creating a contact record using two methods 1) Using Contact c = new Contact() 2) Loading exported data in CSV by using Test.loadData().

Things I have tried: 1. Using Test.startTest() and Test.stopTest() 2. Used System.runAs(loggedInUser) who is system admin

The number of field's I'm assigning values to (after creating a raw object) doesn't seem to matter since in anonymous window and UI, I can create record using only the lastName field (e.g. insert(new Contact(lastName='Blah')

I don't want to use SeeAllData=True as it would be worst. Any help is greatly appreciated.

Here is the Test Class code:

 @isTest

 public class ContactTriggerTest {

     static testMethod void testMarkForColleagueExport(){

        Contact testContact         = new Contact();
        testContact.FirstName       = 'ContactFirstName';
        testContact.LastName        ='ContactLastName';
        testContact.Email           = 'Adress2@Adress.com';
        testContact.Title           = 'ContactTitile';
        testContact.MailingStreet   ='456 garrik st';
        testContact.TargetX_SRMb__Source__c = 'MK-Ft on Cmps-ExternalEvent';
        testContact.TargetX_SRMb__Anticipated_Start_Term__c = 'Fall';
        testContact.TargetX_SRMb__Anticipated_Start_Year__c = 2015;
        testContact.TargetX_SRSTSK__mostRecentSource__c = 'MK-RaisersEdge';
        testContact.TargetX_SRSTSK__Most_Recent_Source_Date__c = System.today();
        testContact.TargetX_SRMb__Source__c='MK-Partners'; 

        insert testContact;

     }
 }

I have also tried:

 static testMethod void testContactTrigger(){

    Contact c1 = new Contact(LastName='Desai');
    insert c1;
}

From Anonymous window I have tried simply:

 insert(new Contact(LastName='Desai'));

Here is the debug log:

Debug Log:

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 10:01:43.094 (10094089854)|EXECUTION_STARTED 10:01:43.094 (10094132830)|CODE_UNIT_STARTED|[EXTERNAL]|01p560000008UsB|ContactTriggerTest.testMarkForColleagueExport 10:01:43.094 (10094669003)|METHOD_ENTRY|[8]|01p560000008UsB|ContactTriggerTest.ContactTriggerTest() 10:01:43.094 (10094787177)|SYSTEM_METHOD_ENTRY|[10]|System.UserInfo.getUserId() 10:01:43.094 (10094909166)|SYSTEM_METHOD_EXIT|[10]|System.UserInfo.getUserId() 10:01:43.095 (10095083838)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|SELECT Id FROM User WHERE Id = :tmpVar1 10:01:43.106 (10106809780)|SOQL_EXECUTE_END|[10]|Rows:1 10:01:43.107 (10107180973)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|SELECT id FROM RecordType WHERE DeveloperName = 'Recruitment_Manager_Student' LIMIT 1 10:01:43.128 (10128060745)|SOQL_EXECUTE_END|[11]|Rows:1 10:01:43.128 (10128276560)|METHOD_EXIT|[8]|ContactTriggerTest 10:01:43.178 (10178396637)|SYSTEM_METHOD_ENTRY|[31]|System.today() 10:01:43.178 (10178475968)|SYSTEM_METHOD_EXIT|[31]|System.today() 10:01:43.178 (10178623776)|DML_BEGIN|[33]|Op:Insert|Type:Contact|Rows:1 10:01:43.634 (10634858735)|ENTERING_MANAGED_PKG|TargetX_SRMb 10:01:43.648 (10648088748)|ENTERING_MANAGED_PKG|TargetX_SRMb 10:01:43.657 (10657748950)|ENTERING_MANAGED_PKG|TargetX_SRMb 10:01:43.700 (10700758226)|ENTERING_MANAGED_PKG|TargetX_SRSTSK 10:01:43.704 (10704400012)|DML_END|[33] 10:01:43.704 (10704486889)|EXCEPTION_THROWN|[33]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TargetX_SRSTSK.sourceToTask: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

(TargetX_SRSTSK): [] 10:01:43.741 (10741390204)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TargetX_SRSTSK.sourceToTask: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

(TargetX_SRSTSK): []

Class.ContactTriggerTest.testMarkForColleagueExport: line 33, column 1 10:01:43.741 (10741414269)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TargetX_SRSTSK.sourceToTask: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

(TargetX_SRSTSK): []

Class.ContactTriggerTest.testMarkForColleagueExport: line 33, column 1 10:01:43.741 (10741432510)|CUMULATIVE_LIMIT_USAGE 10:01:43.741 (10741432510)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 2 out of 100 Number of query rows: 2 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 2 out of 150 Number of DML rows: 2 out of 10000 Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10

10:01:43.741 (10741432510)|CUMULATIVE_LIMIT_USAGE_END

10:01:43.741 (10741467720)|CODE_UNIT_FINISHED|ContactTriggerTest.testMarkForColleagueExport 10:01:43.742 (10742886758)|EXECUTION_FINISHED

closed as off-topic by crmprogdev, Eric, Mohith Shrivastava, Chris Duncombe, Boris Bachovski Aug 9 '15 at 10:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – crmprogdev, Eric, Mohith Shrivastava, Chris Duncombe, Boris Bachovski
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Please take the time to visit the Help center and read How do I ask a good question. The more details you provide, particularly code you've written, the more likely it is that someone will respond to your question with an answer that you'll find helpful. Details from debug logs are often helpful too. – crmprogdev Aug 8 '15 at 16:44
  • If you can do it in dev console and you copy and paste the code into a class it will work the same. UNLESS you are doing other things. Are you sure you inserted the contact in the dev console? Adding your code and the exact error will help. Regardless if the code executes in the dev console and you are running a class with ONLY that code in it it will work just fine. So I suspect you are doing something different... – Eric Aug 8 '15 at 16:45
1

It looks like the managed package is doing something in its code by checking if isRunningTest() = true and that is why the difference from the dev console to the test class

Unfortunately, the package does not properly handle errors so it does not give you a meaningful message.

You will have to reach out to the package vendor and let them know of the error. Also, speak with them about what specifically is required to create a contact.

TRY

  1. Annotating your Test Method with SEEALLDATA=TRUE
  2. DO NOT change anything else

If it does not throw an error, it could be some configuration data is not there when test isolation is active and the package is not accounting for it.

Either way, if you are paying for this and do not get support, I would not use the package. Vendors need to be careful when they are modifying how the package operates during testing, in fact it is not a good practice to do one thing during test and another outside of test. Especially if they do not check for the scope of the tests being theirs or the subscribers.

This is all assumption but sounds like this could be the case

  • We have tried reaching out to the package vendor. Not sure if we are gonna get any help from them. Is there any other way out? – Aryansh04 Aug 8 '15 at 21:09
  • It does not seem so. If you can execute the code in the dev console but not in a test method then the package is doing something when a contact is created in a test context and you are not providing the necessary values or something else is going on. Maybe there is some configuration data that the TargetX app uses or something. TRY using see all data = true using the same code you posted to create a contact. If it works then, then I would guess some config data may be missing. See if TargetX has a testSetup method, or something that you need to run.... – Eric Aug 8 '15 at 21:14
  • Using SeeAllData=true worked. So your early prediction was correct. The package code has used isRunningTest()=true somewhere in their code. I wonder if using just seeAllData=true would do any harm if I'm not querying the existing data in the code. – Aryansh04 Aug 9 '15 at 18:30
  • @Aryansh04 - Best practice would be to setup all the required data. Using see all data = true will bring in records (as you have seen) that may affect your results. However, for this one trigger, it may be perfectly fine as a workaround....until they get it fixed – Eric Aug 9 '15 at 20:17

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