Question

I have a visual force page which displays the current child cases linked to the selected parent case.

Each child case has a dedicated checkbox.

I need to be able to only add a comment to the selected child cases and not all child cases.

    <apex:outputText value="this is a test" rendered="false" />
    <apex:datatable value="{!related}" var="rel" width="80%">

        <apex:commandButton value="Save" action="{!save}"/>
        <apex:column headervalue="Select" width="10%">
            <apex:inputCheckbox value="{!rel.c.UpdateChildcases__c}"/><p/>
        </apex:column>
        <apex:column headervalue="Case Number" width="30%">
            <apex:outputLink value="/{!rel.c.Id}" target="_blank">{!rel.casenumber}</apex:outputLink><p/>
        </apex:column>
        <apex:column headervalue="Account Name" width="30%">
            <apex:outputLink value="/{!rel.Id}" target="_blank">{!rel.account.name}</apex:outputLink><p/>
        </apex:column> 
        <apex:column headervalue="Subject" width="30%">
            <apex:outputLink value="/{!rel.Id}" target="_blank">{!rel.subject}</apex:outputLink><p/>
        </apex:column>            
    </apex:datatable>              

    <apex:commandButton value="Save"  action="{!Save}" />
</apex:form>
</apex:page>

The comment to be inserted is picked up from the text area

public with sharing class ParentCases {
    public List related {get; set;}

    public String mysearchtext {get; set;}
    public boolean selected {get; set;}

    public ParentCases(ApexPages.StandardController std) {
        selected = false;
        Case cs = (Case) std.getRecord();
        related = [select id, CaseNumber,  Subject, description, Status, UpdateChildcases__c , child_update__c, account.name from Case where parentId = :cs.id];
    }

    public void Save() {

        List<CaseComment> childCom = new List<CaseComment>();
        for (integer i = 0; i < related.size(); i++) {

            CaseComment newCom = new CaseComment();
            newCom.CommentBody = mysearchtext;
            newCom.IsPublished = TRUE;
            newCom.ParentId = related[i].id;
            childCom.add(newcom);
        }

        if (!childCom.isEmpty()) {
            insert childCom;

        }
    }
}

this updates all child records. I really need to restrict the insert to the selected ones. ??

Was it helpful?

Solution

In the for-loop check for UpdateChildcases__c field value:

for (integer i = 0; i < related.size(); i++) {
    if (related[i].UpdateChildcases__c) {
        CaseComment newCom = new CaseComment();
        newCom.CommentBody = mysearchtext;
        newCom.IsPublished = TRUE;
        newCom.ParentId = related[i].id;
        childCom.add(newcom);
    }
}

P.S. here is explanation of how the same can been achieved without using a custom boolean field on the Case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top