[SalesForce] Insert Parent and multiple child record with same vf page

My query is when I input account details after that input contact detail and click on the save button its not save given error is

Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [AccountId]*

controller:-

public class AccountContactController{
public Account acc{get;set;}
public List<Contact> cont{get;set;}
public AccountContactController(){
    acc = new Account();
    cont = new List<Contact>();
    AddRow();
    AddRow();
    AddRow();

}
public void AddRow(){
    cont.add(new Contact());

}
public void save(){
    if(acc.name != null){
        insert acc;
        List<contact> con = new List<contact>();
        for(Contact c : cont){
            c.AccountId = acc.id;
            con.add(c);

        }
        if(con != null){
            insert con;
        }
    }

}}

—-vfpage—–

  <apex:page controller="AccountContactController">
  <apex:form >
      <apex:pageBlock >
        <apex:pageblocksection columns="2">
        <apex:inputField value="{!acc.name}"/>
        <apex:inputField value="{!acc.phone}"/>
        <apex:inputField value="{!acc.website}"/>
        <apex:inputField value="{!acc.Sic}"/>

    </apex:pageblocksection>      
    <apex:pageBlockSection columns="1">
        <apex:pageBlockTable value="{!cont}" var="contacts">
            <apex:column headerValue="First Name">
                <apex:inputField value="{!contacts.firstname}"/>
            </apex:column>
            <apex:column headerValue="Last Name">
                <apex:inputField value="{!contacts.lastname}"/>
            </apex:column>
            <apex:column headerValue="Email">
                <apex:inputField value="{!contacts.email}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
   <apex:pageBlockButtons >
    <apex:commandButton value="Save" action="{!save}"/>
    <apex:commandButton value="Add Row" action="{!AddRow}" immediate="true"/>
    </apex:pageBlockButtons>

  </apex:pageBlock> 

Best Answer

Your code is correct. We get this error when we have lookup filter enable. So check if you have lookup filter enable on contact for Account or you have second lookup related to Account which have lookup filter enable.

Just check the condition and update the Account record to match the condition it will solve your problem.

Related Topic