+ Start a Discussion
dfiredfire 

Detail won't accept Master Id - DMLException

I have a PersonAccount Student which has a Detail custom Object Date, which is a list of the date the student entered and left. I create the student and insert it no problem. But when I try to add the student id which is the Master to the Date's student field and insert, I get the following DML Exception error:


System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Student: id value of incorrect type: 001T000000h4YlVIAU: [Student__c]

 

Here is the controller:

public with sharing class studentApplicationController {

    // Non-Object VF Fields
    public String lastname { get; set; }
    public String firstName { get; set; }
    public String salutation { get; set; }

    // Object Variable
    Account student;
    Date__c startdate;
    RelativeContact__c spouse;
    RelativeContact__c father;
    RelativeContact__c mother;
    List<Educational_Institution__c> schools;
    
    // Other Variables
    String submittedPageURL = '';
    
    // util methods
    RecordType getPersonAccountRecordTypeId() {
        return [select id,name,sobjectType,ispersontype from recordType where ispersontype=true and sobjectType='account' limit 1];
    }
    
    // Getters
    public Account getStudent() {
        if(student == null) {
            RecordType recType = getPersonAccountRecordTypeId();            
            student = new Account(recordtypeid=recType.id);
            //student = new Account();
        }
        return student;
    }
 
   
    public Date__c getStartDate() {
        if(startdate == null) {
            startdate = new Date__c(status__c='Entered - Non-Academic');
        }    
        return startdate;
    }   
     
    
    // Save the application into Salesforce
    public PageReference save() {
        
        student.lastname = lastname;
        student.firstname = firstname;
        student.salutation = salutation;
        insert student;
        
        System.debug('Student Id is: '+student.id);
        startdate.student__c = student.id;
        insert startdate;
        
        PageReference submittedPage = new PageReference(submittedPageURL);
        submittedPage.setRedirect(true);
        return submittedPage;
    }
}

 

 

According to the debug, the student does have a valid id, so it's not a null value issue.

 

Here is the test code:

 

 

@isTest
private class StudentApplicationControllerTest {

    static testMethod void testController() {
        studentApplicationController sac = new StudentApplicationController();
        sac.getStudent();
        sac.firstName = 'Unit';
        sac.lastname = 'Test';
        sac.salutation = 'Mr.';
        
        sac.getStartDate().Date__c = Date.newInstance(2, 25, 2011);
        
        sac.save();
    }
}

 

 

 

I get the same error when trying from my VF page.

 

I am not using queues. But the Account is a person account.

 

Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
dfiredfire

I discovered the solution. I was assigning the person account acount id, when I needed to assign the person account contact id, as the master-detail was to the contact side of the person account.

 

I have a detailed account of the solution on the Visualforce board at

http://boards.developerforce.com/t5/Visualforce-Development/Detail-won-t-accept-Master-Id-DMLException/m-p/251061/highlight/false#M32724

 

Please see that post for some pointers and code samples I put up when using person accounts.

All Answers

sfelfsfelf

What is the type of the student__c field in the Date__c object?

dfiredfire

Date__c.student__c is a master-detail field to student which is a person account. I figured for a m-d or lookup field I just set the id of the record I want in the master or lookup field as the value of the field in the detail, but maybe not.

 

So how do I set a master field in the detail in apex?

 

Thanks for the reply.

dfiredfire

I discovered the solution. I was assigning the person account acount id, when I needed to assign the person account contact id, as the master-detail was to the contact side of the person account.

 

I have a detailed account of the solution on the Visualforce board at

http://boards.developerforce.com/t5/Visualforce-Development/Detail-won-t-accept-Master-Id-DMLException/m-p/251061/highlight/false#M32724

 

Please see that post for some pointers and code samples I put up when using person accounts.

This was selected as the best answer