function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John Neilan 18John Neilan 18 

Visualforce Page Duplicate Records

Hi,

I have a pretty simple VF page that allows a user to create 1 or more records of a custom object that has master-detail relationships to the Account and Contact objects. The user can enter a Type value for the custom object, but each related Contact can only have 1 record of each Type value. Type values on the custom object can be repeated on different Contacts.  

What I'm trying to do is highlight the Type values the user is choosing to create, that already exist for that Contact so they can then change the value or remove it. Is there a way to accomplish this?

VF Page
<apex:page Controller="NewRecController">
<apex:form >
    <apex:pageBlock >
        <apex:pageMessages id="showMsg"/>
        <apex:pageBlockTable value="{!listRecType}" var="rec1" >
            <apex:column headerValue="Record Type">
                <apex:inputField value="{!rec1.Rec_Type__c}"/>
            </apex:column>
        </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton value="Add Another Record Type" action="{!addRecType}"/>
            <apex:commandButton value="Save All Record Types" action="{!saveType}" reRender="showMsg"/>
        </apex:pageBlockButtons>
        </apex:pageBlock>
</apex:form>
</apex:page>

Controller
public class NewRecController {

    Record_Type__c rec = new Record_Type__c();
    public list<Record_Type__c> listRecType{ get; set; }

    Id acctId;
    Id contId;
    String RecTypes;
    
//    Constructor 

	public NewRecController() { 
        acctId = ApexPages.currentPage().getParameters().get('acctId');
        contId = ApexPages.currentPage().getParameters().get('contId');
        RecTypes = ApexPages.currentPage().getParameters().get('acctTypes');
        
        rec.Account__c = acctId;
        rec.Contact__c = contId;
system.debug('@@@@@@ - AccountTypes: '+acctTypes);

        listRecType = new list<Record_Type__c>();
        listRecType.add(rec);
	} 

    Public void addRecType() {
        Record_Type__c rec1 = new Record_Type__c();
    		rec1.Contact__c = contId;
        	rec1.Account__c = acctId;
system.debug('@@@@@@ - AccountTypes: '+acctTypes);
system.debug('@@@@@@ - Record Type: '+rec1);
		listRecType.add(rec1);
        }
            
    public PageReference saveType() {
    	insert listRecType;
    return Page.NewRecType;
    }
}


 
ShirishaShirisha (Salesforce Developers) 
Hi John,

Greetings!

Can you please check the controller code in the below thread to avoid duplicates in the list when working with the Visualforce pages.

https://salesforce.stackexchange.com/questions/84063/avoid-duplicates-entry-in-the-list-visualforce-page-controller

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
John Neilan 18John Neilan 18
Thanks Shirisha. I'm not trying to avoid duplicates. There is a trigger on the custom onject that writes data back to the Account and Contact that also throws an error if there are duplicates. I'm trying to highlight to the user that a selection they chose will be flagged as a duplicate before it even gets to that point.
raty singhraty singh