0

I'm writing a test class on Opportunity which is testing trigger which execute on update. I've simple questions- I'm trying to print user which has created opportunity in this test class.

I'm using system.RunAs with system admin user but still code is not printing any user.This is the line in code i'm talking about

System.debug('CreatebyUSer=' + opp.createdbyID); //

My trigger basically find , if opp is created by some particular profile say system administrator then some fields need to be updated. How can I test trigger , if my test class doesn't give me user who has created the opportunity

Code is this :

@ isTest

public class OpportunityTest
{
   static opportunity opp = new Opportunity();

  static void PrePareTestData() 
  { 

   opp.Name = 'TestOpp'; 
   opp.StageName = 'Prospecting';
   opp.CloseDate = date.today();

  }

  testMethod Static void FuncOppTriggerTest()
    {
        PrePareTestData();

        Test.startTest(); 
         User u = Grp1_CreateUser.Adminuser();
         system.debug('Current User=' + u);
         system.runAs(u)
         {
         insert opp;

          opp.stageName = 'Qualification';
         update opp;

         System.debug('CreatebyUSer=' + opp.createdbyID); // This is coming as null 
                } 
        Test.stopTest();

       System.assertEquals(opp.NextStep, 'Opp to move to next step');
    }
}
0

after line , insert opp; save the id of the saved opportunity. after line, update opp;, use this id to get the opportunity by firing soql query

Opportunity justinsertedOpp = [select Id, Name, CreatedById from Opportunity where Id=: opportunityId];

then try to use system.debug on justinsertedOpp object to print createdbyid.

0

You've not created a test user in your test class that creates the opportunity, thus the reason your test class can't find that user's Id. If that's an important part of your trigger, then that's also what I'd think you'd want to be asserting as part of your test. The created by user should also be the runAs user for your testing purposes as that's the user who'll be assigned as the created by for the opportunity when you insert the record.

Also, make certain you are are providing data for all the fields required by your org for a new opp to be created. If you don't, when this hits production and validation rules are turned on, the test will fail because the record won't be inserted.

Edit

Since you're calling a utility method to get a valid user. I suggest you do something like what's below when you do your insert:

list<Id>opir = new list<Id>();

Database.SaveResult[] OppInsertRslts = Database.Insert(opp,true); 

   for(Database.SaveResult noir: OwnrNewInsrtRslts){
      if(opir.IsSuccess()){

      noirIds.add(opir.getId());

   }

You now have a list of new opp Id's created when you inserted your opportunities which you can run a query on to determine what the created by user Id is and any other properties of interest.

  • I've not mentioned earlier, but there is a line : User u = Grp1_CreateUser.Adminuser(); that actually query user from same environment. I'v used sysem.Runas(u). On using system.debug, user is properly coming via this line – curious_techy Mar 15 '15 at 2:18
  • See my updated response above. – crmprogdev Mar 15 '15 at 4:53

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.