• Simon Law (Quantcast)
  • NEWBIE
  • 0 Points
  • Member since 2018
  • Quantcast

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
When i run the test class in production individually all the methods get passed and gives a code coverage 0f 82% .When i run all the test class ...it throws an error for this test class of one method as
System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] 
Error line is :insert pbe1;
Why is this difference?Any Suggestion please.
Here is Part of the code :
static testMethod void contractInterface3()      
    {
        //Create Account
        Account acc =  new Account();       
        acc.Name = 'Compro';          
        acc.BillingStreet = '4 LSC';
        acc.BillingCity = 'New Delhi';
        acc.BillingState = 'Delhi';
        acc.BillingPostalCode = '110049';
        acc.BillingCountry = 'India';
        acc.Phone = '100';
        acc.Industry = 'Banking';         
        acc.Type = 'Paid';
        acc.Customer_Type__c = 'Customer';
        acc.Customer_List__c = true;    
        acc.Traning_sold__c = 0.00;
        acc.Subscriptions_sold__c = 0.00;
        acc.Services_sold__c = 0.00;
        acc.Last_Opportunity_Close_Date__c = system.today(); 
        acc.ARR_Customer__c = false;
        acc.Current_ARR_Customer__c = false;
        acc.Last_Renewal_Close_Date__c = system.today(); 
        insert acc;     


        //Create Contact
        Contact con =  new Contact();
        con.LeadSource = 'Web';                 
        con.FirstName = 'ABC';
        con.LastName = 'XYZ';
        con.MailingStreet = '4 LSC';
        con.MailingCity = 'New Delhi';
        con.MailingState = 'Delhi';
        con.MailingPostalCode = '110049';
        con.MailingCountry = 'India';
        con.Phone = '100';             
        con.AccountId = acc.Id ;
        con.Email = 'xyz@xyz.com';
        con.ContactType__c = 1;
        insert con;     


        //Create Opportunity
        Opportunity oppty =  new Opportunity();
        oppty.Ecommerce_Transaction_ID__c = 999;
        oppty.LeadSource = 'Subscription Renewal';
        oppty.Name = 'Test Oppty';
        oppty.CloseDate = System.Today();
        oppty.StageName = 'Closed Won';                     
        oppty.Compelling_Event__c = 'N/A';
        oppty.NextStep = 'N/A';
        oppty.Decision_Maker__c  = 'N/A';
        oppty.Comments__c = 'N/A';
        oppty.Committed__c = true;
        oppty.Billing_Contact__c = con.Id;
        oppty.Payment_Type__c = 'Credit Card';
        oppty.Partner__c = 'None';
        oppty.AccountId = acc.Id;
        oppty.Update_Complete__c = true;                    
        insert oppty;

        //Add oppty contact role
        string stdpdId = '01s500000001e6k';
        List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();          
        OpportunityContactRole opptyConRole = new OpportunityContactRole();           
        opptyConRole.ContactId = con.Id;
        opptyConRole.OpportunityId = oppty.Id;   
        opptyConRole.IsPrimary = true;
        opptyConRole.Role = 'Partner Contact';  
        insert opptyConRole;


        //Product type support
        Product2 prd1 = new Product2();
        prd1.Name = 'Protection Suite for Linux Multi-site Upgrade - 5 Year Support';
        prd1.IsActive = true;
        prd1.Family = 'PostgreSQL';
        prd1.Description = 'Protection Suite for Linux Multi-site Upgrade - 5 Year Support';
        prd1.ProductCode = 'SE-LB-MSCU-S5';
        prd1.ARR_Impact1__c = 'Yes';
        prd1.Notes__c = 'Accounting Contact';
        prd1.Payment_Terms__c = 'test';
        prd1.Product_Bucket__c = 'Subscription - Multi-year';
        prd1.Product_Categories__c = 'Subscription - Multi-year';
        prd1.Product_Segmentation__c = 'External Products';
        prd1.Product_Name1__c = 'Product - Support';
        prd1.Term__c = '5';
        prd1.Unit_of_Measure__c = 'Server';
        insert prd1;

        PricebookEntry pbe1 = new PricebookEntry();
        pbe1.Product2ID=prd1.id;
        pbe1.Pricebook2ID=stdpdId;
        pbe1.UnitPrice=50; 
        pbe1.isActive=true;
        insert pbe1;

        OpportunityLineItem oli =  new OpportunityLineItem();           
        oli.OpportunityId = oppty.Id;            
        oli.UnitPrice = 100;
        oli.Quantity = 1;
        oli.PricebookEntryId = pbe1.Id;
        oli.Start_Date__c = System.today();
        oli.End_Date__c =  System.today() + 100;
        oli.Term__c = 12;
        //oli.TotalPrice = 12;
        OLI.Transfer_Price__c = 12;
        oli.Quantity = 2;
        insert oli;


        String page1 = '/apex/SubscriptionCertificatePage' + '?id=' + oppty.id;             
        PageReference pageRef = new PageReference(page1);        
        Test.setCurrentPage(pageRef );
        //ApexPages.StandardController controller = new ApexPages.StandardController(oppty);
        ApexPages.currentPage().getParameters().put('oppty',oppty.id);
        SubscriptionCertificateClass ctrl =  new SubscriptionCertificateClass();
        ctrl.savePDFAsAttachment();    

    }
}


 
Hi,

i get an UNABLE_TO_LOCK_ROW  error when running my tests in parallel. The error occurs when creating a pricebookentry in the standard pricebook. As far as i know Salesforce locks also the parent Object when inserting a object. Which would mean that in this case while inserting the pricebookentry the pricebook is locked. When now a parallel test wants to add another entry to the locked pricebook the test throws the UNABLE_TO_LOCK_ROW error.

Does anyone know how to get around this?