• Ana Rottaro
  • NEWBIE
  • 15 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
I have the following validation rule and I want it to only prevent creation of new opportunities if one already exists and is open (what Count of Opportunities is counting). However, I had an account executive unable to Close Won an existing Opportunity which is the only opp on the account because of it.  

AND(
Account.Count_of_Opportunities__c>0,
$User.Title == "Account Executive",
isnew()
)
I am writing a test for a lcass that is called by a process when an opp closes.  The class creates records for a custom object.  I tested it in the sandbox and it is working, however in the test class when I do the action that causes the process builder to go off and update the record, the list of the custom objects is coming back empty.  I suspect it may be because it takes a while for the process builder and class called to finish even when I do it manually (about 30 seconds).  Is there something else that might be casuing this or is there a way I can make sure the assertions don't run until after the process builder and class I am testing finish?
I wrote a class that is set off through process builder if an Opportunity's stage field is closed won.  The class makes records called Ads (a custom onject) based on the opportunity line items (OLI).  Depending on the OLI code, the Ad is created with different information in the fields. This works in the sandbox, but when I write the test class I can't get it to pass because it says the list of Ads is empty. I decided to write the test class as only testing one OLI/Ad type called AWR first so that I could see if I am writing it correctly and then test all the cases.  Here's what I have: 

```@isTest
private class testCreateAd {
 @isTest static void testCreateAd() {
        // Add products and  price books
        List<sObject> lsprd = Test.loadData(Product2.sObjectType, 'testProducts');
        List<sObject> lspb = Test.loadData(Pricebook2.sObjectType, 'testPricebooks');
        
        // Assign standard price to each product and add it to the standard pricebook        
        for(sObject prd : lsprd) {
            PricebookEntry pbe = new PricebookEntry(UnitPrice = 0, Pricebook2Id = Test.getStandardPricebookId(), Product2Id = prd.Id, IsActive = TRUE);
            insert pbe;
        }
        
        // Add price book entries for each product in each price book
        List<sObject> lspbe = Test.loadData(PricebookEntry.sObjectType, 'testPricebookEntry');
        
        // Get the ID for the College price book
        List<Pricebook2> pb = [Select Id, Name From Pricebook2 Where Name = 'College Price Book'];
        System.debug(pb);
        Pricebook2 CPB = pb.get(0);
                
        // Insert account 
        Account acct = new Account(Name = 'Test Account');
        insert acct;
        
        // Add opportunity to the test account
        Opportunity opp = new Opportunity(Name = 'Opp', AccountId = acct.Id, CloseDate = system.today(), 
                                          StageName = 'Proposal', Start_Date__c = system.today(),
                                          Discount_Level__c = NULL, Pricebook2Id = CPB.Id, Type = 'New Business');
        insert opp;
        System.debug(opp);
 
     //Getting Date
        String d = String.valueOf(Date.today()).removeEnd(' 00:00:00');
     
        Test.startTest();
     
        // Choose a package to automatically add opportunity line items to the opportunity
        opp.Package__c = 'Reach Max';
        opp.Discount_Level__c = NULL;
        update opp;
        System.debug(opp);
 //Close the opp so that the process builder goes off to run the CreateAd class 
        opp.StageName = 'Closed Won';
        update opp;
        System.debug(opp); 
        Test.stopTest();

     //AWR coded opportunity line item and ad asserts
     List<Ad__c> adsAWR = [Select Name, Amount_Purchased__c, Account__c, 
                                                 End_Date__c, 
                                                GUID__c, Start_Date__c, Reporting_Type__c, 
                                                 Line_Item_Notes__c, OwnerId, Status__c, 
                                                 Type__c From Ad__c];
      Ad__c adAWR = adsAWR.get(0);
     
     List<OpportunityLineItem> olisAWR = [Select Name, Product2Id, Quantity, ProductCode, Subtotal, Description From OpportunityLineItem Where OpportunityId =: opp.Id];
        System.debug(olisAWR);
     OpportunityLineItem oliAWR = olisAWR.get(0);
     
     System.assert(adAWR.Name == acct.Name + ' ' + oliAWR.ProductCode + ' ' + d);
     System.assert(adAWR.Amount_Purchased__c == oliAWR.Subtotal);
     System.assert(adAWR.Account__c == acct.ID);
     System.assert(adAWR.End_Date__c == acct.Premium_Profile_End__c);
     System.assert(adAWR.GUID__c == acct.GUID__c);
     System.assert(adAWR.Start_Date__c == acct.Premium_Profile_Start__c);
     System.assert(adAWR.Reporting_Type__c == acct.Reporting_Type__c); 
     System.assert(adAWR.Line_Item_Notes__c == oliAWR.Description); 
     System.assert(adAWR.OwnerId == '0053600000DjFmXAAV');
     System.assert(adAWR.Status__c == 'Pending Live'); 
     System.assert(adAWR.Type__c == oliAWR.ProductCode);
 
    }
}```

Thanks in advance! I'm new to apex!
I have the following validation rule and I want it to only prevent creation of new opportunities if one already exists and is open (what Count of Opportunities is counting). However, I had an account executive unable to Close Won an existing Opportunity which is the only opp on the account because of it.  

AND(
Account.Count_of_Opportunities__c>0,
$User.Title == "Account Executive",
isnew()
)