You need to sign in to do that
Don't have an account?
Before Insert Trigger question
Hi,
I wrote a Trigger to prevent the insertion of a new record whithin an SetUp__c sObject if already exists ANY active records in there (the idea is that only one record should exist in that sObject). However the new record is inserted no matter what.
Here is the trigger code:
trigger NewSetUpRecord on SetUp__c (before insert) {
List<SetUp__c> SUR = [SELECT Id, Name, IsDeleted FROM SetUp__c WHERE IsDeleted = false AND id IN :Trigger.new];
boolean exists = false;
if (SUR.size()>0) {
for (SetUp__c sr : SUR) {
if (sr.IsDeleted) {}
else {
exists = true;
break;
}
}
if (!exists) {
insert SUR;
}
}
}
Any ideas why this might be happening?
Thanks!
EUS
Note: You have posted this on the wrong board, you should put it in the Apex Code area for better responses. Not sure if you can swap it now.
If you simply want to prevent any SetUp object from being created unless none currently exists then this should do it (sorry I can't test but it should be close):
This should even work for a mass data load—but you should test. Does it do what you need? Or am I misreading your question?
A few comments to help you see what you did:
Hope this helps.
Regards
MellowRen
All Answers
EUS
Note: You have posted this on the wrong board, you should put it in the Apex Code area for better responses. Not sure if you can swap it now.
If you simply want to prevent any SetUp object from being created unless none currently exists then this should do it (sorry I can't test but it should be close):
This should even work for a mass data load—but you should test. Does it do what you need? Or am I misreading your question?
A few comments to help you see what you did:
Hope this helps.
Regards
MellowRen
Hi MellowRen,
Thanks for you answer! You mention this should even work for a mass data load— so let me ask you something: Where the error messages would be placed on a batch mass data load? ... Where should I access them to see what is going on?
Thanks a lot!
EUS
EUS
It would depend on the tool you are using for mass insert. The DataLoader (and LexiLoader) creates an error report as a csv file. The Excel Connector highlights records that couldn't be inserted and puts an Excel comment (little red triangle in top right of cell) in the ID column of each row.
Other tools would have different ways of doing it.
Regards
MellowRen
Thanks again!