0

how can we make a trigger which will only allow user to add just one record not more than one. I have tried making it for unique and for duplicate record entry but how to proceed further Thanks

4
  • your question is not clear. do you mean that trigger is going in infinite loop like recursive trigger? or you just want to check duplicates in trigger? Sep 3, 2014 at 5:14
  • Hi Atul; my requirement is like trigger should allow addition of only one record and after that it should not allow user to enter new rocords.
    – Himanshu
    Sep 3, 2014 at 5:18
  • Do you mean in one object only one record should be there? Sep 3, 2014 at 5:28
  • yes exactly if I am adding more than one record it should not get added and show error lets take object as account currently I have taken account object
    – Himanshu
    Sep 3, 2014 at 5:36

3 Answers 3

2

Remember that:

  • Salesforce is a platform that allows multiple apps to be installed and those apps all have access to and make use of standard objects like Account and may need to manipulate multiple Accounts at once.
  • Even if your app is the only one used in the org, there are other administrative activities such as restoring backups that need to manipulate multiple Accounts (or other objects) at once.

So using a trigger is a risky approach because it impacts all accesses; finding some other place to impose your constraint (e.g. a controller) would be a much safer approach.

If you absolutely have to do this in a trigger, only apply the trigger for specific profiles or users (that relate to your app) so that other profiles or users are not impacted. Or you could guard the logic with a static variable that is set and unset in e.g. a controller so the trigger is only applied in specific transactions. And add a custom setting to allow the trigger to be disabled as a last resort measure.

2
  • I was trying it with trigger for testing on one custom object so if u could provide some stuff it would be really helpful..........
    – Himanshu
    Sep 5, 2014 at 9:54
  • @Himanshu Probably best to pursue this with the other people who answered.
    – Keith C
    Sep 5, 2014 at 10:03
0

Here is how your trigger should look be:

trigger myAccountTrigger on Account(before delete, before insert, before update, 
                                    after delete, after insert, after update) {

Boolean isFirstTime = true; 

if (Trigger.isBefore) {
    if (Trigger.isInsert) {
        for (Account a : Trigger.new) {
        if(isFirstTime == false)        
              a.Adderror('Can not insert more than one record.');
        else
            isFirstTime = false;
        }

  }
}}

To test this i used flowing code:

List<Account> alist =  new List<Account>();
Account a = new Account();
a.name = 'Test1';

Account a1 = new Account();
a1.name = 'Test1';

alist.add(a);
alist.add(a1);

Insert alist;

If you comment 'alist.add(a1);' line then it insert only account object and no error is returned. when you insert more that one acc then it returns error 'Can not insert more than one record'

4
  • hi atul, thanks for help but it is not showing any error for me its adding more than one record.
    – Himanshu
    Sep 3, 2014 at 6:18
  • how are you adding records? Sep 3, 2014 at 6:21
  • through new button in account object
    – Himanshu
    Sep 3, 2014 at 6:23
  • hi atul there is some problem with the code its not working.
    – Himanshu
    Sep 3, 2014 at 10:01
0

If the requirement is for there only ever to be one Account, then a simply query to check the number of Accounts is what you need:

trigger AccountTrigger on Account (before insert) {

    AggregateResult ar = [select count(id) from account];

    for (Account acc : Trigger.New()) {

        if (ar.get('expr0') > 0) {

            acc.addError('Cannot create this Account!');
        }
    }
}

However if it's a maximum of one Account PER USER then you need to check how many Accounts already exist for the owners of the Accounts in trigger.New(). Like so:

trigger AccountTrigger on Account (before insert) {

    Map<Id, Integer> ownerIdAccountCount = new Map<Id, Integer>();

    List<AggregateResult> arList = new List<AggregateResult>();

    for (Account acc : Trigger.New()) {

        ownerIdAccountCount.put(acc.OwnerId, 0);
    }

    arList = [select ownerId, count(id) from account where ownerid in :ownerIdAccountCount.keySet() group by ownerId];

    for (AggregateResult ar : arList) {

        ownerIdAccountCount.put(ar.get('OwnerId'), ar.get('expr0')).
    }

    for (Account acc : Trigger.New()) {

        if (ownerIdAccountCount.get(acc.OwnerId) > 0) {

            acc.addError('Cannot create this Account!');
        }
    }
}

Hope that answers your question, but let me know if not.

Edit: My answer is how to technically do what you need, but I'd suggest taking Keith C's advice seriously and consider if this is a good idea in the long term.

1
  • its not working actually
    – Himanshu
    Sep 5, 2014 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .