Salesforce 未添加到页面消息的所有错误

Salesforce 未添加到页面消息的所有错误,salesforce,apex-code,Salesforce,Apex Code,我在我的控制器中抛出一些自定义异常,我注意到当出现多个错误时,这些错误不会一次全部显示出来。而是在页面上一次显示一个。我正在使用pageMessages组件 我认为问题在于控制器如何为所有不同的方法在catch块中添加错误 这是我的班级: public with sharing class CallReportControllerExtension { private Call_Report__c callReport; public Boolean isEditMode {get; set

我在我的控制器中抛出一些自定义异常,我注意到当出现多个错误时,这些错误不会一次全部显示出来。而是在页面上一次显示一个。我正在使用
pageMessages
组件

我认为问题在于控制器如何为所有不同的方法在catch块中添加错误

这是我的班级:

public with sharing class CallReportControllerExtension {

private Call_Report__c callReport;

public Boolean isEditMode {get; set;}
public List<Participants> participantLinesForPage {get; set;}
public List<ProductsPresented> productLinesForPage {get; set;}
public List<Tasks> taskLinesForPage {get; set;}

public CallReportControllerExtension(ApexPages.StandardController stdController) {
    this.callReport = (Call_Report__c)stdController.getRecord();
    isEditMode = isEditPage(ApexPages.currentPage().getParameters().get('save_new'));
    refreshParticipantLineItems();  
    refreshProductLineItems();
    refreshTaskLineItems();
}

public PageReference save() {
    Savepoint sp = Database.setSavepoint();
    try {
        insert callReport;
        upsertParticipants(callReport.Id);
        upsertProducts(callReport.Id);
        upsertTasks(callReport.Id);
    }
    catch(Exception ex) {
        ApexPages.addMessages(ex);
        Database.rollback(sp); 
        callReport = callReport.clone(false);
        return null;        
    }
    PageReference pageRef = new ApexPages.StandardController(callReport).view();
    pageRef.setRedirect(true);
    return pageRef;
}

public PageReference updateCallReport() {
    Savepoint sp = Database.setSavepoint();
    try {
        update callReport;
        upsertParticipants(callReport.Id);
        upsertProducts(callReport.Id);
        upsertTasks(callReport.Id);
    }
    catch(Exception ex) {
        ApexPages.addMessages(ex);
        Database.rollback(sp); 
        return null;            
    }
    PageReference pageRef = new ApexPages.StandardController(callReport).view();
    pageRef.setRedirect(true);
    return pageRef;         
}

private PageReference upsertParticipants(String callreportid) {
    List<Participant__c> recordsToUpsert = new List<Participant__c>();
    for(Participants parts : participantLinesForPage) {
        if(parts.participantLine.Call_Report__c == null) {
            Participant__c p = new Participant__c();
            p.Call_Report__c = callreportid;
            if(!String.isEmpty(parts.accountId)) {
                p.Account__c = parts.accountId;
            } else {
                p.Account__c = null;
            }
            if(!String.isEmpty(parts.contactId)) {
                p.Contact__c = parts.contactId;
            } else {
                p.Contact__c = null;
            } 
            if(!String.isEmpty(parts.userId)) {
                p.User__c = parts.userId;
            } else {
                p.User__c = null;
            }
            recordsToUpsert.add(p);
        }
        else {
            if(!String.isEmpty(parts.accountId)) {
                parts.participantLine.Account__c = parts.accountId;
            } else {
                parts.participantLine.Account__c = null;
            }
            if(!String.isEmpty(parts.contactId)) {
                parts.participantLine.Contact__c = parts.contactId;
            } else {
                parts.participantLine.Contact__c = null;
            }
            if(!String.isEmpty(parts.userId)) {
                parts.participantLine.User__c = parts.userId;
            } else {
                parts.participantLine.User__c = null;
            }
            recordsToUpsert.add(parts.participantLine);
        }
    }
    if(!recordsToUpsert.isEmpty() && recordsToUpsert.size() > 0) {
        try {
            upsert recordsToUpsert;
        }
        catch(Exception ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    return null;        
}  

private PageReference upsertProducts(String callreportid) {
    List<Products_Presented__c> recordsToUpsert = new List<Products_Presented__c>();
    for(ProductsPresented prodPresented : productLinesForPage) {
        if(prodPresented.productLine.Call_Report__c == null) {
            Products_Presented__c pp = new Products_Presented__c();
            pp.Call_Report__c = callreportid;
            if(!String.isEmpty(prodPresented.productId)) {
                pp.Product__c = prodPresented.productId;
            } else {
                throw new CallReportException('The Product presented field is blank. Please select a product.');
            }
            pp.Notes__c = prodPresented.productLine.Notes__c;
            pp.At_Risk__c = prodPresented.productLine.At_Risk__c;
            recordsToUpsert.add(pp);
        }
        else {
            if(!String.isEmpty(prodPresented.productId)) {
                prodPresented.productLine.Product__c = prodPresented.productId;
            } else {
                throw new CallReportException('The Product presented field is blank. Please select a product.');
            }
            prodPresented.productLine.Notes__c = prodPresented.productLine.Notes__c;
            prodPresented.productLine.At_Risk__c = prodPresented.productLine.At_Risk__c;
            recordsToUpsert.add(prodPresented.productLine);
        }
    }
    if(!recordsToUpsert.isEmpty() && recordsToUpsert.size() > 0) {
        try {
            upsert recordsToUpsert;
        }
        catch(Exception ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    return null;        
}   

private PageReference upsertTasks(String callreportid) {
    List<Task> recordsToUpsert = new List<Task>();
    for(Tasks t : taskLinesForPage) {
        if(t.taskLine.WhatId == null) {
            Task task = new Task();
            task.WhatId = callreportid;
            if(!String.isEmpty(t.whoId)) {
                task.WhoId = t.whoId;
            } else {
                task.WhoId = null;
            }
            if(String.isEmpty(t.userId)) throw new CallReportException('The Assigned To field is blank. Please select a user that is assigned this task.');
            task.OwnerId = t.userId;
            task.Subject = 'Call Report Task';
            task.ActivityDate = t.taskLine.ActivityDate;
            task.Description = t.taskLine.Description;
            task.Status = 'Not Started';
            task.Priority = 'Normal';
            recordsToUpsert.add(task);
        }
        else {
            if(!String.isEmpty(t.whoId)) {
                t.taskLine.WhoId = t.whoId;
            } else {
                t.taskLine.WhoId = null;
            }
            if(String.isEmpty(t.userId)) throw new CallReportException('The Assigned To field is blank. Please select a user that is assigned this task.');
            t.taskLine.OwnerId = t.userId;
            t.taskLine.ActivityDate = t.taskLine.ActivityDate;
            t.taskLine.Description = t.taskLine.Description;
            recordsToUpsert.add(t.taskLine);
        }
    }
    if(!recordsToUpsert.isEmpty() && recordsToUpsert.size() > 0) {
        try {
            upsert recordsToUpsert;
        }
        catch(Exception ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    return null;        
}

public PageReference addParticipant() {
    Participant__c newRecord = new Participant__c();
    participantLinesForPage.add(new Participants(participantLinesForPage.size(), newRecord, newRecord.Account__r.Id, newRecord.Account__r.Name, newRecord.Contact__r.Id, newRecord.Contact__r.Name, newRecord.User__r.Id, newRecord.User__r.Name));
    return null;
}

public PageReference deleteParticipant() {
    Integer selectId = Integer.valueOf(ApexPages.currentPage().getParameters().get('del'));
    Participants toRemove = participantLinesForPage.get(selectId);
    try {
        if(toRemove.participantLine.Id != null) {
            delete [select Id from Participant__c where Id =: toRemove.ParticipantLine.Id]; 
        }
        participantLinesForPage.remove(selectId);
    } catch (Exception e) {
        ApexPages.addMessages(e);
    }  
    Integer iterate = 0;
    for(Participants parts : participantLinesForPage) {
        parts.iterate = iterate;
        iterate +=1;
    }    
    return null;
}

public PageReference addProduct() {
    Products_Presented__c newRecord = new Products_Presented__c();
    productLinesForPage.add(new ProductsPresented(productLinesForPage.size(), newRecord, newRecord.Product__r.Id, newRecord.Product__r.Name));
    return null;
}

public PageReference deleteProduct() {
    Integer selectId = Integer.valueOf(ApexPages.currentPage().getParameters().get('del'));
    ProductsPresented toRemove = productLinesForPage.get(selectId);
    try {
        if(toRemove.productLine.Id != null) {
            delete [select Id from Products_Presented__c where Id =: toRemove.productLine.Id]; 
        }
        productLinesForPage.remove(selectId);
    } catch (Exception e) {
        ApexPages.addMessages(e);
    }  
    Integer iterate = 0;
    for(ProductsPresented prods : productLinesForPage) {
        prods.iterate = iterate;
        iterate +=1;
    }    
    return null;
}

public PageReference addTask() {
    Task newRecord = new Task();
    taskLinesForPage.add(new Tasks(taskLinesForPage.size(), newRecord, newRecord.Who.Id, newRecord.Who.Name, newRecord.Owner.Id, newRecord.Owner.Name));
    return null;
}

public PageReference deleteTask() {
    Integer selectId = Integer.valueOf(ApexPages.currentPage().getParameters().get('del'));
    Tasks toRemove = taskLinesForPage.get(selectId);
    try {
        if(toRemove.taskLine.Id != null) {
            delete [select Id from Task where Id =: toRemove.taskLine.Id]; 
        }
        taskLinesForPage.remove(selectId);
    } catch (Exception e) {
        ApexPages.addMessages(e);
    }  
    Integer iterate = 0;
    for(Tasks tasks : taskLinesForPage) {
        tasks.iterate = iterate;
        iterate +=1;
    }    
    return null;
}

private void refreshParticipantLineItems() {
    List<Participant__c> lineItems = [select Account__c, Account__r.Id, Account__r.Name, Contact__c, Contact__r.Id, Contact__r.Name, User__c, User__r.Id, User__r.Name, Spent_Amount__c, Call_Report__c from Participant__c where Call_Report__c =: callReport.Id];

    participantLinesForPage = new List<Participants>();

    Integer iterate = 0;
    for(Participant__c p : lineItems) {
        participantLinesForPage.add(new Participants(iterate, p, String.valueOf(p.Account__r.Id), p.Account__r.Name, String.valueOf(p.Contact__r.Id), p.Contact__r.Name, String.valueOf(p.User__r.Id), p.User__r.Name));
        iterate += 1;
    }
}

private void refreshProductLineItems() {
    List<Products_Presented__c> prodsPresentedLineItems = [select Product__r.Id, Product__r.Name, Call_Report__c, Notes__c, At_Risk__c from Products_Presented__c where Call_Report__c =: callReport.Id];

    productLinesForPage = new List<ProductsPresented>();

    Integer iterate = 0;
    for(Products_Presented__c pp : prodsPresentedLineItems) {
        productLinesForPage.add(new ProductsPresented(iterate, pp, String.valueOf(pp.Product__r.Id), pp.Product__r.Name));
        iterate += 1;
    }
}

private void refreshTaskLineItems() {
    List<Task> taskLineItems = new List<Task>();
    if(callReport.Id != null) {
        taskLineItems = [select Who.Id, Who.Name, ActivityDate, Description, WhatId, OwnerId, Owner.Id, Owner.Name from Task where WhatId =: callReport.Id];
    }

    taskLinesForPage = new List<Tasks>();

    Integer iterate = 0;
    for(Task t : taskLineItems) {
        taskLinesForPage.add(new Tasks(iterate, t, String.valueOf(t.Who.Id), t.Who.Name, String.valueOf(t.Owner.Id), t.Owner.Name));
        iterate += 1;
    }
}

private Boolean isEditPage(String param) {
    Boolean retval = true;
    if(param != null) {
        retval = false;
    }
    return retval;
}

class Participants {    
    public Integer iterate {get; set;}
    public Participant__c participantLine {get; set;}
    public String accountId {get; set;}
    public String accountName {get; set;}
    public String contactId {get; set;}
    public String contactName {get; set;}
    public String userId {get; set;}
    public String userName {get; set;}  

    public Participants(Integer iterate, Participant__c participantLine, String accountId, String accountName, String contactId, String contactName, String userId, String userName) {
        this.iterate = iterate;
        this.participantLine = participantLine;
        this.accountId = accountId;
        this.accountName = accountName;
        this.contactId = contactId;
        this.contactName = contactName;
        this.userId = userId;
        this.userName = userName;
    }
}

class ProductsPresented {
    public Integer iterate {get; set;}
    public Products_Presented__c productLine {get; set;}
    public String productId {get; set;}
    public String productName {get; set;}

    public ProductsPresented(Integer iterate, Products_Presented__c productLine, String productId, String productName) {
        this.iterate = iterate;
        this.productLine = productLine;
        this.productId = productId;
        this.productName = productName;
    }
}

class Tasks {
    public Integer iterate {get; set;}
    public Task taskLine {get; set;}
    public String whoId {get; set;}
    public String whoName {get; set;}
    public String userId {get; set;}
    public String userName {get; set;}

    public Tasks(Integer iterate, Task taskLine, String whoId, String whoName, String userId, String userName) {
        this.iterate = iterate;
        this.taskLine = taskLine;
        this.whoId = whoId;
        this.whoName = whoName;
        this.userId = userId;
        this.userName = userName;
    }
}
}
public与共享类CallReportControllerExtension{
私人通话报告;
公共布尔isEditMode{get;set;}
公共列表参与者行页面{get;set;}
公共列表ProductlinesFormage{get;set;}
公共列表任务行页面{get;set;}
public CallReportControllerExtension(ApexPages.StandardController stdController){
this.callReport=(Call_Report_uuc)stdController.getRecord();
isEditMode=isEditPage(ApexPages.currentPage().getParameters().get('save_new');
refreshParticipantLineItems();
refreshProductLineItems();
refreshTaskLineItems();
}
公共页面引用保存(){
Savepoint sp=Database.setSavepoint();
试一试{
插入报告;
高级参与者(callReport.Id);
upsertProducts(callReport.Id);
upsertTasks(callReport.Id);
}
捕获(例外情况除外){
ApexPages.addMessages(ex);
数据库回滚(sp);
callReport=callReport.clone(false);
返回null;
}
PageReference pageRef=新建ApexPages.StandardController(callReport.view();
pageRef.setRedirect(真);
返回pageRef;
}
公共页面引用updateCallReport(){
Savepoint sp=Database.setSavepoint();
试一试{
更新报告;
高级参与者(callReport.Id);
upsertProducts(callReport.Id);
upsertTasks(callReport.Id);
}
捕获(例外情况除外){
ApexPages.addMessages(ex);
数据库回滚(sp);
返回null;
}
PageReference pageRef=新建ApexPages.StandardController(callReport.view();
pageRef.setRedirect(真);
返回pageRef;
}
private PageReference upsertParticipants(字符串callreportid){
List recordsToUpsert=new List();
对于参与者(部分:参与者行页面){
if(parts.participantLine.Call\u Report\u c==null){
参与者__cp=新参与者__c();
p、 Call_Report_uuc=callreportid;
如果(!String.isEmpty(parts.accountId)){
p、 Account\uu c=parts.accountId;
}否则{
p、 账户c=null;
}
如果(!String.isEmpty(parts.contactId)){
p、 Contact__c=parts.contactId;
}否则{
p、 联系人__c=null;
} 
如果(!String.isEmpty(parts.userId)){
p、 User\uu c=parts.userId;
}否则{
p、 用户\uuu c=null;
}
记录插入。添加(p);
}
否则{
如果(!String.isEmpty(parts.accountId)){
parts.participantLine.Account\uu c=parts.accountId;
}否则{
parts.participantLine.Account\uuu c=null;
}
如果(!String.isEmpty(parts.contactId)){
parts.participantLine.Contact\uu c=parts.contactId;
}否则{
parts.participantLine.Contact\uuu c=null;
}
如果(!String.isEmpty(parts.userId)){
parts.participantLine.User\uu c=parts.userId;
}否则{
parts.participantLine.User\uu c=null;
}
recordsToUpsert.add(parts.participantLine);
}
}
如果(!recordsToUpsert.isEmpty()&&recordsToUpsert.size()>0){
试一试{
upsert记录upsert;
}
捕获(例外情况除外){
ApexPages.addMessages(ex);
返回null;
}
}
返回null;
}  
专用页面引用upsertProducts(字符串callreportid){
List recordsToUpsert=new List();
对于(产品展示产品展示:productLinesForPage){
if(prodPresented.productLine.Call\u Report\u c==null){
产品u展示u c pp=新产品u展示u c();
pp.Call\u Report\uu c=callreportid;
如果(!String.isEmpty(prodPresented.productId)){
pp.Product\uu c=prodPresented.productId;
}否则{
抛出new CallReportException('提供的产品字段为空。请选择一个产品');
}
pp.Notes\uuu c=prodPresented.productLine.Notes\uu c;
pp.At_Risk\uu c=prodPresented.productLine.At_Risk\uu c;
记录插入。添加(pp);
}
否则{
如果(!String.isEmpty(prodPresented.productId)){
prodPresented.productLine.Product\uu c=prodPresented.productId;
}否则{
抛出new CallReportException('提供的产品字段为空。请选择一个产品');
}
prodPresented.productLine.Notes\uu c=prodPresented.productLine.Notes\uu c;
prodPresented.productLine.At_Risk_uc=prodPresented.productLine.At_Risk_uc;
recordsToUpsert.add(prodPresented.productLine);
}
}
如果(!recordsToUpsert.isEmpty()&&recordsToUpsert.size()>0){
试一试{
upsert记录upsert;
}
捕获(例外情况除外){
ApexPages.addMessages(ex);
返回null;
}
}
返回null;
}   
专用页面引用upsertTasks(字符串callreportid){
List recordsToUpsert=new List();
对于(任务t:taskLinesForPage){
if(t.taskLine.WhatId==null){
任务=新任务();
task.WhatId=callreportid;
如果(!String.isEmpty(t.whoId)){
task.WhoId=t.WhoId;
}否则{
task.WhoId=null;
}
if(String.isEmpty(t.userId))抛出新的CallRep
try {
       action 1;
       action 2;
       action 3;
       action 4;
} catch(Exception ex) {
    ApexPages.addMessages(ex);
}
try { action 1;}catch(exception ex){ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()));}
try { action 2;}catch(exception ex){ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()));}...
if(ApexPages.hasMessages())return null;