Problem with JavaScript Execute button

  • 1
  • Question
  • Updated 2 years ago
  • Answered
I am trying to call apex web service using following code in salesforce and it working fine.
and I created javaScript  snippet code in skuid with some changes but I am getting an issue with this.Could please anyone help me to resolve this issue.

Salesforce :
sforce.connection.sessionId = "{!$Api.Session_ID}";
orderedChkBox = "{!Opportunity.Ordered__c}";
oppId = "{!Opportunity.Id}";
idArray = {!GETRECORDIDS($ObjectType.OpportunityLineItem)};

if(idArray.length == 0)
{
alert('You have not selected any line items to convert.Please select the line items.');
}
else if (idArray.length > 0)
{
var executionId =  "" +sforce.apex.execute("createAgentCommission","convertLineItems",{lineItemIds :idArray,oppId : oppId});

if(executionId == 'The Line items which you have selected have already been converted.Please select a different line item.' && executionId != null){
alert(executionId);
}
else{
alert(executionId);
location.reload(true);
}
}


SKUID JS Snippet:

var params = arguments[0],   list=params.list,
   selectedItems = list.getSelectedItems(),
   $ = skuid.$;
var records = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){ 
return item.row.Id; 
}); 

alert('test');
var opp = skuid.model.getModel('Application');
var oppRow = opp.getFirstRow();
var orderedChkBox =oppRow.Ordered__c;
var oppId =oppRow.Id;
alert(oppId);
sforce.connection.sessionId = "{{$Api.Session_ID}}"; 
if(records.length === 0)
{
alert('You have not selected any line items to convert.Please select the line items.');
}
else if (records.length > 0)
{
console.log('sforce-'+sforce.apex);
var executionId =  sforce.apex.execute("createAgentCommission","convertLineItems",{lineItemIds :records[0],oppId:oppId});
if(executionId == 'The Line items which you have selected have already been converted.Please select a different line item.' && executionId !== null){
alert(executionId);
}
else{
alert(executionId);
 window.location.reload();
}
}
Photo of Deleep Manda

Deleep Manda

  • 246 Points 100 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
In the Skuid Snippet, you should remove this line because Skuid already sets sforce.connection.sessionId internally:

sforce.connection.sessionId = "{{$Api.Session_ID}}"; 

And change records[0] in this line to just be records:

var executionId =  sforce.apex.execute("createAgentCommission","convertLineItems",{lineItemIds: records, oppId: oppId});
Photo of Deleep Manda

Deleep Manda

  • 246 Points 100 badge 2x thumb
Hi Zach

Thanks for the quick response.

I just made changes to my code according to your suggestions and tried still it didn't execute apex class.

I made changes to my code like below but it didn't get any result.

var params = arguments[0],   list=params.list,
   selectedItems = list.getSelectedItems(),
   $ = skuid.$;
var records =$.map(selectedItems,function(item){return item.row.Id;});
alert('test');
var opp = skuid.model.getModel('Application');
var oppRow = opp.getFirstRow();
var oppId =oppRow.Id;
alert(oppId);
if(records.length === 0)
{
    alert('You have not selected any line items to convert.Please select the line items.');
}
else if (records.length > 0)
{
    console.log('sforce-'+sforce.apex);
    var executionId =  sforce.apex.execute("createAgentCommission","convertLineItems",{lineItemIds:records,oppId:oppId});
    if(executionId == 'The Line items which you have selected have already been converted.Please select a different line item.' && executionId !== null){
    alert(executionId);
}
else{
     alert(executionId);
     window.location.reload();
}
}
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Are there any error messages in the JavaScript console, or in the Network tab? 
Photo of Deleep Manda

Deleep Manda

  • 246 Points 100 badge 2x thumb
No errors occur Zach and When you click on button it didn't respond anything.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Do you get any alerts?
Photo of Deleep Manda

Deleep Manda

  • 246 Points 100 badge 2x thumb
Hi Zach the above code working fine for me.I made mistake previously instead of calling one snippet called wrong one.