• sssteele
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies

Hi, all,

I'm just starting to tinker around with the development side of SFDC and I've created a trigger that works fine, but I can't seem to ever get my test class to assert true. I've included both below. Any assistance you can provide is greatly appreciated (even in my trigger or best practices). Forgive my stray comments inline.

Trigger:

trigger populateOpptyChildRecords on Opportunity (before update) {
for( Opportunity parent: Trigger.new)
{
 
List<Ad_Campaign__c> children = [ SELECT Id, Opportunity_Id__c, Opportunity_OwnerID__c from 
Ad_Campaign__c where Opportunity__c = :parent.Id];
 
List<Ad_Campaign__c> childrenToUpdate = new List<Ad_Campaign__c>();
 
for(Ad_Campaign__c thischild: children )
{
   if( thischild.Opportunity_OwnerID__c !=  parent.OwnerID)
   {
 //      thischild.Opportunity_OwnerId__c =  parent.OwnerId;
       thischild.Parent_Updated__c = TRUE;
       thischild.Parent_Updated__c = FALSE;
      childrenToUpdate.add(thischild);
   }
}
 
//if( !childrenToUpdate.isEmpty)
//{
   update childrenToUpdate;
//}
 
}
}

 



/=========================================

Test Class:

@isTest
 
private class testPopulateOpptyChildRecords{
static testMethod void runPositiveTestCases(){
        //Set up new account, related oppty, and two users
        
        Account acct = new Account(Name='test account');
            insert acct;
        Account a=[SELECT Id from Account WHERE Name = 'test account' LIMIT 1];
        Opportunity o = new Opportunity(Name='test opportunity', AccountID = acct.Id, StageName='Prospecting', CloseDate=system.Today());
        insert o;
        Ad_Campaign__c ac1 = new Ad_Campaign__c(Opportunity__c=o.Id);
        insert ac1;
        Profile p=[SELECT id FROM Profile p WHERE p.name = 'System Administrator'];
        User u1=new User(
            FirstName='Test', 
            LastName='User', 
            Username='test@test.com.damp', 
            Email='test@test.com', 
            Alias='test1', 
            CommunityNickname='test1', 
            TimeZoneSidKey='America/Los_Angeles', 
            LocaleSidKey='en_US', 
            EmailEncodingKey='ISO-8859-1', 
            ProfileId=p.id, 
                LanguageLocaleKey='en_US');
        insert u1;
    // Get the value for Owner from the Oppty
        Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE Name = 'test opportunity' LIMIT 1];
        Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c IN (SELECT Id from Opportunity WHERE Name = 'test opportunity')];
        User u2=[SELECT Id, Name FROM User WHERE Name = 'Test User'];
    //Check to see if the Owner1 field is filled out on the ad campaign
    //System.Assert(ac1.Opportunity_Owner1__c != ''); -- nevermind
    //Update a field on the opportunity to start the trigger
        u2.Id=o1.OwnerId;
        System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);
    }
}

 


//Thanks again

I'm hoping someone can help. I'm writing a test class for a simple Line Item trigger (totally new at this). I am getting an error that the PriceBookEntryId can't be blank, but every time I try to pull in an ID, I get the error that my query returned no rows. 

 

Original trigger:

 

1
2
3
4

trigger OpportunityLineItemBeforeDelete on OpportunityLineItem (before delete) {
for (OpportunityLineItem li: Trigger.old)
    li.addError('Cannot remove a property once added. Contact support');
}

 

 

Test class that gives me the error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id; others must specify product id): [PricebookEntryId, unknown]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@IsTest
public class LIDeleteClass{
    static TestMethod void InsertOppty()
    {Date myDate = date.newinstance(2014, 12, 17);
    Opportunity oppty = new Opportunity(Name='Test', StageName='IO Signed', CloseDate=myDate);
      insert oppty;
    Opportunity oppty1 = [select id, Name, Stagename, Closedate 
                          from Opportunity 
                          where Name = :'Test' LIMIT 1];

     //PricebookEntry pbe = [select Id 
       //                    from PricebookEntry
         //                  where Product2.Name = :'Amazon Ad Platform (AAP)' 
     
            //               and Pricebook2.Name = :'Standard Price Book' LIMIT 1];
    OpportunityLineItem li = new OpportunityLineItem(OpportunityId=oppty1.id, Quantity=12000,TotalPrice=12000);

      insert li;
    Test.starttest();
       try
           {
              delete li;
            
              System.assert(false);
           }
           catch (DMLException e)
           {
          
           }
    Test.stoptest();
    }}

 

When I uncomment the pbe variable, I get the error System.QueryException: List has no rows for assignment to SObject.

 

Any help would be appreciated.

Hi, all,

I'm just starting to tinker around with the development side of SFDC and I've created a trigger that works fine, but I can't seem to ever get my test class to assert true. I've included both below. Any assistance you can provide is greatly appreciated (even in my trigger or best practices). Forgive my stray comments inline.

Trigger:

trigger populateOpptyChildRecords on Opportunity (before update) {
for( Opportunity parent: Trigger.new)
{
 
List<Ad_Campaign__c> children = [ SELECT Id, Opportunity_Id__c, Opportunity_OwnerID__c from 
Ad_Campaign__c where Opportunity__c = :parent.Id];
 
List<Ad_Campaign__c> childrenToUpdate = new List<Ad_Campaign__c>();
 
for(Ad_Campaign__c thischild: children )
{
   if( thischild.Opportunity_OwnerID__c !=  parent.OwnerID)
   {
 //      thischild.Opportunity_OwnerId__c =  parent.OwnerId;
       thischild.Parent_Updated__c = TRUE;
       thischild.Parent_Updated__c = FALSE;
      childrenToUpdate.add(thischild);
   }
}
 
//if( !childrenToUpdate.isEmpty)
//{
   update childrenToUpdate;
//}
 
}
}

 



/=========================================

Test Class:

@isTest
 
private class testPopulateOpptyChildRecords{
static testMethod void runPositiveTestCases(){
        //Set up new account, related oppty, and two users
        
        Account acct = new Account(Name='test account');
            insert acct;
        Account a=[SELECT Id from Account WHERE Name = 'test account' LIMIT 1];
        Opportunity o = new Opportunity(Name='test opportunity', AccountID = acct.Id, StageName='Prospecting', CloseDate=system.Today());
        insert o;
        Ad_Campaign__c ac1 = new Ad_Campaign__c(Opportunity__c=o.Id);
        insert ac1;
        Profile p=[SELECT id FROM Profile p WHERE p.name = 'System Administrator'];
        User u1=new User(
            FirstName='Test', 
            LastName='User', 
            Username='test@test.com.damp', 
            Email='test@test.com', 
            Alias='test1', 
            CommunityNickname='test1', 
            TimeZoneSidKey='America/Los_Angeles', 
            LocaleSidKey='en_US', 
            EmailEncodingKey='ISO-8859-1', 
            ProfileId=p.id, 
                LanguageLocaleKey='en_US');
        insert u1;
    // Get the value for Owner from the Oppty
        Opportunity o1=[SELECT Id, OwnerID from Opportunity WHERE Name = 'test opportunity' LIMIT 1];
        Ad_Campaign__c ac2 =[SELECT Id, Opportunity_OwnerID__c from Ad_Campaign__c WHERE Opportunity__c IN (SELECT Id from Opportunity WHERE Name = 'test opportunity')];
        User u2=[SELECT Id, Name FROM User WHERE Name = 'Test User'];
    //Check to see if the Owner1 field is filled out on the ad campaign
    //System.Assert(ac1.Opportunity_Owner1__c != ''); -- nevermind
    //Update a field on the opportunity to start the trigger
        u2.Id=o1.OwnerId;
        System.Assert(ac1.Opportunity_OwnerID__c==o1.OwnerId);
    }
}

 


//Thanks again

I'm hoping someone can help. I'm writing a test class for a simple Line Item trigger (totally new at this). I am getting an error that the PriceBookEntryId can't be blank, but every time I try to pull in an ID, I get the error that my query returned no rows. 

 

Original trigger:

 

1
2
3
4

trigger OpportunityLineItemBeforeDelete on OpportunityLineItem (before delete) {
for (OpportunityLineItem li: Trigger.old)
    li.addError('Cannot remove a property once added. Contact support');
}

 

 

Test class that gives me the error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id; others must specify product id): [PricebookEntryId, unknown]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@IsTest
public class LIDeleteClass{
    static TestMethod void InsertOppty()
    {Date myDate = date.newinstance(2014, 12, 17);
    Opportunity oppty = new Opportunity(Name='Test', StageName='IO Signed', CloseDate=myDate);
      insert oppty;
    Opportunity oppty1 = [select id, Name, Stagename, Closedate 
                          from Opportunity 
                          where Name = :'Test' LIMIT 1];

     //PricebookEntry pbe = [select Id 
       //                    from PricebookEntry
         //                  where Product2.Name = :'Amazon Ad Platform (AAP)' 
     
            //               and Pricebook2.Name = :'Standard Price Book' LIMIT 1];
    OpportunityLineItem li = new OpportunityLineItem(OpportunityId=oppty1.id, Quantity=12000,TotalPrice=12000);

      insert li;
    Test.starttest();
       try
           {
              delete li;
            
              System.assert(false);
           }
           catch (DMLException e)
           {
          
           }
    Test.stoptest();
    }}

 

When I uncomment the pbe variable, I get the error System.QueryException: List has no rows for assignment to SObject.

 

Any help would be appreciated.