+ Start a Discussion
BrittanieBrittanie 

Test with Error: System.QueryException: List has no rows for assignment to SObject

My trigger works great but...

After 80 drafts of the test I can't get it to work.... I need to make about 4 more triggers similar to this so this one test is kinda of crucial to recreate it for the others.

 

Currently getting the following error:

 

Error Message System.QueryException: List has no rows for assignment to SObject Stack Trace Class.UpdateAcctContactDecliningTestClass.myUnitTest: line 22, column 1

 

@isTest

private class UpdateAcctContactDecliningTestClass { 

   static testMethod void myUnitTest(){
    Account accts = new Account (Contact_Declining__c = Null);
      
          System.debug('Created Account with Id: ' + accts.id);

        
            // fill in all other required fields fields
   
   
    Opportunity o = new Opportunity(AccountId=accts.Id,Primary_Contact__c = '003000000001NjA');
        
   
   o.Primary_Contact__c = '003000000001NjA';
    
    Test.startTest();
    
        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c,[SELECT Contact_Declining__c FROM Account WHERE Id = :accts.Id].Contact_Declining__c ); 
       
             update accts;

       
   Test.stopTest();

    }
}

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
  update accts; //do the update

}
 }

 

Avidev9Avidev9

Brittanie again in trouble ?

 

Seems like I have seen the test class somewhere

 

Anyways you never inserted test data in your class and that was the mistake

try this

 

@isTest
private class UpdateAcctContactDecliningTestClass {

    static testMethod void myUnitTest() {
        Account accts = new Account(Contact_Declining__c = Null);
		insert accts;
        // fill in all other required fields fields
		Contact con = new Contact(LastName ='test');
        insert con;
        Opportunity o = new Opportunity(AccountId = accts.Id, Primary_Contact__c = con.Id);
        insert o;

        Test.startTest();
        update accts;
        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c, [SELECT Contact_Declining__c FROM Account WHERE Id = : accts.Id].Contact_Declining__c);

        //update accts;


        Test.stopTest();

    }
}

 If it complains about required fields, I guess you are aware about what to do right?

BrittanieBrittanie

I appreciate your patients as I try and learn APEX as when I was hired I was hired to run and maintain with minor changes and am having to learn a ton of code to achieve the requested changes to the (very messy) system I inherited and as many can tell my only coding experience was with HTML a good 10-15 years ago :)

 

I manage to update that but realized I have filters on both the look up on the Opportunity and the lookup on the Account.  How and where do I add that to the test.

 

Account lookup field to contact (Contact_Declining__c)  filter: 

Filter Criteria

Person Accepting/Declining Meeting: Account Name IDEQUALSAccount: Account ID

 

 

Opportunity lookup field to contact (Primary_Contact__c) filter:

Filter Criteria

Primary Contact: Account Name IDEQUALSOpportunity: Account Name ID
@isTest
private class UpdateAcctContactDecliningTestClass {

    static testMethod void myUnitTest() {
        Account accts = new Account(
    
    Name = 'Account',
    Account_Classification__c = 'Sector',
    Management_Status__c = 'AP',
    Account_Status__c = 'Not Started',
    Coding__c = 'priority',
    Market_Location__c = 'DMA');

        insert accts;
        // fill in all other required fields fields
        

    Contact con = new Contact(
    LastName ='test');

        insert con;
       

     Opportunity o = new Opportunity(AccountId = accts.Id, Primary_Contact__c = con.Id);

        insert o;

        Test.startTest();

        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c, [SELECT Contact_Declining__c FROM Account WHERE Id = : accts.Id].Contact_Declining__c);

        update accts;


        Test.stopTest();

    }
}

 

Avidev9Avidev9
Can you elaborate the filter criteria ?
Its really hard to understand from what you have given.
may be a lil more detail api name,value etc.

You just have to set apt value according to the filter thats it
BrittanieBrittanie

Field Label:       Person Accepting/Declining Meeting

Object Name:   Account:

Field Name:     Contact_Declining

Data Type:        Lookup

API Name:        Contact_Declining__c

 Filter Criteria:   Person Accepting/Declining Meeting: Account Name IDEQUALSAccount: Account ID

 

 

Field Label:        Primary Contact

Object Name:     Opportunity

Field Name:        Primary_Contact

Data Type:           Lookup

API Name:           Primary_Contact__c

Filter Criteria:      Primary Contact: Account Name IDEQUALSOpportunity: Account Name ID

 

 

I thought it would be some kind of IF statement but not really sure how to write that and where exactly to put it.  I'm assuming that there would be one under Account and one under Opportunity in the test.... I have looked at the boards and through the API sites and can't figure out how to do this.

Avidev9Avidev9
@isTest
private class UpdateAcctContactDecliningTestClass {

    static testMethod void myUnitTest() {
        Account accts = new Account(
    
    Name = 'Account',
    Account_Classification__c = 'Sector',
    Management_Status__c = 'AP',
    Account_Status__c = 'Not Started',
    Coding__c = 'priority',
    Market_Location__c = 'DMA');

        insert accts;
        // fill in all other required fields fields
        

    Contact con = new Contact(AccountId=accts.Id,
    LastName ='test');

        insert con;
       

     Opportunity o = new Opportunity(AccountId = accts.Id, Primary_Contact__c = con.Id);

        insert o;

        Test.startTest();

        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c, [SELECT Contact_Declining__c FROM Account WHERE Id = : accts.Id].Contact_Declining__c);

        update accts;


        Test.stopTest();

    }
}
BrittanieBrittanie

So realized I missed the fields for opportunity... so I added them... struggled with how to format the date... got that... and now I can't seem to get around this error despite closedate being clearly listed and all internet searched lead to a dead in.

 

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]

 

@isTest
private class UpdateAcctContactDecliningTestClass {

    static testMethod void myUnitTest() {
        Account accts = new Account(
    
    Name = 'Account',
    Account_Classification__c = 'Sector',
    Management_Status__c = 'AP',
    Account_Status__c = 'Not Started',
    Coding__c = 'priority',
    Market_Location__c = 'DMA');

        insert accts;
        // fill in all other required fields fields
        

    Contact con = new Contact(AccountId=accts.Id,
    LastName ='test');

        insert con;
       

     Opportunity o = new Opportunity(
     AccountId = accts.Id, 
     Primary_Contact__c = con.Id,
     Name = 'Account Year Month',
     StageName = 'In Process',
     NextStep = 'Set Meeting',
     ForecastCategoryName = 'Pipeline',
     CloseDate = Date.newInstance(2006,10,10));

        insert o;

        Test.startTest();

        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c, [SELECT Contact_Declining__c FROM Account WHERE Id = : accts.Id].Contact_Declining__c);

        update accts;


        Test.stopTest();

    }
}

 

 

Avidev9Avidev9
Add

o.closeDate = System.today();
BrittanieBrittanie

o.closeDate = System.today()); =  error message and won't save:

Error: Compile Error: Invalid field initializer: o.closeDate at line 31 column 4

CloseDate = System.today()); = error message when test fails:

System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]

 

 

I am starting to need one of those bang head here right on my desk sort of things.

 

Avidev9Avidev9
Am not sure what you are doing should work.
Please repost ur code
BrittanieBrittanie
@isTest
private class UpdateAcctContactDecliningTestClass {

    static testMethod void myUnitTest() {
        Account accts = new Account(
    
    Name = 'Account',
    Account_Classification__c = 'Sector',
    Management_Status__c = 'AP',
    Account_Status__c = 'Not Started',
    Coding__c = 'priority',
    Market_Location__c = 'DMA');

        insert accts;
        // fill in all other required fields fields
        

    Contact con = new Contact(AccountId=accts.Id,
    LastName ='test');

        insert con;
       

     Opportunity o = new Opportunity(
     AccountId = accts.Id, 
     Primary_Contact__c = con.Id,
     Name = 'Account Year Month',
     StageName = 'In Process',
     NextStep = 'Set Meeting',
     ForecastCategoryName = 'Pipeline',
   CloseDate = System.today());

        insert o;

        Test.startTest();

        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c, [SELECT Contact_Declining__c FROM Account WHERE Id = : accts.Id].Contact_Declining__c);

        update accts;


        Test.stopTest();

    }
}

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
  update accts; //do the update

}
 }

 

Avidev9Avidev9

Seems good what is the error you are getting ? 

Post exact line and error

BrittanieBrittanie

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]