根据条件创建记录时如何在 Visualforce 页面上显示错误消息

发布于 2025-01-15 12:32:34 字数 2716 浏览 5 评论 0原文

项目需求:-当候选人的 PAN 未列入黑名单时创建新记录,如果已列入黑名单,则在 Visualforce 页面上显示警告消息,并更新列入黑名单的候选人对象中的新电话号码。

精致:- 我有两个对象 1.Sudent_matster__c :字段:- 姓名、电话__c、PAN__c、电子邮件__c 等 2.Black_Listed_Candidate__c 字段:- 姓名、电话__c、PAN__c 等

问题当我输入黑名单字段 PAN- FHSJF10387 时,此代码运行得很好,但我输入了另一个黑名单字段PAN-DKFIT8888S、CNYY78912Q、RYHJI997Q等。那么代码将无法工作。为什么?我认为旧值应该从内存中清除?

Visual Force 页面 IMAGE [这是 Visualforce 页面][1] [1]: https://i.sstatic.net/aLzjo.png

VFP 代码

<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类别代码

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;
}
}

谢谢您

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.sstatic.net/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文