0

I am trying to insert a profile called "Vendor Partner Portal" for a user in my test class. When I run the tests I am getting an error message saying

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Product_Line__c: []

Stack Trace: Class.Test_salesQuotes.runasPartner: line 230, column 1

Below is my code. Can someone help me find what the issue is

    @isTest()
    public class Test_salesQuotes{
     public static  testMethod  void runasPartner() {

         Product_Line__c pl = new Product_Line__c(name = 'myPL');
      insert pl;     

     Product2 p = new product2(name='gack',family='gack',productcode='gack',Product_Line__c=pl.Id,isActive=true);
      insert p;

       Product2 p3 = new product2(name='gack',family='gack',productcode='gack',Product_Line__c=pl.Id,isActive=true);
       p3.Loanable__c=true;
      insert p3;
  Id pricebookId = Test.getStandardPricebookId();     
      try{
          insert new PricebookEntry(pricebook2id = pricebookId , product2id = p.id, 
                                unitprice=1.0, isActive=true);
      }catch(System.DMLException e){
        //Swallow exception - creating the product2 should have created a standard 
        //PricebookEntry already.
      }

        Pricebook2 pb = new pricebook2(name='test', Ext_Ref_Id__c='asldfjlakjsdf234234');
        insert pb;
        PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id, 
                                              unitprice=1.0, isActive=true);
        insert pbe;

       Opportunity o = new Opportunity(name='test', pricebook2id=pb.id,
        stageName='Open', CloseDate=Date.newInstance(2006,10,10));
        insert o;
        OpportunityLineItem oli = new OpportunityLineItem(opportunityid=o.id, 
                                pricebookentryid=pbe.id, unitprice=1.5, quantity=2);
        insert oli;

     SFDC_520_Quote__c q = new SFDC_520_Quote__c(Opportunity__c = o.id); 
    insert q;

      id VendorPQRecordType = [select Id FROM RecordType where Name = 'Primary Cells Request'].Id;

       SFDC_520_Quote__c q1 = new SFDC_520_Quote__c(Opportunity__c = o.id); 
         q1.recordtypeid=VendorPQRecordType;
     insert q1;

    PageReference pageRef = Page.AddQuoteLines;
        Test.setCurrentPage(pageRef);
      system.debug( 'quote id is '+q.id);

      ApexPages.currentPage().getParameters().put('id', q.id);

     salesQuotes s = new salesQuotes();
     ApexPages.currentPage().getParameters().put('reloadQuote','1');

     ApexPages.currentPage().getParameters().put('id', q1.id);

     salesQuotes s1 = new salesQuotes();
     ApexPages.currentPage().getParameters().put('reloadQuote','1');

     ApexPages.currentPage().getParameters().put('userTypeName', null);

     system.assertEquals('Standard',s1.UserTypeName);

        Account acct = test_standardobjectutil.createAccount('Test Account', false);
    // acct.IsPartner=true;
     insert acct;

     Contact c = test_standardobjectutil.createContact('John','Doe2', false);
     c.accountid = acct.Id;
     insert c;  

     Profile userProfile = [SELECT Id FROM Profile WHERE Name='Vendor Partner Portal']; 
     User u = new User(Alias = 't23', Email='teststandarduser@stemcell.com', 
     EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
     LocaleSidKey='en_US', ProfileId = userProfile.Id,
     ContactId = c.id,
     TimeZoneSidKey='America/Los_Angeles', UserName='teststandarduser@stemcell.com');
     insert u;
        system.runAs(u){

            ApexPages.currentPage().getParameters().put('pid', p.id);
             s.enableAddProducts();
             s.searchStr='g';
            system.debug('Test exact user:'+ u); 
            s.doSearch(); 
        }
   }
   }
  • is that my answer helps you? – Santanu Boral May 21 '17 at 19:11
1

Create a @testSetup method and create user, account and other record which can be used across methods in same test class.

@testSetup static void setup() {

     Account acct = test_standardobjectutil.createAccount('Test Account', false);
    // acct.IsPartner=true;
     insert acct;

     Contact c = test_standardobjectutil.createContact('John','Doe2', false);
     c.accountid = acct.Id;
     insert c;  

     Profile userProfile = [SELECT Id FROM Profile WHERE Name='Vendor Partner Portal']; 
     User u = new User(Alias = 't23', Email='teststandarduser@stemcell.com', 
     EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
     LocaleSidKey='en_US', ProfileId = userProfile.Id,
     ContactId = c.id,
     TimeZoneSidKey='America/Los_Angeles', UserName='teststandarduser@stemcell.com');
     insert u;        
}

In the test method retrieve the user and use accordingly.

User u = [SELECT Id, Name FROM User WHERE Email='teststandarduser@stemcell.com'];
system.runAs(u){

}

You should be writing test class following Testing best practices

| improve this answer | |

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.