4

This code results in this trace in the log.

javascript helper:

var bookname = 'Standard Price Book';
action.setParams({pricebookName: bookname});
action.setCallback(this, function(response) { ...

apex controller:

public static List<PricebookEntry> pdt_getProducts(String pricebookName) {
    system.debug('pricebookName:');
    system.debug(pricebookName);

log trace:

14:30:07:003    USER_DEBUG    [44]|DEBUG|pricebookName:
14:30:07:003    USER_DEBUG    [45]|DEBUG|Standard Price Book

However, this code results in this trace in the log.

javascript helper: (passed a literal string)

action.setParams({pricebookName: 'Standard Price Book'});
action.setCallback(this, function(response) { ...

apex controller: (unchanged)

public static List<PricebookEntry> pdt_getProducts(String pricebookName) {
    system.debug('pricebookName:');
    system.debug(pricebookName);

log trace: (the literal is not passed through)

14:26:05:001    USER_DEBUG    [44]|DEBUG|pricebookName:
14:26:05:001    USER_DEBUG    [45]|DEBUG|null

It seems that a string literal and a string variable are not treated the same. That makes it more difficult to make well-functioning programs quickly.

3
  • It would be great if someone could show me that I'm wrong, and you can pass literal strings. I guess that's my question. Can I pass literal strings? If I can, how can I do it? Aug 4, 2016 at 19:56
  • Can you edit your post to fill out the Javascript with more complete context?
    – Adrian Larson
    Aug 4, 2016 at 20:20
  • try double quotes
    – SEuser
    Aug 4, 2016 at 20:48

1 Answer 1

2

Technically, you are not passing correct JSON in the second example. All JSON values can be:

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.

Ref here

Change your call to:

action.setParams({"pricebookName": "Standard Price Book"});

If it still fails, you have found a bug. If not, Salesforce is parsing JSON to the letter of the API and has rejected your single quote enclosed string.

3
  • That is not JSON. That is a Javascript object. The single-quote and the double-quote are treated the same in Javascript. If it were JSON, I would have to say {"pricebookName": "Standard Price Book"}. Crockford didn't want to explain to folks that certain words were reserved (e.g. for) so he required that all the keys be enclosed in quotes. Aug 4, 2016 at 21:53
  • Pah! Just to be complete, I tried it with double-quotes. It worked. I was very confused. So I went back to single-quotes. It still worked. Apparently, my system was in a strange state earlier when I encountered the problem. I can't reproduce it. Thanks for your time and help. Aug 4, 2016 at 22:11
  • Sorry, I didn't explain myself correctly - I was meaning that the param is passed as JSON to the Salesforce backend and that it's possible that something is going wrong here - although it looks like it handles both Javascript objects and JSON objects just fine. Aug 4, 2016 at 22:38

You must log in to answer this question.

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