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
RLBJaynesRLBJaynes 

creating an Apex test class help

I'm not a developer and am having a heck of a time creating test coverage. I have created a trigger which automatically creates an opportunity when a custom object meets certain criteria (code below). Other than pointing me to the Apex Classes overview page, would someone be able to help me structure the code to test this?

 

Any help would be SUPER appreciated.

 

trigger Opportunity on Offers_and_Messages__c (after update) {

    list<Opportunity> Addopp = new list<Opportunity>();

    for( Offers_and_Messages__c o : Trigger.new )
    {
      if(o.Business_Days_in_Queue__c == 2 && o.recipient_dealership_stage__c != 'Member')
        {
        Opportunity PA = new Opportunity (
            AccountID = o.Recipient__c,
            OwnerID = '005d0000001VA91AAG',
            Name = o.Name,
            StageName = 'Prospecting',
            CloseDate = o.closedateforopportunity__c,
            LeadSource = 'Non-Member Offer'); 
            Addopp.add(PA);//You have to add it to the list before you insert
            }
        
        insert Addopp;

ManjunathManjunath

 

Hi,

 

try this.

@isTest 
public class testOpportunity {

	public static testmethod void testOpp(){
		Offers_and_Messages__c o = new  Offers_and_Messages__c o (Name='testRec');// while inserting add all the required fields
		insert o;
		
		test.starttest();
		o.Business_Days_in_Queue__c=2;
		o.recipient_dealership_stage__c = 'XYZ';
		Update o;
		test.stoptest();

	}

}

 

 Regards,

RLBJaynesRLBJaynes

Thank you so much for responding back so quickly!! I've edited the section to define any required fields and the problem that I'm running into now is that 3 of the required fields need to actually be Ids of records that already exist. I thought that simply creating a list of the entities and then referring to them in the "new" statement would be enough but the manner in which I've written it (see below) produces an error message: System.StringException: Invalid id

 

Would you mind helping me get past this hurdle too??

 

@isTest
public class testOpportunity {

    public static testmethod void testOpp(){
    
        List<Contact> r =[SELECT id,AccountId FROM Contact limit 1];
       
        List<Account> a2 =[SELECT id FROM Account limit 1];
        
        Offers_and_Messages__c o = new  Offers_and_Messages__c (Name='testRec',Message_Thread_ID__c = '2134fgj90as',Sender__c = 'r.id',Recipient__c = 'a2.id',
        Sender_Dealership__c = 'r.accountid');
        // while inserting add all the required fields
        insert o;
        
        test.starttest();
        o.Business_Days_in_Queue__c=2;
        o.recipient_dealership_stage__c = 'Member';
       
        Update o;
        test.stoptest();

    }

}

RLBJaynesRLBJaynes

I think I actually figured it out on my own!! (I gasped out loud with excitement ...this is literally my second day at doing all of this!!)

 

@isTest
public class testOpportunity {

    public static testmethod void testOpp(){
    
    Account account = new Account();// specify all the required fields
account.name = 'testing 1';
account.customer_number__c = 1234567;
account.GUID__c = 'hjk1234hjkll';
insert account;

Account account2 = new Account();// specify all the required fields
account2.name = 'testing 2';
account2.customer_number__c = 7890123;
account2.GUID__c = 'asd7890asd';
insert account2;

Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
        
        Offers_and_Messages__c o = new  Offers_and_Messages__c (Name='testRec',Message_Thread_ID__c = '2134fgj90as',Sender__c = contact.ID,Recipient__c = account2.ID,
        Sender_Dealership__c = account.ID);
        // while inserting add all the required fields
        insert o;
        
        test.starttest();
   
        o.recipient_dealership_stage__c = 'Member';
       
        Update o;
        test.stoptest();

    }

}