-2

Able to pass lightning component values to server side. But passed Ids are not working in server side

For ex: caseIDStr contains values.Please check below image

enter image description here

But this is not building below query

   clist=[Select Id, 
CaseNumber,GSS_Business_unit__c,GSS_Support_Level__c,owner.Name from Case 
     where Id IN:caseIDStr];

Instead of caseIDStr IF i Hardcode Ids ,i will get the reults

I suspect that probelm caseIDStr, why its breaking

VFPAGE:

 <apex:page standardController="case" extensions="Gss_CsHelathCheckExtension"  
 recordsetVar="lstAllUserData" sidebar="false" showHeader="false">
      <apex:includeScript value="/lightning/lightning.out.js" />
<apex:includeLightning />
<div id="lightning" />
  <script>
// alert(typeof "{!lstSelUserDataIds}");
   var  myList = '{!lstSelUserDataIds}';
   var tempmylist = myList.split(',');
//alert(typeof tempmylist);
    $Lightning.use("c:Gss_CloneSkillsToApp", function(){
        $Lightning.createComponent(
            "c:Gss_CaseHealthCheck",
            {"caselist" : tempmylist},
            "lightning",
            function(cmp){});
    });
</script>

controller:

public class Gss_CsHelathCheckExtension {
ApexPages.StandardSetController setCon;
public String[] lstSelUserDataIds{get; set;}
public Gss_CsHelathCheckExtension(ApexPages.StandardSetController standardController)
{
    system.debug('---constructor--');
    this.setCon =standardController;
    lstSelUserDataIds = new String[]{};
        for(case selectedRec : (List<case>)setCon.getSelected())
    {
        lstSelUserDataIds.add(selectedRec.id);
        system.debug('---lstSelUserDataIds--'+lstSelUserDataIds);
    }
}

}

Component:

<!-- caselist value from vf page
 <aura:attribute name="caselist" type="String[]"/>

Js side:

var action=component.get("c.getCaseDetails");
action.setParams({
     "caseIDStr":component.get("v.caselist"),
});
action.setCallback(this,function(response) {
   var state=response.getState();
   if(state==='SUCCESS'){
       component.set("v.caseSerExcList",response.getReturnValue());
   }
});
$A.enqueueAction(action);  

Server side:

@AuraEnabled
public static List<object> getCaseDetails(String [] caseIDStr){
 system.debug('caseIDStr---->'+caseIDStr);
  //for this query id values are not passing.If I do hardcode it will work
   if(!caseIDStr.isEmpty()){
        clist=[Select Id, 
CaseNumber,GSS_Business_unit__c,GSS_Support_Level__c,owner.Name from Case 
     where Id IN:caseIDStr];
    }
    system.debug('clist --->'+clist);

    for(Case c: clist ){
        caseSerExcList.add(new CaseAssociatedSerExc(c));
    }
    system.debug('caseSerExcList--->'+caseSerExcList);
}

2 Answers 2

2

Visualforce's default formatting for a list of strings is in the form:

[value1,value2]

Which means your code looks like this:

var  myList = '[value1,value2]';

So when you split it, you get an array that looks like this:

var tempmylist = ['[value1','value2]'];

Which is what ultimately gives you that bizarre value in your Lightning Component's controller.

Instead, serialize the list as JSON initially and present that to your script:

public String lstSelUserDataIds{get; set;}

...

lstSelUserDataIds = JSON.serialize(dataIds);

...

// DO NOT USE QUOTES HERE //
var myList = {!lstSelUserDataIds}; 
$Lightning.use("c:Gss_CloneSkillsToApp", function(){
    $Lightning.createComponent(
        "c:Gss_CaseHealthCheck",
        { caselist : myList },
        "lightning",
        function(cmp){});
});
3
  • Problem is : If I remove quotes myList = {!lstSelUserDataIds} component isnot loading.Value will not pass from vf page to Lightning components. If I add it in quote value will pass from vf page to lightning components Feb 11, 2019 at 15:14
  • still that issue is present. If I pass myList values in quotes.still same issue present Feb 11, 2019 at 15:35
  • @AnnappaPH You need to json serialize, it should be a Srting instead of Dtring[].
    – sfdcfox
    Feb 11, 2019 at 16:27
2

As I noted in a comment on your previous question, the debug log clearly shows that what is reaching your Apex controller is not a List of Ids. It is a List containing exactly one String, whose value is the literal text '[5001k..., 5001k...]'.

You can demonstrate this for yourself just by running in the Developer Console

System.debug(new List<String>{ '001', '002' });

which yields

23:00:23:004 USER_DEBUG [1]|DEBUG|(001, 002)

Note that there are no square brackets - Apex does not use square brackets when debugging a List.

The code that populates v.caselist in your JavaScript controller is at fault here. I was mistaken, sfdcfox identified the root cause in your Lightning Out code.

The contents of your attribute are not what you think they are, and you need to work through your code to determine why.

2
  • I am happy to accept u r suggestion..I don't think it will be not the ans for my question..I am cross verifying it.. and I mentioned it in comments my debug results and root causes Feb 11, 2019 at 15:42
  • I missed the root cause. sfdcfox found it.
    – David Reed
    Feb 11, 2019 at 15:47

You must log in to answer this question.

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