1 year ago

#312845

test-img

Nitin Mane

How to show error message on Visualforce Page when create records based on Condition

Project Requirement:-create new record when candidate's PAN is not blacklisted, if it is blacklisted then show warning message on visualforce page and update new phone number in blacklisted candidate object.

Pre exquisite:- i have two object 1.Sudent_matster__c : Fields:- Name, Phone__c, PAN__c, email__c etc 2.Black_Listed_Candidate__c Fields:- Name, Phone__c, PAN__c etc

QUESTION When i enter blacklisted field PAN- FHSJF10387 then this code runs perfectly fine, but i enter another blacklisted field PAN- DKFIT8888S,CNYY78912Q, RYHJI997Q.etc. then code wont work. why? i think old value should be cleared from memory?

Visual force page IMAGE [This is visualforce page ][1] [1]: https://i.stack.imgur.com/aLzjo.png

VFP CODE

<apex:page Controller="Class_Practice">
<apex:form id="frm" html-autocomplete="off" style="border-style:solid;border-width:2px;border-color:black;background-color:lightyellow">
   <H1 style="text-align:center;font-size:30px;">PAN DATA CHECK</H1>
   <apex:pageBlock >
         <apex:pageMessages></apex:pageMessages>
        <apex:pageBlockSection title="My Content Section" columns="2">
            <apex:inputField value="{!newStudent.Name}"/>
            <apex:inputField value="{!newStudent.First_Name__c}"/>
            <apex:inputField value="{!newStudent.Last_Name__c}"/>
            <apex:inputField value="{!newStudent.Email__c}"/>
            <apex:inputField value="{!newStudent.Phone__c}"/>   
            <apex:inputField value="{!newStudent.PAN__c}"/>   
            <Apex:commandButton value="Save" Action="{!MySave}"/>
       </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

APEX CLASS Code

public class Class_Practice
{   
public Student_Master__c newStudent {get;set;}  
Map<Id, string> IdAndBlackListPhoneMap = New Map<Id, string>();
public static list< Black_Listed_Candidate__c> blackList =[SELECT Name,PAN__c,Phone__c FROM Black_Listed_Candidate__c];

public Class_Practice()
 {
     newStudent = new Student_Master__c();
     
 }

public pageReference MySave() 
{   
    for(Black_Listed_Candidate__c bp:blacklist)
    { 
        if (bp.PAN__c==newStudent.PAN__c ) 
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'PAN is Black Listed'));
            bp.Phone__c = newStudent.Phone__c;
            IdAndBlackListPhoneMap.put(bp.Id, bp.Phone__c);
            Update blacklist;
            return null;
        }
        else
        {
            insert newStudent;
            PageReference pg = new PageReference('/' + newStudent.Id);
            pg.setRedirect(true);
            return pg;
            }
    }
    return null;
}
}

THANK YOU

salesforce

visualforce

salesforce-lightning

salesforce-communities

0 Answers

Your Answer

Accepted video resources