0

So I am trying to assign the current record I am on to recordID but I am having some issues.

Apex controller

@AuraEnabled
public static String returnid()
{

     String idofpage; 
     idofpage = apexpages.currentpage().getparameters().get('id'); 
     return idofpage;
}

My JS controller

        handleUploadFinished: function (component, event, helper) {
     var getid = component.get('c.returnid');   

      getid.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            alert("From server: " + response.getReturnValue());
        }
        else if (state === "INCOMPLETE") {
            alert("INCOMPLETE");
        }
        else if (state === "ERROR") {
         alert("Error");
        }
    });     

       $A.enqueueAction(getid);   

    // Get the list of uploaded files
    //var uploadedFiles = event.getParam("files");
    //alert("Files uploaded : " + uploadedFiles.length);
},

Component

 <aura:attribute name="myRecordId" type="String" description="Record to which the files should be attached" />
<lightning:fileUpload label="Attach receipt"
    multiple="true"
    accept=".pdf, .png"
    recordId="{!v.myRecordId}"
    onuploadfinished="{!c.handleUploadFinished}" />

My JS controller shows 'error' as the alert. When I hard code a value in my Apex controller it does not complain leading me to believe I am getting the id of the page wrong.

What I plan to do is assign the current page id to recordId in my component. Is there a better approach then what I am currently doing? If not, how can I get the current id of the page in my Apex controller to pass to my JS controller?

2

I think you're confusing two different contexts. ApexPages.currentPage().getParameters().get('id') is useful for Visualforce, but you're writing a lightning component. If you lightning component has a recordId (because it implements force:hasRecordId) then it has a hidden attribute named recordId. So all you have to do to get it from your controller is component.get("v.recordId") and no Apex is required. So in your example:

handleUploadFinished: function (component, event, helper) {
    var getid = component.get('v.recordId');     

    // Get the list of uploaded files
    //var uploadedFiles = event.getParam("files");
    //alert("Files uploaded : " + uploadedFiles.length);
},

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.