-
-
Save sbob-sfdc/3605663 to your computer and use it in GitHub Desktop.
global with sharing class InvoiceUtilities { | |
// class method to renumber Line Items for a given Invoice number | |
// returns a string that indicates success or failure | |
webservice static String renumberLineItems(String invoiceName) { | |
// create a copy of the target Invoice object and it's Line Items | |
Invoice__c invoice = [Select i.Name, (Select Name From Line_Items__r ORDER BY Name) | |
From Invoice__c i | |
Where i.Name = :invoiceName LIMIT 1]; | |
// loop through each Line Item, renumbering as you go | |
Integer i = 1; | |
for (Line_Item__c item : invoice.Line_Items__r) { | |
item.Name = String.valueOf(i); | |
System.debug(item.Name); | |
i++; | |
} | |
// update the Line Items in one transaction, rollback if any problems | |
// and return error messages to the calling environment | |
try { | |
database.update(invoice.Line_Items__r); | |
} | |
catch (DmlException e) { | |
return e.getMessage(); | |
} | |
// on success, return a message to the calling program | |
return 'Line items renumbered successfully.'; | |
} | |
} |
Works for me but from a custom button with 'JavaScript in the custom button that leverages the AJAX Toolkit to make SOAP calls from JavaScript' This is from page 94 of the Winter 14 Force.com workbook. To make it work in Developer Console (without AJAX), change lines 1 & 5 to following.
- public class InvoiceUtilities{
// class method to renumber Line Items for a given Invoice number
// returns a string that indicates success or failure
5. public static String renumberLineItems(String invoiceName) {
fresh5447 - The parameter name for the method does not match between the JavaScript supplied for the button and the source code provided here. You can solve 2 ways. The JavaScript in the button thinks the parameter name is "invoice". You can change this source code to have the parameter name of invoice, but then you must change the local variable of the method from invoice to something else. The better way would be to go back to your button and change the parameter name in the JavaScript to match this method signature which is "invoiceName". That will be one change and it will then jive with this code.
Sorry this is 5 months later...just saw this.
fresh5447 ,I am also getting the same error. Were you able to fix this.?
I tried the solution above, but it dint fix the issue. I am pretty much ensuring that i am passing the same parameter as Apex Class to javascript button call.
to add : my error msg is bit different :
No operation available for request {http://soap.sforce.com/schemas/package/InvoiceUtilities}renumberLineItems, please check the WSDL service
Any suggestion on how to go about it?
Hi, i have the following code:
global with sharing class InvoiceUtilities {
webservice static String renumberLineItems(String invoiceName){
List<Factura__c> facturas = [SELECT Name, (SELECT Name FROM Partidas__r ORDER BY Name) FROM Factura__c WHERE Name = :invoiceName LIMIT 1];
if (!facturas.isEmpty()) {
Factura__c factura = facturas[0];
Integer i = 1;
for (Partida__c item : factura.Partidas__r)
{
item.Name = String.valueOf(i);
System.debug(item.Name);
i++;
}
try {
Database.update(factura.Partidas__r);
}
catch (DmlException e){
return e.getMessage();
}
return invoiceName+': Line items re-numbered successfully';
}
else
{
return invoiceName+': No records found on this invoice.';
}
}
}
For this workshop, but when i click on re-ordering button, i got the following message "No records found on this invoice." and actually the invoice have line items.
Why is given me an empty list?
You could help me with this?
I'm new on apex, and I leaning, is my SOQL code wrong?
Ok I found the error, here is the solution if somebody need it.
In the javascript code is the error, replace you javascript code for this:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}
var result = sforce.apex.execute("InvoiceUtilities","renumberLineItems",{"invoiceName":"{!Factura__c.Name}"});
alert(result);
window.location.reload();
Note that the paramater name is not << invoice >> like the workshop explain, is << "invoiceName" >> like the name of the parameter given to the method << renumberLineItems >>
Had the same Error Alert, glad to stumbleupon this post that helped me figure it out...
The Javascript button that use the SOAP API can call the Apex Class and transfer the String Variable into to it only when I changed the definition of the Class and Method to be global and use the webservice..
FROM:
public class InvoiceUtilities {
public static String renumberLineItems(String invoiceName) {
INTO:
global with sharing class InvoiceUtilities {
webservice static String renumberLineItems(String invoiceName) {
Hey is this for sure accurate? : I copied and pasted it inside the salesforce develope apex classer area and I am still getting this message.
A problem with the OnClick JavaScript for this button or link was encountered:
{faultcode:'soapenv:Client', faultstring:'No such parameter invoice defined for the operation, please check the WSDL for the service.', }