-1

When i run the test class for the below controller extn it is failing with the error

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: : [RecordTypeId]"

I checked the record type access and provided the access to all the opportunity record types to all the users, even after doing this also it failing. please find the code for apex class and test class.

Extension

public class OpptyExtn {
    public Opportunity opp{get;set;}
    public OpptyExtn(ApexPages.StandardController controller){
        opp = new Opportunity();
        string RecorftypeId =      ApexPages.currentPage().getParameters().get('RecordType'); 
        opp.RecordTypeId=RecorftypeId;
    }

    public pagereference save(){
        pagereference p;
        insert opp;

        p= new pagereference('/'+opp.id);
        p.setredirect(true);
        return p;
    }
}

Test Class

@isTest(seeAllData = true)
public class testOpptyExtn  {

    public static testMethod void testMyOpptyExtn() {
        opportunity opp = new opportunity();
        opp.name='test oppty';
        opp.StageName='Closed Won';
        opp.CloseDate=system.ToDay();
        //insert opp;
        OpptyExtn  con = new OpptyExtn (new ApexPages.StandardController(opp));
        con.save();
    }
}

closed as off-topic by Adrian Larson, Ratan Paul, Brian Mansfield, Mohith Shrivastava, Himanshu Feb 23 '16 at 19:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Adrian Larson, Ratan Paul, Brian Mansfield, Mohith Shrivastava, Himanshu
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you checked what you're getting for RecorftypeId and whether that's a valid Opportunity record type? – Nick Cook Feb 23 '16 at 0:45
  • 1
    Do you have a good reason why you are using SeeAllData? You should avoid it at all costs. – Adrian Larson Feb 23 '16 at 0:47
2

In your controller you are reading record type ID from the URL parameter. But in your test method you are not setting the URL parameter. Please set it in the standard controller instance in your test class and check it

  • Thanks for Your reply George. I modified the test class as follows. but even after doing this also getting the same error. Any idea. my modified code is public class OpptyExtn { public Opportunity opp{get;set;} public OpptyExtn(ApexPages.StandardController controller){ opp = new Opportunity(); string RecorftypeId = ApexPages.currentPage().getParameters().get('RecordType'); opp.RecordTypeId=RecorftypeId; } public pagereference save(){ pagereference p; upsert opp; //if(!test.isRunningTest()){ p= new pagereference('/'+opp.id); p.setredirect(true); return p; } } – narendhar reddy Feb 23 '16 at 12:42
  • Please print the record type ID in debug log and make sure that you are getting the record type ID in testclass. Also ensure that the current user ha access to the record type which you are passing into the standard Controller parameter – george thomas Feb 23 '16 at 12:46
0

Your error indicates that you are getting an invalid RecordTypeId from your parameters. You should try something like the following:

public class MyExtension
{
    public static final String RECORDTYPE_PARAM = 'RecordType';
    public Opportunity opportunity { get; private set; }
    public MyExtension(ApexPages.StandardController controller)
    {
        opportunity = (Opportunity)controller.getRecord();
        String recordType = ApexPages.currentPage().getParameters().get(RECORDTYPE_PARAM);
        List<RecordType> candidates = [
            SELECT Id FROM RecordType
            WHERE DeveloperName = :recordType
            AND SObjectType = 'Opportunity'
            LIMIT 1
        ];
        if (!candidates.isEmpty()) opportunity.RecordTypeId = candidates[0].Id;
    }
}

In the above, you can replace DeveloperName with Name. It does not seem based on the error message like you are passing an Id.

When overwriting save, best practice is more along the lines of:

public PageReference save()
{
    try
    {
        insert opportunity;
    }
    catch (DmlException dmx)
    {
        ApexPages.addMessages(dmx);
        return null;
    }
    return new ApexPages.StandardController(opportunity).view();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.