0

I have a component on my account record which displays either a lead or an account that might be interesting to the user. In addition I give him the option to "change the owner" (Anfragen) of that record - basically just an update realized in the apex class.

enter image description here

Now what I want to happen is the following:

When the lightning:button is hit and the controller calls my class updating the record I want that the component to refreshe itself so that next record will get "loaded" (with changing the owner this record no longer matches the query criteria) - as for now the user would have to hit F5 for a page refresh to see the new record inside the component.

Here is my component:

 <aura:component controller="NextShadowRecruitController" 
 implements="flexipage:availableForRecordHome,force:hasRecordId" access="global">

   <aura:attribute name="ShadowRecruitList" type="list" />  
   <aura:attribute name="ShadowRecruitItem" type="Object" access="global"/> 
   <aura:attribute name="currentRecord" type="Object"/> 
   <aura:registerEvent name="JTo_Event" type="c:JTo_ShadowRecruitEvent"/> 


<force:recordData aura:id="recordLoader"
recordId="{!v.recordId}"
fields="JOB_Titel_letzte_bez_rel_Anzeige__c,QueryCity__c,WZ_Code_ebene_2__c,Referenz_Jobtitle__c"
targetFields="{!v.currentRecord}"
targetError="{!v.recordLoadError}"
recordUpdated="{!c.handleRecordUpdated}"
                
/>
<div>
<aura:iteration items="{!v.ShadowRecruitList}" var="sr">
<lightning:card footer="Sample footer" iconName="standard:forecasts" class="potential_card">
      
    <aura:set attribute="title">
        <a href="{!'/' + sr.Record_ID__c}">{!sr.Name__c}</a>
    </aura:set>    
         
     <aura:set attribute="actions">
        <lightning:button aura:id="pick" label="Anfragen" onclick="{!c.pickyRecord}"/>
    </aura:set>  
     <div aura:id="spinnerContainer" class="spinnerOuterBox slds-size_1-of-1 slds-hide"> 
            <lightning:spinner aura:id="spinner" variant="brand" size="medium" class="spinner" alternativeText="accountFound"/>
        </div>

    <div class="slds-p-horizontal--small">  
      
       <p> Standort: {!sr.Street__c}, {!sr.PostalCode__c + ' ' + sr.City__c} </p>
       <p> Titel: <a href="{!'http://db.advertsdata.com/matching/company.cfm?nav_id=' + sr.Index_ID__c + 'kundenid=17'}"> {!sr.Jobtitel__c} </a> </p>
       <p> Branche: {!sr.Industry__c} </p> 
       <p> Mitarbeiter: {!sr.Mitarbeiter__c} </p>
                
    </div> 

    <aura:set attribute="footer" >
         <lightning:badge label="Titel" class="slds-theme--success"/> <!-- iconName="utility:success"  -->
         <lightning:badge label="Standort"  class="{!if(v.currentRecord.QueryCity__c == sr.City__c, 'slds-theme--success','slds-theme--error')}"/> <!-- iconName= "{!v.accountRecord.ShippingCity == acc.ShippingCity ? 'utility:success' : 'utility:ban'}" -->
         <lightning:badge label="Branche"  class ="{!if(v.currentRecord.WZ_Code_ebene_2__c == sr.WZ_Code_ebene_2__c , 'slds-theme--success','slds-theme--error')}"/> <!-- iconName ="{!v.accountRecord.WZ_Code_ebene_2__c == acc.WZ_Code_ebene_2__c ? 'utility:success' : 'utility:ban'}" -->
    </aura:set>     

     </lightning:card>
 </aura:iteration>

my Controller:

({
   
   handleRecordUpdated : function(c, e, h){
    var eventParams = e.getParams();
    if(eventParams.changeType === "LOADED") {
       // record is loaded (render other component which needs record data value)
        console.log("Record is loaded successfully.");
        h.getRelatedRecords(c, e, h);
    } else if(eventParams.changeType === "CHANGED") {
        // get the fields that changed for this record
        var changedFields = eventParams.changedFields;
        console.log('Fields that are changed: ' + JSON.stringify(changedFields));
    } else if(eventParams.changeType === "REMOVED") {
        // record is deleted
    } else if(eventParams.changeType === "ERROR") {
        // there’s an error while loading, saving, or deleting the record
    }
  $A.get('e.force:refreshView').fire();
  },


pickyRecord : function(component, event, helper) {
    helper.showSpinner(component);
    var action = component.get("c.pickRecord");
   action.setParams({ sr : component.get("v.ShadowRecruitItem")[0] }); 
    action.setCallback(this, function(a) {
      
        var toastEvent = $A.get("e.force:showToast");
        //get the response state
        var state = a.getState();
        var errors = a.getError();
        //check if result is successfull
        if(state == "SUCCESS"){
            toastEvent.setParams({
                "title": "Erfolg!",
                "message": "Datensatz wurde erfolgreich an dich übertragen.",
                "mode": 'sticky',
                "type" : "success"
            });
        }
        else if(state === "ERROR") {
            let message = 'Unknown error'; // Default error message
            // Retrieve the error message sent by the server
            if (errors && Array.isArray(errors) && errors.length > 0) {
                message = errors[0].message;
            }
            toastEvent.setParams({
                "title": "Error!",
                "message": message,
                "mode": 'sticky',
                "type" : "error"
            });
        }
            else{
                console.log('Done nothing ...');
            }
        helper.hideSpinner(component);
        toastEvent.fire();
        var refresherEvent = component.getEvent("JTo_Event");
        refresherEvent.fire();
    });
    $A.enqueueAction(action);
 }
 
})

my Helper:

({
getRelatedRecords: function (component, event, helper) {
var action = component.get("c.getRecordBasedonJobTitle");
  
console.log("::jobTitle:: >", component.get("v.currentRecord.Referenz_Jobtitle__c"));
console.log("::city:: >", component.get("v.currentRecord.QueryCity__c"));
console.log("::industry:: >", component.get("v.currentRecord.WZ_Code_ebene_2__c"));
  

  
    action.setParams({ 
    jobTitle: component.get("v.currentRecord.Referenz_Jobtitle__c"), 
    city:     component.get("v.currentRecord.QueryCity__c"),
    industry: component.get("v.currentRecord.WZ_Code_ebene_2__c"),
    recordId: component.get("v.currentRecord.Id")
});
  
   action.setCallback(this, function (response) {
    if (response.getState() === "SUCCESS") {
    component.set("v.ShadowRecruitList", response.getReturnValue());
    component.set("v.ShadowRecruitItem", response.getReturnValue());  
      
  } else {
    console.log("::some error::::");
  }
});
$A.enqueueAction(action);
},
showSpinner : function(component) {
    $A.util.removeClass(component.find("spinnerContainer"), 'slds-hide');
    component.find("pick").set("v.disabled", true);
    //component.set("v.showLeItemPanel", false);
},

hideSpinner : function(component){
    $A.util.addClass(component.find("spinnerContainer"), 'slds-hide');
    component.find("pick").set("v.disabled", false);
    //component.set("v.showLeItemPanel", true);
}
});

1 Answer 1

0

Add this under your class controller and change the value of component C

({

    refresh : function(component, event, helper) {
    var action = component.get('c.myController');
    action.setCallback(component,
    function(response) {
    var state = response.getState();
    if (state === 'SUCCESS'){
    // Refresh the view
    // after data is updated
    $A.get('e.force:refreshView').fire();
    } else {
    // Handle the 'ERROR' or 'INCOMPLETE' state
    }
    }
    );
    $A.enqueueAction(action);
    }

})

You must log in to answer this question.

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