Salesforce:基于VisualForce创建外部用户可提交的DormsApplicants__c表单
实现外部用户填写的VisualForce页面创建DormsApplicants__c记录
Got it, let's walk through exactly how to build this VisualForce page that lets external users (like your customers) submit a new DormsApplicants__c record using just the First_name__c field. Since you already have Site.com set up, we’ll focus on the core VF page, controller logic, and critical configuration checks to make this work seamlessly.
1. 自定义Apex控制器(核心逻辑)
External users (Site.com guest users) can’t use standard controllers for record creation without proper permissions, so we’ll build a simple custom controller to handle the insert logic and error handling.
public class DormsApplicantController { // 初始化一个新的DormsApplicants__c记录 public DormsApplicants__c applicant {get; set;} public DormsApplicantController() { applicant = new DormsApplicants__c(); } // 处理表单提交与记录创建 public PageReference saveApplicant() { try { insert applicant; // 提交成功后跳转到感谢页面(替换成你的Site.com页面或其他VF页面) return Page.ThankYouPage; } catch (DmlException e) { // 捕获并向用户展示错误信息 ApexPages.addMessages(e); return null; } } }
2. VisualForce页面(用户界面)
这个页面针对外部用户做了简化(去掉Salesforce头部/侧边栏),添加了基础样式提升友好度,同时包含表单验证和错误提示功能。
<apex:page controller="DormsApplicantController" showHeader="false" sidebar="false" standardStylesheets="true"> <style> .form-container { max-width: 500px; margin: 2rem auto; padding: 2rem; border: 1px solid #ddd; border-radius: 8px; font-family: Arial, sans-serif; } .form-group { margin-bottom: 1.5rem; } label { display: block; margin-bottom: 0.5rem; font-weight: 600; color: #333; } input[type="text"] { width: 100%; padding: 0.75rem; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } .submit-btn { background-color: #0070d2; color: white; border: none; padding: 0.75rem 1.5rem; border-radius: 4px; cursor: pointer; font-size: 1rem; } .submit-btn:hover { background-color: #005fb2; } .error-message { color: #dc3545; margin-top: 0.5rem; font-size: 0.9rem; } </style> <div class="form-container"> <h2>宿舍申请信息填写</h2> <!-- 展示提交过程中的错误信息 --> <apex:messages styleClass="error-message" /> <apex:form> <div class="form-group"> <label for="firstName">姓名</label> <apex:inputField value="{!applicant.First_name__c}" id="firstName" required="true" /> </div> <apex:commandButton action="{!saveApplicant}" value="提交申请" styleClass="submit-btn" /> </apex:form> </div> </apex:page>
3. Site.com关键配置检查
要让外部用户能访问页面并创建记录,你需要更新以下权限设置:
- 对象与字段权限:进入Site.com来宾用户配置文件,确保它拥有
DormsApplicants__c对象的创建权限,以及First_name__c字段的编辑权限。 - Apex控制器访问权限:将
DormsApplicantController类添加到来宾用户配置文件的启用的Apex类访问列表中。 - 页面授权:在Site.com站点设置中,将这个VisualForce页面添加到启用的Visualforce页面列表里。
- 感谢页面(可选):如果用了跳转的感谢页面,也要确保该页面被授权给来宾用户访问。
4. 测试流程
- 在隐身窗口打开你的Site.com站点公开URL,模拟外部用户访问。
- 输入姓名并提交表单。
- 查看Salesforce组织中的
DormsApplicants__c列表视图,确认新记录已创建。 - 测试边界情况:留空姓名字段验证必填项规则是否生效,检查错误提示是否正常显示。
内容的提问来源于stack exchange,提问作者user3617337
相关产品推荐
相关产品推荐

