1

Getting error message when I'm running the test don't know what's wrong with this code

this is the error msg: System.StringException: Invalid id: recordtype.id

Stack Trace: Class.LeadEventCreation.dosave: line 13, column 1 
             Class.LeadEventCreation.test_BathSolution: line 50, column 1


 public with sharing class LeadEventCreation 
{
    public PageReference dosave() 
    {
        if(!Test.isRunningTest())
        insert objlead;
        objlead.recordTypeId = apexpages.currentPage().getparameters().get('id'); // Line 13,column 1
        if(!Test.isRunningTest())
        update objlead;

        objevent.whoId = objlead.id;
        if(!Test.isRunningTest())
        insert objevent;  
        return (new pagereference('/'+objlead.id).setredirect(true));  
    }
    public Lead objlead { get; set; }
    public Event objevent { get; set; }
    public LeadEventCreation ()
    {
        objlead = new Lead();
        objevent = new Event();
    }

    private static testmethod void test_BathSolution()
    {
        test.startTest();

                    Lead objlead = new lead();
                    objlead.LastName= 'test name';
                    objlead.Response_Channel__c = 'Email';
                    objlead.Status = 'Hoax';
                    objlead.Products__c = 'Shower';
                    objlead.leadSource = 'Website';
                    objlead.Geo_Code__c = 'North';
                    objlead.Product_Type__c='Walk in';
                    objlead.Medium__c = 'All The Best';
                    objlead.DoNotCall = False;
                database.insert(objlead,false);
                RecordType recordtype = [select id,name,sobjecttype from RecordType where sobjecttype = 'Lead' limit 1];
                    objlead.RecordTypeId = recordtype.id;
                database.update(objlead,false);

                LeadEventCreation Bsolution= new LeadEventCreation  ();
                ApexPages.currentPage().getParameters().put('id','recordtype.id');// line 50, Column 1
                Bsolution.doSave();
        test.stoptest();        
   }
}
3
  • you're passing in 'recordtype.id' as a literal string on the line before Bsolution.doSave(); it should be recordtype.id without the surrounding ' Nov 30, 2012 at 11:32
  • after removing the string i getting new error msg System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must review the marketing preferences section before you can save this record: []
    – rakesh
    Nov 30, 2012 at 11:53
  • 1
    i have a sense of déjà vu here...didn't we go through this here: salesforce.stackexchange.com/questions/4853/… ? Nov 30, 2012 at 12:03

1 Answer 1

0

Your insert statements only execute when you are not running a test due to the !Test.isRunningTest() conditional. So, when you run a test the ID field will never get set as well as anything else that happens with an insert will never happen.

I suggest removing those and then do the following in your test method:

  1. Create, but do not insert the records needed in your controller for insert.

  2. Set the controller's properties to those objects, e.g., controller.objLead = newRec, where objLead is a public Lead property.

  3. Remove the !Test.isRunningTest() conditionals.

This will cause the properties to exist when the insert statement inside of your doSave method are called. Also make sure that the API version of your test class is at least 24 (it probably is if you just created it), because API version 24+ has no access to the org's actual data by default.

Unit tests in Salesforce require you to set up the data manually as part of each unit test. See Introduction to Apex test Methods and Unit Testing Custom Controller and Extensions.

Basically, each one of your unit tests should follow the pattern of setup all test data needed, possibly set variables on controllers, call out to controller methods under test within a Test.startTest() and Test.stopTest() block.

// Generic example
static testMethod void testMyUnitTest() {
    // Set up all data here that your controller will expect.
    Account acct = new Account(Name='Unit Test Account');

    // Testing insert inside of a controller, so don't insert the account.

    // Create the controller and then assign the account.
    MyCustomController controller = new MyCustomController();
    controller.account = acct;

    // Alternatively you could have a controller extension...
    ApexPages.StandardController sc = new ApexPages.StandardController(acct);
    MyAccountControllerExtension extension = new MyAccountControllerExtension (acct);

    Test.startTest();

    controller.doSave();

    Test.stopTest(); 
}

I also suggest reading the SFSE tag wiki for unit-test.

2
  • I just removed the !Test. isRunningTest () conditionals. as a new starter of salesforce i didnt understand the 1st and 2nd point
    – rakesh
    Nov 30, 2012 at 11:47
  • @rakesh - I beefed up the answer a bit. There's a lot of very good documentation for unit tests that I pointed you to. Nov 30, 2012 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .