Find your content:

Search form

You are here

What can changes RecordTypeID on insert via Apex using database.DMLOptions()

 
Share

So I have some code that creates a case. The case is assigned a RecordTypeId before the insert.

When I test this, instead of the ID assigned in the code - the RecordTypeId is the default type specified in the user profile.

I have no idea why this is happening. I can see the Assignment Rule assign the case based on the RecordTypeID. this shows the correct ID. However, a few WF_Rule evaluations below the RecordTypeID has changed. There's nothing in between that is changing the RecordTypeID.

So I'm stuck. Only thing I can think of is I may have inadvertently created two instances of the same record which is causing confusion. I'm not sure how that would be happening.

I'm using an Extension of the standard Case Controller and a custom Save method.

Here's some snippets:

private final Case mycase;
public Research_Request_Ext(ApexPages.StandardController controller) {
     this.mycase = (Case) controller.getrecord();
     newCase();
}

public void newCase() {
     RecordType ResearchRecordType = [SELECT Description, DeveloperName, Id, IsActive, Name, SobjectType FROM RecordType WHERE DeveloperName = 'Research_Request'
      AND SobjectType = 'Case'
      and IsActive = true
     ];
     this.mycase.RecordTypeId = ResearchRecordType.id;
}

public PageReference MySave() {
    AssignmentRule AR = new AssignmentRule(); //Assignment Rule Query
    AR = [select id from AssignmentRule where SobjectType = 'Case'
     and Active = true limit 1
     ];
    //Creating DML Options
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    PageReference NewPage = Page.Research_Request;
    string mode = ApexPages.currentPage().getParameters().get('mode');
    boolean ContainsErrors = false;
    system.debug('Saved is being called!');
    if (AR != null) {
        dmlOpts.assignmentRuleHeader.assignmentRuleID = AR.ID;
        mycase.setOptions(dmlOpts);
        mycase.description = mycase.description + '\n On Insert: ' + MyCase.RecordTypeID;
        insert mycase;

        NewPage.getParameters().put('id', mycase.id);
        NewPage.getParameters().put('mode', 'View');
        return NewPage.setRedirect(true)

    }
}

Attribution to: Salesforce Wizard

Possible Suggestion/Solution #1

Thanks to Robert Cruz ‏@Bob_Cruz_ on Twitter for answering this.

The Support Settings for cases has an option to override existing record type with the assignee's default record type.

That was causing the issue. Flipping that to "Keep the existing record type" fixes the problem


Attribution to: Salesforce Wizard
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1665

My Block Status

My Block Content