0

Is it possible to have the same apex controller being used by two different aura components? I'm trying to do that but the the component controller is getting an error response from the apex controller. This is my code:

Component:

        <div class="{!'slds-box slds-theme–alert-texture slds-theme–' + v.type}">
             <lightning:icon iconName="{! 'utility:'+ v.type }" size="medium"
                             alternativeText="{!v.type}" Class="iconContainer"/>&nbsp;
        <span class="slds-text-body–regular">
             <aura:unescapedHtml value="{!v.message}"/>
        </span>
        </div>
        <br/><br/><br/><br/>
        <div class="slds-p-around--medium">
            <div class="slds-float--right">
                <lightning:button label="Close" 
                                  class="slds-button slds-button_brand"
                                  variant="brand"
                                  onclick="{!c.closeBtn}"/>
             </div>
         </div> 

Component JS Controller

({
handleRecordUpdated: function(component, event, helper) {

    var action = component.get("c.setCall");
    console.log("Test1");
     //set the parameters
     action.setParams({
         nif: component.get("v.simpleLead").NIF__c,
         country: component.get("v.simpleLead").Country
     })
     console.log("Test2 " + component.get("v.simpleLead").Country);
     action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS"){
            console.log("Success");  
            if(response.getReturnValue()==="Success"){
                component.set("v.type", "success" );
                component.set("v.message", response.getReturnValue());
            } else if (response.getReturnValue()!="Success"){
                component.set("v.type", "error" );
                component.set("v.message", response.getReturnValue());
            }
            }

        else if (state === "ERROR") {
        console.log("Error");
            component.set("v.type", "error" );
            var errors = response.getError();
                if (errors) {
                  if (errors[0] && errors[0].message) {
                     component.set("v.message",errors[0].message );//Fetching Custom Message.
                     }
                  }
         }
         else {
              component.set("v.message", 'Request Failed!' );
         }
    });
    //Send action off to be executed
    $A.enqueueAction(action);

},

closeBtn : function(component, event, helper) {
    var dismissActionPanel = $A.get("e.force:closeQuickAction");
    dismissActionPanel.fire();
}
}
})

Apex controller

public with sharing class Ctrl_CallInformaDB {
@AuraEnabled
public static string setCall(String nif, String country){

    string errorCode = InformaDB_API.CreditInfoProviderOrquestrator(nif,country);
    string returnMessage;
    List<Translate_Metadata__mdt> messages=[SELECT Value_EN__c FROM Translate_Metadata__mdt WHERE label = : errorCode];
    for(Translate_Metadata__mdt s : messages){
       System.debug('message ' + s.Value_EN__c);
       returnMessage = s.Value_EN__c;
       }

    return returnMessage;
 }
}
  • 1
    Yes, it's possible, but we would need to see a specific failing example to help you. – sfdcfox Oct 16 '18 at 8:14
  • I posted my code that is failing with an response.getState = Error. – I. Simoes Oct 16 '18 at 8:25
  • Can you check if you are passing the parameters correctly? – Sagar Hinsu Oct 16 '18 at 8:36
  • I did that and both parameters are passing the value correctly. – I. Simoes Oct 16 '18 at 8:39
  • Did you check debug log from developer console? – tugce Oct 16 '18 at 9:11
0

I had the nif field set to decimal, and the method was expecting a string. So one of the parameters was passing a decimal to the apex controller while this one was expecting a string, thus trowing an illegal argument error. Thank you for the help!

|improve this answer|||||

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.