0

My VF page stops working when I have the following code inside the page. I am unable to figure out what is causing the errors.

function sendToCart() {
var skuSearch = name+" "+size+" "+amt+" "+flv;
var outSku;
<apex:form>
<apex:actionFunction name="getSkus" action="{!skuSearch}" rerender="outSku">
<apex:param name="skey" value = "skuSearch" assignto="{!skey}"/>
</apex:actionFunction>
</apex:form>
}

The corresponding apex class is as follows:

public class skuControl{

// The action function parameter gets set here - this is the input
public String skey {get; set;}

// The output is set here so it can be re-rendered into the page
public Product__C outSku {get; set;}

public PageReference skuSearch() {
   // TODO why is this only returning on and anarbitrary one at that?
   // Pass in the skey via a bind variable
   List<Product__c> skus = [
           select ItemSKU__c
           from Product__c
           where Description__c
           LIKE :skey
           ];
   // Pass out the value
   outSku = skus[0];
   return null;
   }
}

EDIT: I updated the apex class to as below and I changed the JS function as above. I checked the debugger on my web browser and it gives the error:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Thanks in advance

1
  • On the error after the edit, see the link I provided about Visualforce debugging. You can look at the data going back and forth in the Network tab. I don't know what might be causing it. Note that this site is for questions and answers: best not to keep editing after you have accepted and answer.
    – Keith C
    Commented Jul 1, 2018 at 9:44

1 Answer 1

1

You should always check in your browser's Console for errors and use the various other debugging tools your browser offers - see e.g. How do I start to debug my own Visualforce/JavaScript?

Copy more exactly the example in the apex:actionFunction documentation e.g. you have to call the automatically generated JavaScript function getSkus from your JavaScript and nest the apex:param inside the apex:actionFunction.

The way to return a value is to set it in a property of the controller:

public class skuControl{

    // The action function parameter gets set here - this is the input
    public String skey {get; set;}

    // The output is set here so it can be re-rendered into the page
    public Product__C outSku get; set;};

   public PageReference skuSearch() {
       // TODO why is this only returning on and anarbitrary one at that?
       // Pass in the skey via a bind variable
       List<Product__c> skus = [
               select ItemSKU__c
               from Product__c
               where Description__c
               LIKE :skey
               ];
       // Pass out the value
       outSku = skus[0];
       return null;
   }
}

Then within the re-rendered part of your Visualforce page the new value will be available in Visualforce expressions such as {!outSku}.

1
  • I modified the classes as above and I tried to match the style give by the apex:actionFunction documentation but it still gives an error. Please see the above edit. Many thanks
    – R83nLK82
    Commented Jul 1, 2018 at 9:21

You must log in to answer this question.

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