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
Alexa KaiserAlexa Kaiser 

Having trouble with my Test Class on Auto-Opp creating trigger

Hi,

First bear with me, I'm an admin learning Apex.

I created a trigger that creates an Opportunity once an Account is created, it's working great but im struggling with the test class.

User-added image

I realize I have an issues with line 5, but im unsure how to insert the date fields into the test account that will then pull through to the new test opportunity (I tried to remove them and got an error that Variable did not exist).  
User-added image

Thoughts?  Help? 

 
Best Answer chosen by Alexa Kaiser
karthikeyan perumalkarthikeyan perumal
Hello, 

Change your 2 lines of code in your Test class. the date format  which you assigned is not correct @ line 5,6.


If your Field is Date DataType
 
acc.Original_Trip_Inquiry_Start_date__c=Date.newInstance(2016, 1, 1);
acc.Original_Trip_Inquiry_End_date__c=Date.newInstance(2016, 1, 20);

Or IF your Field is DateTime DataType
 
acc.Original_Trip_Inquiry_Start_date__c=datetime.newInstance(2016, 1, 1, 0, 0, 0);
acc.Original_Trip_Inquiry_End_date__c=datetime.newInstance(2016, 1, 20, 0, 0, 0);

its should work. 

Mark Best ANSWER if its works for you. 

Thanks
karthik
 

All Answers

Alain CabonAlain Cabon
Hi,

1) a trick for indenting your code easily: select all your code (CTRL + A) and fix the indentation (Edit > Fix Indentation ( SHIFT + TAB ))
2) post your code here with the button "< >" (Add  a code sample) so other people can copy/paste it.
3) a date is created like that in Apex:  Date myDate = Date.newInstance(2016, 1, 20) (or Date myDate = Date.today(); )
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Alain
karthikeyan perumalkarthikeyan perumal
Hello, 

Change your 2 lines of code in your Test class. the date format  which you assigned is not correct @ line 5,6.


If your Field is Date DataType
 
acc.Original_Trip_Inquiry_Start_date__c=Date.newInstance(2016, 1, 1);
acc.Original_Trip_Inquiry_End_date__c=Date.newInstance(2016, 1, 20);

Or IF your Field is DateTime DataType
 
acc.Original_Trip_Inquiry_Start_date__c=datetime.newInstance(2016, 1, 1, 0, 0, 0);
acc.Original_Trip_Inquiry_End_date__c=datetime.newInstance(2016, 1, 20, 0, 0, 0);

its should work. 

Mark Best ANSWER if its works for you. 

Thanks
karthik
 
This was selected as the best answer
Alexa KaiserAlexa Kaiser
Reposting my code, I dont think I need the dates in theory right?  I just need to opp to test an opp creation and see if theres a failure issue.  I took the dates out and now have 100% coverage.

My concern is the trigger works with the date mapping I added but will the test class cause any issues?

Test clas
 
@isTest
public class TEST2AutoOpp  {
    static testMethod void insertNewOpportunity() {
        Account acct = New Account(Name='Test Account');
        insert acct;
        
        
        Opportunity opp = new Opportunity();
        opp.Name= 'Test';
        opp.RecordTypeId  = '0121a0000001nPRAAY';
        opp.StageName   = 'Unassigned';
        opp.OwnerId = '0051a000001jvogAAA';
        opp.CloseDate   = Date.today() + 60;
        insert opp;
    }
}

Trigger
// On insert of a new account an auto opp created to create first trip
trigger AutoOpp on Account(after insert) {
  List<Opportunity> newOpps = new List<Opportunity>();

//For loop to enter the Opportunity info
    for (Account acc : Trigger.new) {
    Opportunity opp = new Opportunity();
    opp.Name        = acc.name +'-'+'New trip';
    opp.RecordTypeId  = '0121a0000001nPRAAY';
    opp.StageName   = 'Unassigned';
    opp.OwnerId = '0051a000001jvogAAA';
     opp.CloseDate   = Date.today() + 60;
    opp.Trip_Start__c   = acc.Original_Trip_Inquiry_Start_Date__c;
    opp.Trip_End__c   = acc.Original_Trip_Inquiry_End_Date__c;   
    opp.AccountId = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
  }
  //DML operation.
    insert newOpps;
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

If you got 100 % code coverage then no issues in your test class. Go head and deply your code in to production. it will work if your logic is perfectly correct. 

Hope this will help you. 

Thanks
karthik
 
Alain CabonAlain Cabon
It is not advisable to use Id values (not easy to read and verify).

You have to verify if these values also exist in production.
select id,name,SobjectType from recordtype where id = '0121a0000001nPRAAY'
select id , firstname , lastname from user where id = '0051a000001jvogAAA'

opp.RecordTypeId = '0121a0000001nPRAAY';
opp.OwnerId = '0051a000001jvogAAA';

Beautifying your code : select all your code (CTRL + A) and fix the indentation (Edit > Fix Indentation ( SHIFT + TAB ))

File headers should include (comments) /*   */
– Description of purpose.
– Names / emails of the author
– Date of creation, and last modification.
– Related metadata (eg. classes, triggers, objects, VF pages).

Regards
Alain