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
WikWik 

Displaying Error on VF Page

HI ,

Following is my trigger:

trigger Duplicate on Contact (before insert, before update) {
    set<Id> accIds = new set<Id>();

    for(Contact p :trigger.new){
        accIds.add(p.accountId);
    }
    map<Id,account> parentAccmap = new map<Id,account>([select Id,parentId,recordtypeId from account where Id IN :accIds and parentId!=NULL and recordtypeId='012600000005J5J']); //add Site record type Id here.

    set<ID> parentIds = new set<ID>();
    for(Account acc : parentAccmap.values()){
        parentIds.add(acc.parentId);
    }
    
    map<Id,Set<String>> parentPrMap = new map<Id,Set<String>>();
    list<Contact> prParentlist = [select ID,Email,Name,AccountId, Account.recordtypeId from Contact where AccountId IN:parentIDs and Account.recordtypeId='012600000005J5I']; //add Customer record type Id here.
    for(Contact pr:prParentlist){
        if(parentPrMap.get(pr.AccountId) == null){
            parentPrMap.put(pr.AccountId, new Set<String>());
        }
        parentPrMap.get(pr.AccountId).add(pr.email);
    }

    list<Contact> prParentlist2 = [select ID,Email,Name,account.parentid, Account.recordtypeId from Contact where account.parentid in :parentIDs and Account.recordtypeId='012600000005J5I']; //add Customer record type Id here.
    for(Contact pr:prParentlist2){
        if(parentPrMap.get(pr.account.parentid) == null){
            parentPrMap.put(pr.account.parentid, new Set<String>());
        }
        parentPrMap.get(pr.account.parentid).add(pr.email);
    }
    system.debug(parentPrMap);
    
    for(Contact prr:trigger.new){
        if(
            prr.email!=null
            && parentAccmap.containsKey(prr.Accountid)
            && parentPrMap.containsKey(parentAccmap.get(prr.Accountid).ParentId)
        ){
            if(parentPrMap.get(parentAccmap.get(prr.Accountid).ParentId).contains(prr.email)){
                String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
                string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ prr.accountid +'"> Please Create a Contact Role  </a>';
                prr.addError('This Contact already exists.'+ errorMsg,false);
            }
        }   
    }
}

I have written a vf page to display error:


<apex:page standardController="Contact" recordSetVar="contacts" tabStyle="Contact" sidebar="false">
<p>Error.</p>
<apex:form >
<apex:pageBlock > <apex:pageMessage summary="This pageMessage will always display." severity="warning" strength="3" /> <apex:pageMessages />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!contacts}" var="con">
<apex:column value="{!con.name}"/>
<apex:column headerValue="Name">
<apex:outputField value="{!con.Email}"/>
<apex:pageMessages id="errorMessage">
</apex:pageMessages>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

I am new to apex, can some one help me with linking them and displaying the trigger error on vf page.

Thank You
SonamSonam (Salesforce Developers) 
If I understand this correctly, you wish to add the error messages you are generating in the controller to the VF page.

You can use:apex:pageMessages tag to display the error message on the VF.
Sample code on the link below:
https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_pageMessages.htm