0

I have a component showing case notes with a simple toggle button for public/private. I'm trying to use a popup for creating a new case note. It also works fine but how do I wait on the results before I requery and redraw the associated case notes? I click new comment, it pops up the new dialog box, I fill in the comment and click save. The comment is properly added but the list of case comments is queried before the new comment is inserted into the database

cmp

<aura:component controller="mevLCaseComment" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
  <aura:attribute name="commentList" type="casecomment[]" />
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />   
  <article class="slds-card slds-card_boundary related_list_card_border_top forceRelatedListCardDesktop" >
    <div class="container forceRelatedListSingleContainer" style="padding: 5px 5px">

        <lightning:icon iconName="custom:custom19" alternativeText="Case Notes"/><lightning:formattedText value=" - Case Comments" /> 
         <div class="slds-float_right"> <button style="align:right;width:75px !important;" class="slds-button slds-button--icon-border" onclick="{!c.createRecord}" label="New Comment">New Comment</button>        </div>
      <div style="width:100%"><hr></hr></div>
      <table>
        <tr><th style="width: 20%">Created By</th><th style="width: 20%">Date/Time</th><th style="width: 50%">Comment</th><th>Public</th></tr>
        <aura:iteration items="{!v.commentList}" var="cc" >
          <tr>
            <td>{!cc.CreatedBy.Name}</td>
            <td><ui:outputDateTime value="{!cc.CreatedDate}" format="M/D/YYYY HH:mm a"/></td>
            <td>{!cc.CommentBody}</td>
            <td>&nbsp;<ui:outputCheckbox aura:id="outputBox" value="{!cc.IsPublished}"/></td>
            <td>
              <aura:if isTrue="{!cc.IsPublished}">
                <button data-id="{!cc.Id}" style="width:50px !important" class="slds-button slds-button--icon-border" label="Make Private" onclick="{!c.makePrivate}">Make Private</button>
                <aura:set attribute="else">
                  <button data-id="{!cc.Id}" style="width:50px !important" class="slds-button slds-button--icon-border" label="Make Public" onclick="{!c.makePublic}">Make Public</button>
                </aura:set>                  
              </aura:if>  
            </td>  
          </tr>
          <tr class="ccdividerrow"><td colspan="5"><hr></hr></td></tr>
        </aura:iteration>
      </table>    
    </div>
  </article>
</aura:component>

.js

({
    doInit: function(component, event, helper) {
        helper.apexCall(component, event, helper, 'getCaseComments', component.get('v.recordId'));
    },
    makePublic: function(component, event, helper) {
        var ccid = event.currentTarget.dataset.id;
        helper.apexCall(component, event, helper, 'changeToPublic', ccid);
    },
    makePrivate: function(component, event, helper) {
        var ccid = event.currentTarget.dataset.id;
        helper.apexCall(component, event, helper, 'changeToPrivate', ccid);
    },
    createRecord: function(component,event,helper) {
      var ccid = component.get('v.recordId');
        console.log('Retrieved ParentId: ' + ccid);
      var createRecordEvent = $A.get("e.force:createRecord");      
      createRecordEvent.setParams({
          "entityApiName": "CaseComment","defaultFieldValues": { "ParentId" : ccid }
      });
      createRecordEvent.fire();
       console.log('create Record Event has been fired');
      helper.apexCall(component, event, helper, 'getCaseComments', ccid);        
    }

})

helper

({
    apexCall: function(component, event, helper, actionName, ccid) {
        var action = component.get('c.'+actionName);
        action.setParams({ ccid: ccid });
        action.setCallback(this, function(data) {
            var state = data.getState();
            if (state === 'SUCCESS') {
                component.set('v.commentList', data.getReturnValue());
            } else {
                console.error("ERROR");
            }
        });
        $A.enqueueAction(action);
    }
})

apex class

public class mevLCaseComment {
    @AuraEnabled
    public static list<casecomment> getCaseComments(id ccid) {
        system.debug(logginglevel.info, 'MEVION DEBUG: case id ' + ccid);
        return [Select Id, commentbody,ispublished,createdby.name,createddate From casecomment Where parentid = :ccid order by createddate desc]; 
    }

    @AuraEnabled
    public static list<casecomment> changeToPublic(string ccid) {
        system.debug(logginglevel.info, 'MEVION DEBUG: casecomment id ' + ccid);
        casecomment[] cc = [select id, parentid, ispublished from casecomment where id=:ccid];
        id pid=null;
        if(cc.size()>0) {
          cc[0].ispublished = true;
          pid = cc[0].parentid;
          update cc;
        }
        return getCaseComments(pid);
    }

    @AuraEnabled
    public static list<casecomment> changeToPrivate(string ccid) {
        system.debug(logginglevel.info, 'MEVION DEBUG: casecomment id ' + ccid); 
        casecomment[] cc = [select id, parentid, ispublished from casecomment where id=:ccid];
        id pid = null;
        if(cc.size()>0) {
          cc[0].ispublished = false;
          pid = cc[0].parentid;
          update cc;
        }
        return getCaseComments(pid);
    }
}
1
1

You could create an event for toast and then use the toast message to write some javascript logic.

cmp
<aura:handler event="force:showToast" action="{!c.handleToastEvent}"/>

controller
handleToastEvent : function(component, event, helper) {
var toastMessageParams = event.getParams();
var message = toastMessageParams.message;


//if the toast includes Account (obj) and includes 'was created'
if (message.includes('Account') && message.includes('was created')) {
    //do something here such as reload after record was created.   
    $A.get('e.force:refreshView').fire();
}else if(message.includes('Account') && message.includes('was saved')){
    //do something here such as reload after record was saved.
    $A.get('e.force:refreshView').fire();
}


},
1
  • Thanks ebar. Was able to use detection of the Toast message to fire the getCaseComments after record was created – Ohmicron Aug 6 '19 at 16:55
1

force:createRecord does not have a callback.

Since you are using CaseComment, you have a very simple workaround. Just implement recordEditForm and create record. Then you can have handle all events like success and error. Also you can prepolute the values if needed. You can implement this form in modal.

2
  • Doesn't work as now I get this when I assign the record and use lighting:recordEditForm of lilghtning:recordForm 'Object CaseComment is not supported in UI API INVALID_TYPE – Ohmicron Jul 30 '19 at 19:11
  • 1
    Sry, forgot to check that. will check for better solution and let you know. – salesforce-sas Jul 30 '19 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.