Skip to content

Instantly share code, notes, and snippets.

@apocsve
Forked from sbob-sfdc/InvoiceUtilities.cls
Last active August 29, 2015 14:07
Show Gist options
  • Save apocsve/853e227fa3683fe67c74 to your computer and use it in GitHub Desktop.
Save apocsve/853e227fa3683fe67c74 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.';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment