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
❤Code❤Code 

test class failing

Hi,

My test class is failing saying insert failed at line 15. Can anybody point where i am doing the mistake.

@isTest(SeeAllData= false)
private class testClass
{
    static testMethod void test()
    {
        Test.StartTest();
        Id accountRecordTypeId = Schema.SObjectType.Project__c.getRecordTypeInfosByName().get('BSG').getRecordTypeId();
        Project__c proj = new Project__c();
        proj.RecordTypeId= accountRecordTypeId ;
        proj.Name='Test Proj';
        proj.Department_Name__c='BSG';
        proj.AXA_BS_Project_Sponsor__c='00511000002WNYjAAO';
        proj.Start_Date__c=system.today();
        proj.End_Date__c=system.today();
        insert proj;
        Project_Report ipmo = new Project_Report ();
        ipmo.FindAllName();
        ipmo.doSearch();
       
        Test.stopTest();
        
    }
}
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Which error are you getting?

Regards.
❤Code❤Code
At   insert proj;

Regards
AjayDubediAjayDubedi
Hi, There could be any number of reason why this might fail
  • Validations on the Project Object migh not be allowing a record entry
  • Mandatory fields being missed out in Project object intitialisation.
  • You should also check if the Record Type "BSG" exists.
  • Any of the following fields might have a rule?
    •         proj.Name='Test Proj';
    •         proj.Department_Name__c='BSG';
    •         proj.AXA_BS_Project_Sponsor__c='00511000002WNYjAAO'; // We should avoid hardcoding the record Ids like this in Test class.
    •         proj.Start_Date__c=system.today();
    •         proj.End_Date__c=system.today();
Finally without the HINT (Error message) it would be better to do a gothrough of salesforce Test classes Best practice. (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm)
❤Code❤Code
Hi Ajay,
I have covered the all the 5 mandatory fields. Record Type exists.I am having admin rights.
Except all these fields there is another non-mandatory field which is a formula field and it is populated according record type selected + 1 trigger(Which i have inactivated.)
Is this might cause this issue.

I am getting inter failed error message.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

What is the message (description) are you getting? 

Regards.
❤Code❤Code

I am getting insert failed error message at line 15
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Yeah, I understood that but what is the error message? Usually when an insert error occours it comes followed by an additional information message that helps you to identify what is going wrong.

E.g:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateElevateSurveyResponded: execution of AfterInsert

Regards.
 
AjayDubediAjayDubedi
Hi, I understand that while running the Test class cia "Run All Test" you could get a detailled reason why you are getting this error message. Still I have few suggestions: 
  1. If you are trying this test code run in sandbox or your personal developer Org or in any salesforce org where you can play around or do the development. Then please take the piece of code within test class and run it in Execute anynoumous.  (https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_execute_anonymous.htm&language=en) and then you can find out whether it fails there or not and the reason behind it. DONT TRY THIS ON LIVE PRODUCTION.
  2. Try to do a Run the test class using a User context (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm). May be the usercontext from which you are running this is not having permission to insert a record on pRoject object.
I think if you try to dig a bit deeper into this you will find the solution yourself.
AjayDubediAjayDubedi
Hi sdfc1.buddy Do let me know if this was resolved and vote the best answer please.