0

I have a vf page that displays an error message. On Case object when I edit a case, and save it with an attachment, I don't want it to display that vf page.

VF Page Code:

<apex:page standardController="Case" extensions="CaseAttachmentExtensions" >
<apex:form ><apex:outputLabel id="labelId" value="error message"  rendered="{!attach==False}" ></apex:outputLabel></apex:form></apex:page> 

Controller Extension:

public class CaseAttachmentExtensions {
    public boolean attach{get; set;}
    public string saveattach{get;set;}

    public CaseAttachmentExtensions(ApexPages.StandardController controller){
        List<Case> newList =New List<case>();
        List<RecordType> recId=[
            SELECT id 
            FROM recordtype 
            WHERE Name='WDMS Customer Admin' 
            LIMIT 1
        ];

        List<Attachment> listA = new List<Attachment>();
        set<id> caseIds=new set<id>();
        for(Case c:newList) {
            System.debug('gargi--'+c.Sub_Type__c );

            if(
                c.recordtypeId == recId.get(0).id && (
                    c.Sub_Type__c == 'Process Contract' || 
                    c.Sub_Type__c == 'Process Recontract' || 
                    c.Sub_Type__c == 'Process Trial' || 
                    c.Sub_Type__c == 'Request Cancel Quote' ||
                    c.Sub_Type__c == 'Vendor Form' || 
                    c.Sub_Type__c == 'DSA Account/contact change'
                )
            ){
                caseIds.add(c.id);
            }

            if (!caseIds.isEmpty()) {
                listA =[
                    SELECT Id,ParentId 
                    FROM Attachment 
                    WHERE ParentId IN: caseIds 
                        AND Parent.Type = 'Case'
                ];

                if (listA.isEmpty()) {  
                    attach=False;
                    system.debug('the value of the attachment'+attach); 
                } 
            }
        }
    }
}
2
  • 1
    Try rendered="{!NOT(attach)}"
    – gNerb
    May 7, 2019 at 15:35
  • 1
    The problem is, you never set the "attach" variable to true in your constructor. It must be set to true if the attachment exists.
    – Aayush K
    May 7, 2019 at 16:15

1 Answer 1

0

One of the issues I see is that newList is always an empty list as you never populate it with the case from the page. Its also important to note that the page will only give you a single case, not a list of cases.

Try this:

public class CaseAttachmentExtensions {
    public boolean attach{get; set;}
    public Case record {get; set;}
    public string saveattach{get;set;}

    public CaseAttachmentExtensions(ApexPages.StandardController controller){
        // THis is how we get the get the caseId from the controller
        this.record = getCase(controller.getId()); 

        if (this.record != null) {
            this.attach = this.record.attachments.size() > 0;
        } else {
            this.attach = false;
        }
    }

    private Case getCase(Id recordId) {
        // There is no need to run a query to get the record type info
        Schema.DescribeSObjectResult caseDescribe = Schema.SObjectType.Case;
        Map<String,Schema.RecordTypeInfo> rtMapByName = caseDescribe.getRecordTypeInfosByName();
        String caseRecordTypeId = rtMapByName.get('WDMS Customer Admin').getRecordTypeId();

        List<Case> cases = [
            SELECT Id, (
                SELECT Id
                FROM Attachments
            )
            FROM Case
            WHERE Id = :recordId
                AND Sub_Type__c IN (
                    'Process Contract','Process Recontract','Process Trial',
                    'Request Cancel Quote','Vendor Form','DSA Account/contact change'
                )
                AND RecordTypeId = :caseRecordTypeId
        ];

        if (!case.isEmpty()) {
            return cases[0];
        } else {
            return null;
        }
    }
}

Here we are making the database do as much of the work as possible. We are only pulling back the case in the event that it matches the criteria you defined (sub type/recordtype) if the record is null, it does not match those criteria. If the record does come back, we can check to see if it has any attachments and if it does, we set attach to true.

Then you can do what my comment says and use {!NOT(attach)} in your rendered attribute.

Disclaimer:

I've been considering trying to see if querying the attachment object directly based on parentId had any merit. The key issue being that the parent lookup references the hidden Name object and not the actual case (as its a polymorphic field). This means in the query, we cannot reference any of the fields on the case. This would mean we would either require a second query to pull in the case or would require all of the necessary fields be referenced in the VF page so that we can use standardController.getRecord() to bring in the case instead. And this would still require an ugly if statement to evaluate the case against the sub-type/record type criteria.

Ultimately, I think this approach is safest.

6
  • I am getting this error Error: Compile Error: Variable does not exist: caseDescribe at line 19 column 9 May 7, 2019 at 17:59
  • Cant make things too easy for you :) (fixed the code)
    – gNerb
    May 7, 2019 at 20:04
  • The Visualforce page that contains the error is displayed even when the sub-type is NOT any of these 'Process Contract','Process Recontract','Process Trial', 'Request Cancel Quote','Vendor Form','DSA Account/contact change' May 8, 2019 at 15:20
  • To be clear, the page cannot be hidden. Only the content within the output label can be hidden. Are you saying that the content within the label is being displayed?
    – gNerb
    May 8, 2019 at 19:20
  • 1
    Add -{!attachment}- to your VF page so you can see what the value of attachment is in order to determine whether you are indeed changing the boolean value to what you expect. You could also add some System.debug() statements throughout your code and log both the results of your SOQL queries and the state of your attach boolean value so you can see if it's setting it to what you expect.
    – Tad
    May 8, 2019 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .