Skip to content

Instantly share code, notes, and snippets.

@sbob-sfdc
Created September 2, 2012 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbob-sfdc/3605654 to your computer and use it in GitHub Desktop.
Save sbob-sfdc/3605654 to your computer and use it in GitHub Desktop.
Workshop 201, Tutorial 3, Step 4.3, Apex
public class InvoiceUtilities {
// class method to renumber Line Items for a given Invoice number
// returns a string that indicates success or failure
public 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.';
}
}
@cindynor
Copy link

i couldn't get this to give back any records until i changed the SELECT statement to use "LIKE" as in:
Invoice__c invoice = [Select i.Name, (Select Name From Line_Items__r ORDER BY Name)
From Invoice__c i
Where i.Name LIKE :invoiceName LIMIT 1];
However, then the input parameter had to change to use % instead of - so the "INV-0004" had to become "INV%0004" and that messed up the rest of the workbook exercises.
DId anyone else have this trouble? How did you solve it?

@gdemarcosSFDC
Copy link

I'm having issues with the reference to the Arrays in the app. invoice.Line_Items__r does not work at all. I double check if the Object had some misspelling or something but it doesn't. Can't reference the Line Items per invoice :(

@rafal00785
Copy link

@cindynor I had the same trouble. Here is my solution:

List<Invoice__c> invoices =     
            [Select i.Name,(Select Name From Line_Items__r ORDER BY Name)
            From Invoice__c i
            Where i.Name =  'INV–0004'];//It work (dash copied from Debug Log from Developer Console) 
        //                  'INV-0004'];//It doesn't work (dash typed from keyboard or copied from sample code)
        //  look at the different length of dashes

Dash from Debug Dev Console you can get in this way:

List<Invoice__c> invoices =     
            [Select i.Name,(Select Name From Line_Items__r ORDER BY Name)
            From Invoice__c i ];
for (Invoice__c invoice : invoices) {
        System.debug(invoice.Name);
        }

@gjolapamo
Copy link

My code executed pretty ok. I noticed after the execution however that my "Invoice Number" field format is without the dash so that's INV0004 for me.

thanks rafal00785 I have also noted your approach.

@Srilaxmi15
Copy link

Hi Faced same issue..Here is my solution.
My code executed.I just copied the Invoice number from Record and used in code. The dash length is same as developer console.

String s = InvoiceUtilities.renumberLineItems('INV–0011');

@cmcguinness
Copy link

Srilaxmi15 is correct, copy and paste the invoice number from the application; for some reason it's a different dash. In the workbook, they use U+2013, En Dash, instead of what's in the record, a simple U+002D, Hyphen Minus sign.

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