Skip to content

Instantly share code, notes, and snippets.

@pritaunk
Created October 26, 2017 19:28
Show Gist options
  • Save pritaunk/74530dee69b591d482c04ac14c3bbb6b to your computer and use it in GitHub Desktop.
Save pritaunk/74530dee69b591d482c04ac14c3bbb6b to your computer and use it in GitHub Desktop.
Requirement 1 RAD Presentation
//*REQUIREMENT 1* Before a Stock Item can be created, check that there is not already
// a stock item record with a matching name.
//the below creates the new stockitems in a set that
//doesn't allow for duplicates. Trigger.new to ensure all new records created
//for this object get added to this Set
set <string> stockitems = new set <string> ();
if (trigger.isInsert) {
for (stock_item__c stock : trigger.new) {
stockitems.add(stock.Item_Name__c);
}
}
//here we are checking for duplicates in the existing records to ensure
//we don't enter new stock items with the same name
//SOQL query to get a list of existing stock items from the org calling for the stock name
list <stock_item__c> stocklist = new list <stock_item__c> (
[select id, Item_Name__c from stock_item__c where Item_Name__c in : stockitems]);
if (trigger.isInsert) // was getting a nullpointerexception error when trying to delete in UI
//read about the error and added this if trigger.new statement - was able to delete to test req.2
for(stock_item__c newstock : trigger.new) {
//checking if the list has a duplicate item then add the below words to the new stock item name
if(stocklist.size() > 0)
newstock.adderror('duplicate item' + newstock.Item_Name__c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment