1

I have created a 'Download csv' button through lightning page.When I click the button and choose the user from the dropdown for whom the related list be downloaded.The entire list according to the drop down chosen should be downloaded.On clicking for the first time only the page gets loaded and stores a cache according to the dropdown selected.On clicking for the next time list gets downloaded but for the previous action.How to overcome this?Any help please?I have attached the code for your reference.

Component:

 <aura:attribute name="zoneList1" type="ZoneBasedPricingController.ZoneWisePriceListnew[]"/>
   <div align="center">
                <button class="slds-button slds-button--brand" onclick="{!c.downloadCsv}">Download As CSV</button> <br/><br/>
            </div>

Controller:

downloadCsv : function(component,event,helper){
          //call the listnew
        var list = component.get("v.zoneList1");
            console.log("result>>1234" );
        console.log('helper' +helper);
        var page = null;
        var recordToDisply = null;
        var SearchKey = '';
        var FamilyKey = component.get("v.FamilyKeyValue");
        console.log('Fam>>>' +FamilyKey);
        component.set("v.SearchKeyValue", SearchKey);
        var UserName = component.get("v.UserName");
        // var list = component.get("v.zoneList1");
        // call the helper function which "return" the CSV data as a String 
       // helper.ZonePriceListnew(component, page, recordToDisply, SearchKey, FamilyKey, UserName);

        var csv = helper.convertArrayOfObjectsToCSV(component,list, page, recordToDisply, SearchKey, FamilyKey, UserName);   

         if (csv == null){return;} 

        // ####--code for create a temp. <a> html tag [link tag] for download the CSV file--####     
         var hiddenElement = document.createElement('a');
          hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
          hiddenElement.target = '_self'; // 
          hiddenElement.download = 'ExportData.csv';  // CSV file Name* you can change it.[only name not .csv] 
          document.body.appendChild(hiddenElement); // Required for FireFox browser
          hiddenElement.click(); // using click() js function to download csv file

        }, 

Controller Helper:

convertArrayOfObjectsTo CSV : function(component,objectRecords, page, recordToDisply, SearchKey, FamilyKey, UserName){
          console.log("result>>1" +component);
        console.log("page>>" +page);
        console.log("recordToDisply>>" +recordToDisply);
        console.log("SearchKey>>" +SearchKey);
        console.log("FamilyKey>>" +FamilyKey);
        var action = component.get("c.getZWPListnew");
        action.setParams({
            "pageNumber": page,
            "recordToDisply": recordToDisply,
            "Searchkey": SearchKey,
            "FamilyKey": FamilyKey,
            "UserName" : UserName
        });

Apex Class:

 @AuraEnabled
    public static List<ZoneWisePriceListnew> getZWPListnew(Decimal pageNumber ,Integer recordToDisply, String Searchkey, String FamilyKey, String UserName){
        //List<ZWPListnew> userIdList = new List<ZWPListnew>();

        Integer pageSize = Null;
        Integer offset = Null;
        System.debug('offset>>>' +offset);
        String searchName = '%' + searchKey + '%';

        List<User> u;
        List<String> userIdList = new List<String>();
        String Usr;
        Boolean OtherUser = true;
        String UsrCheck = UserName;
        System.debug('1>>>>' +UsrCheck );
        if(UsrCheck == '' || UsrCheck == null){
            UsrCheck = userinfo.getName();
        }
        Map<Id,AggregateResult> ProdOwners = new Map<id,AggregateResult>([SELECT ProductManager__c Id,count(id) FROM  Product2 where ProductManager__c != null group by ProductManager__c ]);
        Map<String,User> mapUser = new Map<String,User> ();

        for(User ulist : [select id,Name from User where Id in: ProdOwners.KeySet() ]){
            mapUser.put(ulist.Name, ulist);
        }

        System.debug('2>>>>' +mapUser);
        System.debug('3>>>>' +UsrCheck);
        System.debug('4>>>>' +mapUser.containsKey(UsrCheck));

        if(mapUser.containsKey(UsrCheck)){
            Usr = UsrCheck;
        }else{
            Usr = 'All';
            if(mapUser.containsKey(userinfo.getName())){

            }else{
                            OtherUser = false;
            }
        }

        if(Usr == 'All'){
            u = [Select id , Name from User where Id in: ProdOwners.KeySet() ];
        }else{
             u = [Select id, Name from User where Name =: Usr ];
        }

        string UserList='';
        for(User UId : u){
            UserList=UId.id;
            userIdList.add(UserList);
        }


        String[] PBs;
        List<Product2> PrdList;
        List<Product2> PrdListSize;
        List<Product2> PrdListSearch;
        Map<String,PriceBook2> PB = new Map<String,PriceBook2> ();
        Map<String,PriceBookEntry> PBE = new Map<String,PriceBookEntry> ();
        List<ZoneWisePriceList> ZWPList= new List<ZoneWisePriceList>(); 
        List<ZoneWisePriceListnew>ZWPListnew = new List<ZoneWisePriceListnew>();
        /*********************Query String to find number of Products*******************************/
        String strFilter = '';
        string strqry = 'Select Id, Name,ProductCode from Product2  where (Name LIKE :searchName OR ProductCode LIKE :searchName) AND ZoneProduct__c = True';
        if(FamilyKey == 'All'){
            strqry += ' AND b__c != null ';   
        }else{
            strqry += ' AND b__c =: FamilyKey ';    
        }  

        strqry += ' AND ProductManager__c =: userIdList';

        PrdListSize = Database.query(strqry);
        System.debug('QueryString>>>' +PrdListSize.size());
        /***************End of Query String*****************************/
        System.debug('FamilyKey>>>' +FamilyKey );
        System.debug('searchName>>>' +searchName);
                if(FamilyKey == 'All'){
            PrdList = [Select Id, Name,ProductCode,x_c,y__c,z__c,a__c,b__c,m__c,n__c,g__c,h__c,w__c,p__c,k__c
                       from Product2  where ((Name LIKE :searchName OR ProductCode LIKE :searchName) AND b__c != null AND ZoneProduct__c = True AND ProductManager__c =: userIdList) ORDER BY Name,  w__c ];
            System.debug('PrdList>>>' +PrdList);
        }else{

            PrdList = [Select Id, Name,ProductCode,x__c,y__c,z__c,a__c,b__c,m__c,n__c,g__c,h__c,w__c,p__c,k__c
                       from Product2  where ((Name LIKE :searchName OR ProductCode LIKE :searchName) AND b__c =: FamilyKey AND ZoneProduct__c = True AND ProductManager__c =: userIdList) ORDER BY Name,  w__c  ];    
            System.debug('PrdList>>>' +PrdList);
        }

        for(PriceBookEntry p : [select Id, Product2Id,PriceBook2Id,Product2.Name,Product2.Family,Product2.ProductCode,Product2.IsActive,UnitPrice, Product2.x__c, Product2.y__c
                                from PriceBookEntry where Product2Id =: PrdList order by Product2.Name ]){
                                    String ProdPBId = p.Product2Id+'_'+p.PriceBook2Id;
                                    System.debug('ProdPBId>>>' +ProdPBId);
                                    PBE.put(ProdPBId,p);
                                    System.debug('ProdPBId1>>>' +PBE);
                                }
        Map<String,Zone_Wise_Pricing__c> ZWP = Zone_Wise_Pricing__c.getAll();
        List<Zone_Wise_Pricing__c> CustomSettingValue = ZWP.values();
        for(Zone_Wise_Pricing__c ZP : CustomSettingValue ){
            PBs = ZP.PriceBooks__c.split(',');
            System.debug('PBs>>>' +PBs);
            for(PriceBook2 PBList : [Select Id, Name from PriceBook2 where Name =: PBs order by Name]){
                PB.put(PBList.Id , PBList);
                System.debug('PBMap>>>' +PB);
            }
        }

        for(Product2 P : PrdList)    {
            ZoneWisePriceList ZWPL = new ZoneWisePriceList();
            ZoneWisePriceListnew ZWPN = new ZoneWisePriceListnew();
            ZWPL.pageSize = pageSize;
            ZWPN.PageSize = Null;
            ZWPL.page = (Integer) pageNumber;
            ZWPN.Page = Null;
            ZWPL.total = PrdListSize.size();  
            ZWPN.total = Null;
            System.debug('PrdListsize>>>' +PrdListSize.size());
            for(PriceBook2 Zone : PB.values()) {
                System.debug('Keyy>>>' +Zone.id);  
                String ProdPriceKey = P.Id+'_'+Zone.id;
                    ZWPL.ProductName = P.Name;
                    ZWPN.ProductName = P.Name;
                    ZWPL.ProductCode = P.ProductCode;
                    ZWPN.ProductName = P.ProductCode;
                    ZWPL.ValidFrom = P.x__c;
                    ZWPN.ValidFrom = P.x__c;
                    ZWPL.ValidTo = P.y__c;
                   ZWPN.ValidTo = P.y__c;
                    ZWPL.Unit = P.p__c;
                    ZWPN.Unit = P.p__c;
                    ZWPL.NetWeight = P.m__c;
                    ZWPN.NetWeight = P.m__c;                  
                    ZWPL.PriceBreak = P.w__c;
                    ZWPN.PriceBreak = P.w__c;
                    ZWPL.SalesRep = OtherUser;
                    ZWPN.SalesRep = OtherUser;
                    ZWPL.ProdId = P.id;
                    ZWPN.ProdId = P.id;
                    ZWPL.GroupDescription = P.b__c;
                    ZWPN.GroupDescription = P.b__c;
                    ZWPL.UOM = P.g__c;
                    ZWPN.UOM = P.g__c;
                    ZWPL.k = P.k__c;
                    ZWPN.k = P.k__c;
                if(PBE.containsKey(ProdPriceKey))  {  
                    PriceBookEntry price = new PriceBookEntry();
                    price = PBE.get(ProdPriceKey);                    
                    System.debug('PriceBook2Name>>>' +Zone.Name);
                    if(Zone.Name == 'Standard Price Book'){                    
                        ZWPL.BasePrice = price.UnitPrice;
                        ZWPN.BasePrice = price.UnitPrice;  
                        Double OldBasePrice = price.UnitPrice;
                        ZWPL.PBEId = price.id;
                        ZWPN.PBEId = price.id;
                        ZWPL.ProdId = price.Product2Id;    
                        ZWPN.ProdId = price.Product2Id;                        
                    }else if(Zone.Name == 'Zone 1'){
                        ZWPL.Zone1 = price.UnitPrice;
                         ZWPN.Zone1 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 2'){
                        ZWPL.Zone2 = price.UnitPrice;
                         ZWPN.Zone2 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 3'){
                        ZWPL.Zone3 = price.UnitPrice;
                         ZWPN.Zone3 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 4'){
                        ZWPL.Zone4 = price.UnitPrice;
                         ZWPN.Zone4 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 5'){
                        ZWPL.Zone5 = price.UnitPrice;
                        ZWPN.Zone5 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 6'){
                        ZWPL.Zone6 = price.UnitPrice;
                        ZWPN.Zone6 = price.UnitPrice;
                    }else if(Zone.Name == 'Zone 7'){
                       ZWPL.Zone7 = price.UnitPrice;
                        ZWPN.Zone7 = price.UnitPrice;
                    }
                }         
            }
            System.debug('ZWPL>>>' +ZWPL);
            System.debug('offset>>>' +offset);
            //ZWPList.add(ZWPL);
            ZWPListnew.add(ZWPN);
            System.debug('ZWPList>>>' +ZWPList);
            /*******To get the initial values**************/            
        }
       // return ZWPList;   
       System.debug('returned list>>>'+ZWPListnew);
       //return null;
              return ZWPListnew;  
    }
3

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.