function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Uves RavatUves Ravat 

For every invoice have just one credit note

Hi, 

 

I want to create trigger or class that will only allow the user to create credit note per invoice and also make sure for every invoice there is matching credit note.

 

Any help???

 

Cheers

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

This is basically the pseudocode that should give you what you need.

 

List<Credit_Note__c> note = [SELECT Invoice__c from Credit_Note__c WHERE Invoice__c IN :(set of parent Invoices)];
...
if (note.contains currentNote.Invoice__c)
  currentNote.Invoice__c.addError('Note already contained for this invoice.');

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

 

This should show how to create a basic trigger.

 

Good luck!

All Answers

Damien_Damien_

You could have a trigger, that on creation of a CreditNote runs a query to see if there already exists another credit note on the invoice.  If there is another one, you force your trigger to fail out with an error.

Mackey316Mackey316

would be able to give an example because am new to force.com and apex

Damien_Damien_

This is basically the pseudocode that should give you what you need.

 

List<Credit_Note__c> note = [SELECT Invoice__c from Credit_Note__c WHERE Invoice__c IN :(set of parent Invoices)];
...
if (note.contains currentNote.Invoice__c)
  currentNote.Invoice__c.addError('Note already contained for this invoice.');

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

 

This should show how to create a basic trigger.

 

Good luck!

This was selected as the best answer