0

I'm trying to fire a custom event in a child component to notify the parent that a case record has been saved and that it should perform an appropriate action.

I'm finding that inside the saveRecord callback there is no access to the component to retrieve the event, any relevant properties for me to pass into the event and the ability to fire the event.

This is the code I'm running in the helper.js for my child component.

component.find('caseRecordCreator').saveRecord($A.getCallback(function(saveResult){
            if(saveResult.state === 'SUCCESS' || saveResult.state === 'DRAFT'){
                var savedCaseEvent = component.getEvent('caseSaved');
                var caseRecord = component.get('v.caseRecordTemplate');
                savedCaseEvent.setParams({record : caseRecord, success : true});
                savedCaseEvent.fire();
            }else{  
                //TODO: Error handle other states
                console.log('case error: ' + JSON.stringify(saveResult.error));
            }
        }));

This is the error I'm receiving:

This page has an error. You might just need to refresh it.
Error in $A.getCallback() [Cannot read property 'setParams' of null]
Callback failed: serviceComponent://ui.force.components.controllers.recordGlobalValueProvider.RecordGvpController/ACTION$saveRecord Failing descriptor: {c:CaseEditorForm}

I have tried declaring the event before I call saveRecord as per below:

var savedCaseEvent = component.getEvent('caseSaved');
        var caseRecord = component.get('v.caseRecordTemplate');
        component.find('caseRecordCreator').saveRecord($A.getCallback(function(saveResult){
            if(saveResult.state === 'SUCCESS' || saveResult.state === 'DRAFT'){
                savedCaseEvent.setParams({record : caseRecord, success : true});
                savedCaseEvent.fire();
            }else{  
                //TODO: Error handle other states
                console.log('case error: ' + JSON.stringify(saveResult.error));
            }
        }));

I don't receive any error here but the event still doesn't fire. If I place all of the event code outside of the saveRecord callback then it fires as expected, I can't do this as I need the save result before firing.

Many thanks.

EDIT: Component code as well:

<aura:attribute name="caseRecordTemplate"   type="Object" />
<aura:attribute name="caseRecord"           type="Object" />
<aura:attribute name="caseError"            type="String" />

<aura:registerEvent name="caseSaved"            type="c:CaseSavedToServerEvt"/>

<force:recordData aura:id="caseRecordCreator"
    layoutType="FULL"
    targetRecord="{!v.caseRecordTemplate}"
    targetFields="{!v.caseRecord}"
    targetError="{!v.caseError}"/>
  • Could you add your force:recordData markup in here ? if possible add the component mark-up as well. – StormBr34ker Oct 23 '17 at 9:57
  • @KReader Please add your complete component by editing your quetion – Himanshu Oct 23 '17 at 10:05
  • Yes, apologies - just edited the question with component code. Please let me know if you need any further information. Thanks – KReader Oct 23 '17 at 10:06
  • I've determined the issue was related to another event I was firing earlier in my helper function. Whilst the rest of the code in the function ran as expected, the callback did not work. I switched to a standard server callback for inserting the sobject record and experienced the same issues, which led me to the problem. – KReader Oct 24 '17 at 9:37

Your Answer

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

Browse other questions tagged or ask your own question.