2

I will most likely just re-write my code to work with this way of passing the value to the controller BUT I was curious why the below method doesn't work. Is there a rule about remote action and passing params? ParentId definitely has a value in the JS. I am finding little information about this way of passing. It seems like the only option is the hidden field.

<a href="javascript:void(0)" class="btn" onClick="refreshBankDataFunction(parentId)">Refresh</a>

function refreshBankDataFunction(parentId){
    Visualforce.remoting.Manager.invokeAction(
   'BankDataAttachment.webServiceAuth',
    parentId,
     function(result, event) {
         return parentId;
     },
     {escape:true}
  );
}

@RemoteAction
global static string webServiceAuth(String parentId)
{
    system.debug('got into webServiceAuth: ' + parentId);
}

//This returns: DEBUG|got into webServiceAuth: {}

UPDATE: When I console.log the parentId and the bankButtonRefresh I get:

bankButtonRefresh: <a href="javascript:void(0)" class="btn-sm btn-primary permissionDeny pull-right" onClick="refreshBankDataFunction(a34c0000000jiezAAA)" >Refresh</a>
parentId: a34c0000000jiezAAA

I should also state that I am building the table in JavaScript:

var bankButtonRefresh = '<a href="javascript:void(0)" class="btn-sm btn-primary permissionDeny pull-right" onClick="refreshBankDataFunction('+parentId+')" >Refresh</a>';
2
  • refreshBankDataFunction(parentId) what is parentId here?
    – sfdcfox
    Commented May 15, 2017 at 15:24
  • @sfdcfox I updated the question. I am getting back an ID of a34c0000000jiezAAA. And parentID is a little complicated but I define it earlier in the JS function: var parentId = result[i].Summary.Related_Bank_Data__c;
    – Olivia
    Commented May 15, 2017 at 15:32

1 Answer 1

2
var bankButtonRefresh = '<a href="javascript:void(0)" class="btn-sm btn-primary permissionDeny pull-right" onClick="refreshBankDataFunction('+parentId+')" >Refresh</a>';

You're missing the quotes, so you're trying to access the variable a34c0000000jiezAAA, which is undefined. You should not be building raw HTML strings.

At minimum, write your code as follows:

var bankButtonRefresh = document.createElement('a');
a.href = 'javascript:void(0)';
a.className = 'btn-sm btn-primary permissionDeny pull-right';
// This creates a "closure", so parentId will be correct here.
a.onclick = function() { refreshBankDataFunction(parentId); };
outputElement.appendChild(a);
2
  • why is it bad practice to build a raw HTML string? A simple solution I did was just adding quotes around the parentID like: var parentId2 = "'" + parentId + "'"; Why would this not be a good long term way of passing in the ID?
    – Olivia
    Commented May 15, 2017 at 19:01
  • 1
    @Olivia Any time you use user input, you run the risk of malformed HTML or even intentional injections, or even in this case, writing raw HTML had the effect of introducing a bug that was difficult to find. It is much safer to use document.createElement and use attributes. Some browsers also have better performance when you use document.createElement properly.
    – sfdcfox
    Commented May 15, 2017 at 21:28

You must log in to answer this question.

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