Created
October 22, 2017 17:58
-
-
Save pritaunk/3f6e3eb70f167b3d044a4a21422c5e8d to your computer and use it in GitHub Desktop.
Final Project Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*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> (); | |
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]); | |
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