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
Rachel Linder 8Rachel Linder 8 

Trigger Deployment Errors

I am new to this org I am currently working in. I am deploying my first trigger as an admin. I received the attached 2 errors. Any ideas on how I can troubleshoot these:

User-added image
Raj VakatiRaj Vakati
Hi Rachel,
You apex Test class are failing in while you are deploying .
Class 1: - Test_UpdateContactRoleCount - System Asserting is work
Class 2 :- TestClass - You are query is not returning any data.  So insert data first and Query them in the test method. 

 
Rachel Linder 8Rachel Linder 8
Hi Raj, thanks for your help. But since I am new to this org and writing triggers. What exactly do I need to do to get my trigger to deploy and work? For the Class1 that is part of a package we installed. 

Either way what is needed to move past the errors? Here is the actual trigger I am deploying:

User-added image
bainesybainesy
Can you post the code for your 2 test classes?
Rachel Linder 8Rachel Linder 8
This first code is from a test class from a package we installed (test_updatecontactrolecount):
User-added image

Here are three screen shots for the second one (TestClass). The second one was written by a previous employee who had developer experience.
User-added image

User-added image

User-added image

If there is a better way for me to get this code to you please let me know.
bainesybainesy
Let's tackle test_updatecontactrolecount first.
The assert is failing because the code is throwing an error. Do you have any validation rules or required fields that would make an insert of opporutunities or contacts fail?
Rachel Linder 8Rachel Linder 8
I am in Southern California and will be heading out of the office in about 10 minutes (4:30pm my time). I will be back online around 6:00pm my time. Thanks in advance.
bainesybainesy
OK, if we dont catch each other tonight, we can continue tomorrow. I'll look at the second test class now
Rachel Linder 8Rachel Linder 8
sounds great.
bainesybainesy
For the 2nd test class, this is not best practice but this will make the test pass...

Create an opportunity with the name = 'test', a close date more than 60 days in the past, Sales Stage High Level = 'Open' and add at least 1 product. That should allow that test class to work correctly and pass.
Rachel Linder 8Rachel Linder 8
Ok..i was able to create the test opportunity today. and i deployed the trigger again. And it failed with 2 errors. I am running the default test option. Should I be using another test option when deploying?

Here are the 2 errors:

User-added image
Rachel Linder 8Rachel Linder 8
I also believe it triggered and error with a process flow.

 User-added image
Rachel Linder 8Rachel Linder 8
If anyone can point me iin the right direction to get this deployed it would be helpful.
bainesybainesy
Creating the test opportunity did not help. For some reason, the code on line #66 is not finding the opportunity you created. In all honesty, your test classes should be rewritten to create test data rather than using (and depending on) records exiting in SFDC.  Can you pull down the actual source code and paste in here rather than images?
Rachel Linder 8Rachel Linder 8
I am trying tp pull down the code by hitting the download button and it is putting it in a file format of CLS. What is the best way to pull the code down.
bainesybainesy
Navgiate to the class in Sandbox, click Edit, then copy the code from the editor.
Or, you can open the cls file in a text editor and grab the code from there.
Rachel Linder 8Rachel Linder 8
Here is the code for testclass:

@isTest(SeeAllData=true)
private class TestClass {

    static testMethod void myUnitTest() {
        Profile p = [Select id from Profile where Name ='System Administrator']; 
        Profile EUP = [Select id from Profile where Name ='EU - Standard User w/ Reporting'];
        user u = [select id, ProfileId, UserRoleId,Name, Team__c, Territory__c, Region__c from User where /*Name =: 'Gavin Barber']; //*/ProfileId =: EUP.Id and isActive = true limit 1]; //
        user u2 = [select id, ProfileId, UserRoleId,Name, Team__c, Territory__c, Region__c from User where /*Name =: 'Amy Harrington'];//*/ProfileId =: p.Id and isActive = true limit 1]; //
        
         /*User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');*/
        Date d = System.Today(); 
                
        DateTime dt = system.now();
        date flightstart = date.valueof(dt.addMonths(-3));
        date flightend = date.valueof(dt.addMonths(3));
        string ThisWeek1 = dt.format('w');
        string LastWeek = dt.addDays(-7).format('w');
        integer Lastweeki = integer.valueof((string)Lastweek);
        integer thisweeki = integer.valueof((string)thisweek1);
        string thisyear = dt.format('yyyy');
        string thisMonth = dt.format('MMMM');
        string Quarter = ThisMonth == 'January' ? 'Q1' : ThisMonth == 'February' ? 'Q1' : ThisMonth == 'March' ? 'Q1' : ThisMonth == 'April' ? 'Q2': ThisMonth == 'May' ? 'Q2' : ThisMonth == 'June' ? 'Q2' : ThisMonth == 'July' ? 'Q3' : ThisMonth == 'August' ? 'Q3' : ThisMonth ==  'September' ? 'Q3' : ThisMonth ==  'October' ? 'Q4' : ThisMonth == 'November' ? 'Q4' : ThisMonth == 'December' ? 'Q4' : '' ;
            list<Quota__c> qtalist = new list<Quota__c>();
            Quota__c testqta = new Quota__c (OwnerId = u2.Id, January__c = 100, February__c = 100, March__c = 100, April__c = 100, May__c = 100, June__c = 100, July__c = 100, August__c = 100, September__c = 100, October__c = 100, November__c = 100, December__c = 100, RecordTypeId = '012500000009iOD',Year__c = '2013',Region__c = 'aaa',Office__c = 'aaa',Team__c = 'aaa');
                qtalist.add(testqta);
            Quota__c qta3 = new Quota__c (OwnerId = u2.Id, January__c = 2, February__c = 2, March__c = 2, April__c = 2, May__c = 2, June__c = 2, July__c = 2, August__c = 2, September__c = 2, October__c = 2, November__c = 2, December__c = 2, RecordTypeId = '012500000009iOD',Year__c = '2012',Region__c = 'aaa',Office__c = 'aaa',Team__c = 'aaa');
                qtalist.add(qta3);
            Quota__c qta2 = new Quota__c (name ='default',OwnerId = u2.Id, January__c = 2, February__c = 2, March__c = 2, April__c = 2, May__c = 2, June__c = 2, July__c = 2, August__c = 2, September__c = 2, October__c = 2, November__c = 2, December__c = 2, RecordTypeId = '012500000009iOD',Year__c = '2012',Region__c = 'aaa',Office__c = 'aaa',Team__c = 'aaa');
                qtalist.add(qta2);
            insert qtalist;
         test.startTest();
            list<Account> AdvertiserList = new list<Account>();
            id holdingcompanyrt = utils.getRecordTypeId('Account', 'Holding Company');
            Account HoldingCompany =    new Account(Name = 'Test HoldingCompany', RecordTypeId = holdingcompanyrt, OwnerID = u2.Id);
                insert HoldingCompany;
            id mainagencyrt = utils.getRecordTypeId('Account', 'Main Agency');  
            Account MainAgency = new Account(Name = 'Test Main Agency', RecordTypeId = mainagencyrt,OwnerID = u2.Id,Holding_Company_Name__c = HoldingCompany.Id);  
                insert MainAgency;   
            id LocalAgencyrt = utils.getRecordTypeId('Account','Local Agency');
            Account LocalAgency = new Account(Name = 'Test Local Agency', RecordTypeId = LocalAgencyrt, Main_Agency__c = MainAgency.Id, Holding_Company_Name__c = HoldingCompany.Id, SageID__c ='ABC100');
                insert LocalAgency;
            Account LocalAgency2 = new Account(Name = 'Test Local Agency2', Main_Agency__c = MainAgency.Id ,RecordTypeId = LocalAgencyrt, SageID__c ='ABC100');
                insert LocalAgency2; 
            id Advertiserrt = utils.getRecordTypeId('Account', 'Advertiser');
            Account Advertiser = new Account(Name = 'Test Advertiser', RecordTypeId = Advertiserrt, Local_Agency__c = LocalAgency.Id );
                insert Advertiser;
                Advertiser.BillingState = 'Idaho';
                Advertiser.BillingCountry = 'United States';
                Advertiser.Create_Sage__c = true;
                Advertiser.SageID__c = '';
                Advertiser.Main_Agency__c = null;
                    update Advertiser;  
                Advertiser.BillingState = '';
                    update Advertiser;
                Advertiser.SageID__c ='100ABC';
                    update Advertiser;  
           test.stopTest();
            
                    
    }
    static testmethod void testmethod2() {
        test.startTest();   
        Opportunity opp = [select id from Opportunity where Name IN('test') and CloseDate >: system.today()-60 and Sales_Stage_High_Level__c =: 'Open' and HasOpportunityLineItem =: true limit 1];
         
            opp.move_all_flight_dates__c = true;
            opp.ContractStartDate__c = system.Today();
            opp.ContractEndDate__c = system.Today()+90;
            update opp;
        test.stopTest();
    }       
    static testmethod void testmethod5() {
        test.startTest();    
        Opportunity opp = [select id from Opportunity where Name IN('test') and Sales_Stage_High_Level__c =: 'Open' and HasOpportunityLineItem =: true limit 1];

            opp.StageName = 'Closed Won';
            opp.HasSageID__c = false;
            opp.Exception__c = false;
            opp.SVP_Email__c = null;
            
            update opp;
            delete opp;
        test.stopTest();
    }       
    static testmethod void testmethod4(){
        test.startTest();
        Opportunity opp = [select id from Opportunity where Name IN('test') and Sales_Stage_High_Level__c =: 'Open' limit 1];
        OpportunityTeamMember ot = new OpportunityTeamMember(UserId = '005500000018PC2', TeamMemberRole='Account Manager',Split__c = 50,Date_Split_Starts__c = system.Today(),Date_Split_Ends__c=system.Today()+30, OpportunityId = opp.Id);
            insert ot;
        test.stopTest();
    }
    static testmethod void testmethod6(){
         test.startTest();  
       /*    Opportunity opp = [select id from Opportunity where Name IN('test') and Sales_Stage_High_Level__c =: 'Open' and HasOpportunityLineItem =: false limit 1];
           Product2 prod = new Product2(name='test product',CanUseRevenueSchedule=True);
                insert prod;  
           PricebookEntry pbe = new PricebookEntry(UnitPrice=0, UseStandardPrice=true,Product2Id=prod.id, Pricebook2Id = '01s50000000IJr3',IsActive=true);
                insert pbe;                 
           OpportunityLineItem oliUS = new OpportunityLineItem(UnitPrice=0,Quantity=1,PricebookEntryId=pbe.Id ,OpportunityId=opp.id, Placement__c = 'aaa', Flight_Start_Date__c =system.today()-1,Flight_End_Date__c = system.today()+30, Rev_Type__c = 'CPM',Rate__c = 1, insertbudget__c = 600, distribute_evenly__c = true );
                 insert oliUS;*/
         test.stopTest();
    }
        static testmethod void testmethod7(){
        test.startTest();
        
        Opportunity opp = [select id from Opportunity where Name = 'The Testing Opportunity' limit 1];

        update opp;
        opp.StageName = 'Closed Won';
        update opp;
        delete opp;
        test.stopTest();    

    }
    static testmethod void testmethod3(){
        PageReference pageref = new Pagereference('apex/Account_Transfer2');
        test.setcurrentpage(pageRef);
        
        //Instantiate the standard controller
        Account act = new Account();
        ApexPages.StandardController sc = new Apexpages.standardController(act);
        
        //Instantiate the extension
        //Account_Transfer2 ext = new Account_Transfer2(sc);
        
        //Do the work
        //ext.getEmps();
        //ext.del();
        //ext.onSave();
        //ext.AccountWork();
        //ext.NextPage();
        //ext.Opportunity_Transfer();
    }
    
}
Rachel Linder 8Rachel Linder 8
Here is the second one which is from an installed package:

public class test_updatecontactrolecount
{
 public static testMethod void testcreateopptywithconditionandrole()
{
//Insert Opportunities 
try
{
    Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Stage 1 - Unqualified Prospect',Type ='New Business', CloseDate=System.Today());
    insert Oppty;
    
    // insert contact
    Contact[] cont = new Contact[]
    {
        new Contact(LastName = 'testcontact1'),
        new Contact(LastName = 'testcontact2')
    };    
    insert cont;    
    // insert contact role     
    OpportunityContactRole [] ocr = new OpportunityContactRole[]
    {
    new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = True),
    new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
    };
    insert ocr;    
    Oppty.StageName = 'Stage 3 - Eval Request';    
    //Update opportunity
    
    Test.StartTest();
    update Oppty;
    Test.StopTest();
    
    Oppty =[SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
    system.assert (Oppty.Number_of_Contacts_Roles_Assigned__c == 2);
    system.assert (Oppty.Primary_Contact_Assigned__c == True);
}
catch (System.DmlException e)
{
    System.assert(false);
}        

}
bainesybainesy
Try replacing lines 64-73 in TestClass with this:
static testmethod void testmethod2() {
        test.startTest();

        Oportunity opp = new Opportunity();
        opp.Name = 'test';
        opp.Sales_Stage_High_Level__c = 'Open';
        opp.CloseDate = System.today() - 60;
        insert opp;

        //Opportunity opp = [select id from Opportunity where Name IN('test') and CloseDate >: system.today()-60 and Sales_Stage_High_Level__c =: 'Open' and HasOpportunityLineItem =: true limit 1];
         
        opp.move_all_flight_dates__c = true;
        opp.ContractStartDate__c = system.Today();
        opp.ContractEndDate__c = system.Today()+90;
        update opp;

        test.stopTest();
    }

 
bainesybainesy
For the second one...
One of your inserts in your test is failing, so the code is throwing an exception which is causing the test to fail. Try replacing the code with this. It will provide a few more details as to what the failure is.
 
public class test_updatecontactrolecount
{
    public static testMethod void testcreateopptywithconditionandrole() {
        //Insert Opportunities 
        try
        {
            Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Stage 1 - Unqualified Prospect',Type ='New Business', CloseDate=System.Today());
            insert Oppty;
            
            // insert contact
            Contact[] cont = new Contact[]
            {
                new Contact(LastName = 'testcontact1'),
                new Contact(LastName = 'testcontact2')
            };    
            insert cont;

            //ensure the contacts were inserted correctly
            foreach (Contact contact : cont) {
                System.assert(contact.Id != null);
            }  

            // insert contact role     
            OpportunityContactRole [] ocr = new OpportunityContactRole[]
            {
                new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = True),
                new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
            };
            insert ocr;    

            foreach (OpportunityContactRole oc : ocr) {
                System.assert(oc.Id != null);
            }

            Oppty.StageName = 'Stage 3 - Eval Request';    
            //Update opportunity
            
            Test.StartTest();
            update Oppty;
            Test.StopTest();
            
            Opportunity updatedOpp = [SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
            system.assert (updatedOpp.Number_of_Contacts_Roles_Assigned__c == 2);
            system.assert (updatedOpp.Primary_Contact_Assigned__c == True);
        }
        catch (System.DmlException e)
        {
            System.assert(false);
        }      
    } 
}