1

我在我的控制器中抛出了一些自定义异常,并且我注意到当有多个错误时,这些错误不会一次全部显示。相反,它们在页面上一次显示一个。我正在使用该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;
    }
}
}

谢谢你的帮助。

4

1 回答 1

1

例如,您有四个 DML 操作并且第一个操作失败,因此代码不会继续执行第二个操作。所以你只在页面上显示第一个错误。

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;
于 2013-08-15T05:30:38.330 回答