function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashok0572Ashok0572 

Getting error in Test class plx help me

DeleteQuoteLineItemCTLRTest.myUnitTest -- System.DmlException: Insert failed. First exception on row 0; 

first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Quote Account and the Opportunity Account must be insync: [Account__c]

    Stack trace: Class.DeleteQuoteLineItemCTLRTest.myUnitTest: line 58, column 1



==========================================================

@isTest
private class DeleteQuoteLineItemCTLRTest {

    static testMethod void myUnitTest() {
        Organization__c organizationObj = (Organization__c)TestDataHelper.createSObject('Organization__c');
        insert organizationObj;
        TestDataHelper.createSalesRegionUser(organizationObj.id);
        
        Account accObj = (Account) TestDataHelper.createSObject('Account');
        accObj.BillingCountry = 'Germany';
        insert accObj;
        
        AccountContactRole accountContactRole=(AccountContactRole)TestDataHelper.createSObject('AccountContactRole',true,1);
        Opportunity oppty=(Opportunity)TestDataHelper.createSObject('Opportunity');
        oppty.accountId=AccountContactRole.AccountId;
        accountContactRole.IsPrimary=true;
        insert accountContactRole;
        insert oppty;
        
        system.assertequals(oppty.accountid,accountContactRole.AccountId);
        Quote quote = (Quote)TestDataHelper.createSObject('Quote');
        quote.opportunityId=oppty.id;
        quote.Sold_To__c = accobj.Id;
        //quote.Account__c= accObj.Id;
        //quote.Account__c = accObj.Id;
        insert quote;
        
        
        system.assertequals(quote.OpportunityId,oppty.id);
        list<QuoteFieldIds__c> quoteFields=TestDataHelper.getQuoteFieldIds();
        insert quoteFields;
        
        system.Test.setCurrentPage(Page.AppendSaveURLQLI);
        
         Test.startTest();
            // Run as System Administrator
            //system.runAs(systemAdministrator){ 
                //runDeleteQuoteCTLR(quote);
                ApexPages.currentPage().getParameters().put('newid',quote.id);
                ApexPages.currentPage().getParameters().put('oppid',oppty.id);
                runDeleteQuoteCTLR(quote);
                system.assertequals(ApexPages.currentPage().getParameters().get('newid'),quote.id);
                system.assertequals(ApexPages.currentPage().getParameters().get('oppid'),oppty.id);
                //ApexPages.currentPage().getParameters().put('save_new','Https://login.salesforce.com?id=test');
                //runDeleteQuoteCTLR(quote);
            //}
            
        Test.stopTest();
    }
    static void runDeleteQuoteCTLR(Quote quote){
        ApexPages.Standardcontroller controller=new ApexPages.Standardcontroller(quote);
        DeleteQuoteLineItemCTLR deleteQLICTLR=new DeleteQuoteLineItemCTLR(controller);
        deleteQLICTLR.deleteQLI();
        deleteQLICTLR.appendSaveURL();
    }
}
Rajiv Bhatt 16Rajiv Bhatt 16
The error message is thrown from a custom validation rule that is written on the Account object. You might want to check the validation rules and prepare your test data accordingly.
ManojjenaManojjena
Hi Anji ,

Please try with below code .Your error is saying that the Opportunity and Quote should tag to same account .
 
@isTest
private class DeleteQuoteLineItemCTLRTest {

    static testMethod void myUnitTest() {
        Organization__c organizationObj = (Organization__c)TestDataHelper.createSObject('Organization__c');
        insert organizationObj;
        TestDataHelper.createSalesRegionUser(organizationObj.id);
        
        Account accObj = (Account) TestDataHelper.createSObject('Account');
        accObj.BillingCountry = 'Germany';
        insert accObj;
        
        AccountContactRole accountContactRole=(AccountContactRole)TestDataHelper.createSObject('AccountContactRole',true,1);
        Opportunity oppty=(Opportunity)TestDataHelper.createSObject('Opportunity');
        oppty.accountId=AccountContactRole.AccountId;
        accountContactRole.IsPrimary=true;
        insert accountContactRole;
        insert oppty;
        
        system.assertequals(oppty.accountid,accountContactRole.AccountId);
        Quote quote = (Quote)TestDataHelper.createSObject('Quote');
        quote.opportunityId=oppty.id;
        quote.Sold_To__c = accobj.Id;
        quote.accountId=AccountContactRole.AccountId;
        quote.Account__c = AccountContactRole.AccountId;
        insert quote;
        
        
        system.assertequals(quote.OpportunityId,oppty.id);
        list<QuoteFieldIds__c> quoteFields=TestDataHelper.getQuoteFieldIds();
        insert quoteFields;
        
        system.Test.setCurrentPage(Page.AppendSaveURLQLI);
        
         Test.startTest();
            // Run as System Administrator
            //system.runAs(systemAdministrator){ 
                //runDeleteQuoteCTLR(quote);
                ApexPages.currentPage().getParameters().put('newid',quote.id);
                ApexPages.currentPage().getParameters().put('oppid',oppty.id);
                runDeleteQuoteCTLR(quote);
                system.assertequals(ApexPages.currentPage().getParameters().get('newid'),quote.id);
                system.assertequals(ApexPages.currentPage().getParameters().get('oppid'),oppty.id);
                //ApexPages.currentPage().getParameters().put('save_new','Https://login.salesforce.com?id=test');
                //runDeleteQuoteCTLR(quote);
            //}
            
        Test.stopTest();
    }
    static void runDeleteQuoteCTLR(Quote quote){
        ApexPages.Standardcontroller controller=new ApexPages.Standardcontroller(quote);
        DeleteQuoteLineItemCTLR deleteQLICTLR=new DeleteQuoteLineItemCTLR(controller);
        deleteQLICTLR.deleteQLI();
        deleteQLICTLR.appendSaveURL();
    }
}

Please let me know it helps or you need any help .
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Anji,

Please try below code.
@isTest
private class DeleteQuoteLineItemCTLRTest {

    static testMethod void myUnitTest() {
        Organization__c organizationObj = (Organization__c)TestDataHelper.createSObject('Organization__c');
        insert organizationObj;
        TestDataHelper.createSalesRegionUser(organizationObj.id);
        
        Account accObj = (Account) TestDataHelper.createSObject('Account');
        accObj.BillingCountry = 'Germany';
        insert accObj;
        
		Quote quote = (Quote)TestDataHelper.createSObject('Quote');
        //quote.opportunityId=oppty.id;
        quote.Sold_To__c = accobj.Id;
        //quote.Account__c= accObj.Id;
        //quote.Account__c = accObj.Id;
        insert quote;
		
        AccountContactRole accountContactRole=(AccountContactRole)TestDataHelper.createSObject('AccountContactRole',true,1);
        Opportunity oppty=(Opportunity)TestDataHelper.createSObject('Opportunity');
        oppty.accountId=AccountContactRole.AccountId;
		oppty.accountId =accObj.Id;
        accountContactRole.IsPrimary=true;
        insert accountContactRole;
		oppty.SyncedQuoteId = quote.Id;
        insert oppty;
        
        system.assertequals(oppty.accountid,accountContactRole.AccountId);
        
        
        
        system.assertequals(quote.OpportunityId,oppty.id);
        list<QuoteFieldIds__c> quoteFields=TestDataHelper.getQuoteFieldIds();
        insert quoteFields;
        
        system.Test.setCurrentPage(Page.AppendSaveURLQLI);
        
         Test.startTest();
            // Run as System Administrator
            //system.runAs(systemAdministrator){ 
                //runDeleteQuoteCTLR(quote);
                ApexPages.currentPage().getParameters().put('newid',quote.id);
                ApexPages.currentPage().getParameters().put('oppid',oppty.id);
                runDeleteQuoteCTLR(quote);
                system.assertequals(ApexPages.currentPage().getParameters().get('newid'),quote.id);
                system.assertequals(ApexPages.currentPage().getParameters().get('oppid'),oppty.id);
                //ApexPages.currentPage().getParameters().put('save_new','Https://login.salesforce.com?id=test');
                //runDeleteQuoteCTLR(quote);
            //}
            
        Test.stopTest();
    }
    static void runDeleteQuoteCTLR(Quote quote){
        ApexPages.Standardcontroller controller=new ApexPages.Standardcontroller(quote);
        DeleteQuoteLineItemCTLR deleteQLICTLR=new DeleteQuoteLineItemCTLR(controller);
        deleteQLICTLR.deleteQLI();
        deleteQLICTLR.appendSaveURL();
    }
}

Let us know if it helps.
Ashok0572Ashok0572
Hi Ashish,
Thanks for posting,
and i'm getting the error after copying urs code
 Variable does not exist: quote.Id at line 26 column 31
Ashok0572Ashok0572
Hi Manoj,
Thanks for posting,
 i'm getting the error after copying urs code

Invalid field accountId for SObject Quote at line 24 column 9
Ashok0572Ashok0572
Hi Manoj,
By removing
quote.Account__c = AccountContactRole.AccountId;

getting Null pointer exception
ManojjenaManojjena
HI Anji ,

Please remove below line and test ,let me know any issue .
quote.accountId=AccountContactRole.AccountId;
Ashok0572Ashok0572
Hi Manoj,
by removing mentioned code,
getting an error
System.NullPointerException: Attempt to de-reference a null object