Skip to content

Instantly share code, notes, and snippets.

@sbob-sfdc
Created September 2, 2012 23:40
Show Gist options
  • Save sbob-sfdc/3605663 to your computer and use it in GitHub Desktop.
Save sbob-sfdc/3605663 to your computer and use it in GitHub Desktop.
Workshop 201, Tutorial 4, Step 3, Apex
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.';
}
}
@apocsve
Copy link

apocsve commented Oct 16, 2014

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 >>

@vyuvalv
Copy link

vyuvalv commented Feb 20, 2015

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) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment