0

I want to be able to run 4 different queries on a single visualforce page depending on the button clicked on a lightning component. Right now the way its set up works but it can't access the static variable on the apex controller so the page is always blank. (This is going to be a table style query with multiple rows and columns)

My other thought process was to put all the queries in the constructor but then how do you set the variable from the lightning controller? I tried action.setParams({'detQ':'ten'}) but I guess im using the wrong syntax or this isn't feasible. (also tried 'c.detQ', detQ with no luck) and then the constructor would be automatically called when the class is invoked right? Heres what I have so far.

Aura:

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

some stuff

 <lightning:button
     label="10%"
     iconName="utility:open"
     iconPosition="left"
     variant="base"
     type="submit"
     onclick="{!c.ten}"
     value="{!property.Id}" 
 />

 <lightning:button
    label="25%"
    iconName="utility:open"
    iconPosition="left"
    variant="base"
    type="submit"
    onclick="{!c.twentyFive}"
    value="{!property.Id}" 
 />

some stuff

</aura:component>

Controller

 ten : function (component, event, helper)
    {
        //get value of invoked button
        var myId = event.getSource().get('v.value');
        //call method of controller
        var action = component.get("c.DisplayAccounts");
        //set variable equal to acc id
        action.setParams({'propSearch': myId});
        action.setCallback(this,function(response){
            var state = response.getState();
            if(state === "SUCCESS")
            {         
                window.open('/apex/PropertyPDFViewer');  
            }
            else{
                console.log('unable to generate the pdf');
            }
        });
        $A.enqueueAction(action);

    }, 

Apex:

public class PropertyPDFController
{  
 @AuraEnabled
    public String detQ;
    public List<opportunity> hipdeals {get; set;}

     public PropertyPDFController(ApexPages.StandardController controller)
    {
        hipdeals = new List<opportunity>();

        //wondering if i can somehow set this value from lightning controller
        detQ = '';
       //then use if/else statements to run queries based on value
        if(detQ == 'ten')
        {
            hipdeals = [select stuff];
        } else 
        if(detQ==25)
        {
           do other stuff
        }

    }

//what i'm using right now
@AuraEnabled  
    public static List<Opportunity> DisplayAccounts(String propSearch)
    {
        system.debug(propSearch);
        String secondSearch = propSearch;
        List<opportunity> hipdeals = new List<opportunity>();
        deals = [select stuff from opportunity};
       //I cant access this variable from VFP even if its declared outside method
        return deals;
    }

VFP:

<apex:page StandardController="Opportunity" extensions="PropertyPDFController" applyBodyTag="false" sidebar="false" showHeader="false" renderAs="PDF">

<apex:repeat var="Deals" value="{!hipdeals}" id="theRepeat">
        <tr>   
          <td>{!Deals.Ownership1__c}</td>    
          <td>{!Deals.HIP_Market__c}</td>            
          <td>{!Deals.Prop_Building_Name__r.name}</td>    
          <td>{!Deals.Name}</td>    
          <td>{!Deals.Probability}<ins>%</ins></td>             
          <td>
</apex:page>

1
  1. The instance of apex controller called from visualforce page will be totally different from the instance called by lightning component.

  2. You need to query for the fresh data again from page. Basic criteria can be passed in parameters.

    public PropertyPDFController(ApexPages.StandardController controller)
    {
        hipdeals = new List();
        String detq = apexpages.currentpage().getparameters().get('detq');
        String otherattr = apexpages.currentpage().getparameters().get('otherattr');
        if(detQ == 'ten') {
            hipdeals = DisplayAccounts('some_search');
        } else if (detQ==25) {
            //do other stuff
        }
    }

    @AuraEnabled  
    public static List DisplayAccounts(String propSearch)
    {
        system.debug(propSearch);
        String secondSearch = propSearch;
        List hipdeals = new List();
        hipdeals = [select stuff from opportunity];
        return hipdeals;
    }
  1. Now pass the parameters in JS: (you should be using encodeURI for safe encoding non-URL characters like space)

    let detq = component.get("v.detq");
    let otherattr = component.get("v.otherattr");
    window.open(encodeURI(`/apex/PropertyPDFViewer?detq=${detq}&otherattr=${otherattr}`));
    
5
  • Okay that makes sense. So do I need to make a variable in aura called detq and set it, then grab from controller? or can i just pass a value from the controller straight to apex? Say i wanted the value to be ten or twentyFive? I would set the value of a button in aura to value=ten and then do component.get(v.value)? or could i just do let detq = component.set('10'); – Tyler Aug 13 '19 at 14:23
  • you have to pass in URL which will be readable by page in its apex constructor – salesforce-sas Aug 13 '19 at 14:26
  • so it would be /apex/PropertyPDFViewer?detq=${ten}&otherattr=${myId} – Tyler Aug 13 '19 at 14:31
  • it should be something like /apex/PropertyPDFViewer?detq=some_value&otherattr=other_value. The syntax ${} is for string concatenation as shown in code. That will fetch and place the value in string so that you get above string (like) – salesforce-sas Aug 13 '19 at 14:45
  • Cool! Thanks that helped a lot! Also, what if I only wanted to open the window if the query returned information, is there an easy way to do that? It would be even better if i could gray out the button if the query wasn't going to return anything – Tyler Aug 13 '19 at 16:36
0

You are creating a new variable in your method, so it will be considered local to that method which doesn't have any value.

Don't reinitialise the variable, just do..

hipdeals = [select stuff from opportunity] in your method.

Another thing, you are using same Apex class for lightning component and vf also, so once you call the VF using JavaScript window.open the whole class will reinitiate, it will not hold the state of your data.

Either keep everything in a Lightning component or in visualforce so that it will maintain the state.

Hope this will help.

1
  • Thanks for the feedback! – Tyler Aug 13 '19 at 16:27

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.