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
Jerrad SchmittleJerrad Schmittle 

APEX Trigger Issue - HELP!

PLEASE HELP - I am trying to create a new Opportunity but I keep getting this error message:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger trg_Opportunity caused an unexpected exception, contact your administrator: trg_Opportunity: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times): Trigger.trg_Opportunity: line 15, column 1

...I was told that it has to do with an APEX trigger that I have set. I do not know how to go about turning off the APEX trigger or if there is something I can modify to allow the creation of manual Opportunities.

Please Advise.

Jerrad
hemant ranahemant rana
Hi Jerrad --- if you want to turn of the trigger---
1) Go to setup
2) Go to customize.
3) gor to opportunity and select Trigger 
4) After it opens select "trg_Opportunity" trigger and edit it and deselect the Active checkbox.

Then try creating opportunity
AshlekhAshlekh
Hi,

If you can provide the code of trigger than we can find out the error and provide you the soluiton.

And 

If you want to stop trigger to fire when you create data than you can do this by clcik on Setup --> in App Setup area -->Click on Develop-->click on Apex Trigger 

Now you can see the list of Trigger  click on edit link ot that trigger which is throwing error. I think the trigger name is trg_Opportunity this.

After click on edit link you can see there is check box Is Active uncheck this checkbox, then this trigger will not fire



IF it helps you than please mark it as a solution and ENJOY APEX
James LoghryJames Loghry
Can you post the Trigger / Apex where the exception is encountered?

My guess without seeing the code is that you're requerying the records in your trigger when you don't need to and/or you're querying all records from an object when you don't need to.
Deepak Kumar ShyoranDeepak Kumar Shyoran
You only want to turnoff means to deactivate that trigger then go to Setup> Develop > Apex Trigger and the press edit for the trigger you want to deactivate and unselect the Active checkbox and then press quick save or save button.
Jerrad SchmittleJerrad Schmittle
Thanks for the replies... here is the APEX code for the trigger...

trigger trg_Opportunity on Opportunity (before update, before insert) {
    Map<string, Id> oppAccountNameMap = new Map<string, Id>();
    Map<Id, Id> oppAccountIdMap = new Map<Id,Id>();

    // Get the id of Account : 'Opportunities Without Accounts'
    List<Account> accList = new List<Account>([select Id from account where name = 'Opportunities Without Accounts']);

    //Create Map of AccountName - OppId
    for(Opportunity oppObj : Trigger.New)
    {
       oppAccountNameMap.put(oppObj.BC_Account_Lookup__c, oppObj.Id);
    }
   
    //Create Map of OppId - AccId
    for(Account acc : [select Id, Name from account where Name in :oppAccountNameMap.keyset()])
    {
        for(string accName : oppAccountNameMap.keyset())
        {
            if(acc.Name == accName)
            {
                oppAccountIdMap.put(oppAccountNameMap.get(accName), acc.Id);
            }
        }
    }
   
    //Set the correct AccountId for each Opp and set 'Opportunities Without Accounts' for other Opportunities
    for(Opportunity oppObj : Trigger.New)
    {
        if(oppAccountIdMap.keyset().contains(oppObj.Id))
            oppObj.AccountId = oppAccountIdMap.get(oppObj.Id);
        else
        {
            if(accList.size() == 1)
                oppObj.AccountId = accList[0].Id;
            else
            {
                //If there is no 'Opportunities Without Accounts' account then throw error.
                oppObj.AccountId.AddError('No Account to assign');
            }
        }
    }
}

I don't want to disable the trigger entirely, is there something that can be changed that will not prevent me from creating a manual Opportunity?

Jerrad
Deepak Kumar ShyoranDeepak Kumar Shyoran
The above trigger will prevent you to create or update with No Account on Opportunity and if you want to disable that functionality and want to create Opportunity with empty Account then use below code.
trigger trg_Opportunity on Opportunity (before update, before insert) {
    Map<string, Id> oppAccountNameMap = new Map<string, Id>();
    Map<Id, Id> oppAccountIdMap = new Map<Id,Id>();

    // Get the id of Account : 'Opportunities Without Accounts'
    List<Account> accList = new List<Account>([select Id from account where name = 'Opportunities Without Accounts']);

    //Create Map of AccountName - OppId
    for(Opportunity oppObj : Trigger.New)
    {
       oppAccountNameMap.put(oppObj.BC_Account_Lookup__c, oppObj.Id);
    }
   
    //Create Map of OppId - AccId
    for(Account acc : [select Id, Name from account where Name in :oppAccountNameMap.keyset()])
    {
        for(string accName : oppAccountNameMap.keyset())
        {
            if(acc.Name == accName)
            {
                oppAccountIdMap.put(oppAccountNameMap.get(accName), acc.Id);
            }
        }
    }
   
    //Set the correct AccountId for each Opp and set 'Opportunities Without Accounts' for other Opportunities
    for(Opportunity oppObj : Trigger.New)
    {
        if(oppAccountIdMap.keyset().contains(oppObj.Id))
            oppObj.AccountId = oppAccountIdMap.get(oppObj.Id);
        else
        {
            if(accList.size() == 1)
                oppObj.AccountId = accList[0].Id;
           /* else
            {
                //If there is no 'Opportunities Without Accounts' account then throw error.
                oppObj.AccountId.AddError('No Account to assign');
            } */
        }
    }
}
Please mark it as best solution to your problem if it does solve your problem.