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
BrandiTBrandiT 

Error message when deploying trigger

I'm trying to deploy a pretty simple trigger into production.  It works fine in my sandbox, but when I deploy I get all kinds of error message on line 4. 


Here's the trigger:

 

trigger AutoCltGroup on Account (before insert, before update) {

   Client_Groups__c CGU = [Select ID From Client_Groups__c  Where Name='UNGROUPED' limit 1];
   client_groups__c CG996 = [Select ID From Client_Groups__c where Name='RPM996' limit 1];
   Client_Groups__c CG997 = [Select ID From Client_Groups__c where Name='RPM997' limit 1];

    
 for (Account a : Trigger.new) {
    if(a.cmr_number__c == '996')

{
      a.Client_Group__c =CG996.id;

    } 

 

    if(a.cmr_number__c == '997')

{
      a.Client_Group__c =CG997.id;

    } 


    if(a.Client_Group__c==null && a.cmr_number__c != '996' && a.cmr_number__c != '997')

{
      a.Client_Group__c =CGu.id;

    } 
}

 

 

}

 

 

Here's the error I'm getting:

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoCltGroup: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.AutoCltGroup: line 4, column 29: []", Failure Stack Trace: "Class.Accoun...


 

I would appreciate any help you can give me with this one.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SrikanthKuruvaSrikanthKuruva

Did you check running the following queries in the environment where you are deploying the trigger?

Client_Groups__c CG997 = [Select ID From Client_Groups__c where Name='RPM997' limit 1];

Client_Groups__c CGU = [Select ID From Client_Groups__c  Where Name='UNGROUPED' limit 1];
client_groups__c CG996 = [Select ID From Client_Groups__c where Name='RPM996' limit 1];

 

Are your queries returning the rows as expected? i think one of these queries is not retruning any row so you are getting the error.

All Answers

SrikanthKuruvaSrikanthKuruva

Did you check running the following queries in the environment where you are deploying the trigger?

Client_Groups__c CG997 = [Select ID From Client_Groups__c where Name='RPM997' limit 1];

Client_Groups__c CGU = [Select ID From Client_Groups__c  Where Name='UNGROUPED' limit 1];
client_groups__c CG996 = [Select ID From Client_Groups__c where Name='RPM996' limit 1];

 

Are your queries returning the rows as expected? i think one of these queries is not retruning any row so you are getting the error.

This was selected as the best answer
JayantJayant

CG997 is actually null, i.e. no record meets the criteria.

Instead use

list <Client_Groups__c> CG997 = [Select Id from Client_Groups__c where Name = 'RPM997' limit 1];

 

and then execute your actual logic with in If (CG997.size() > 0){ }

otherwise use this -

 

Client_Groups__c CG997 = null;

CG997 = [Select ID From Client_Groups__c where Name='RPM997' limit 1];

 

This will also sort out the problem but in this case, the statement

a.Client_Group__c =CG997.id; 

would throw a nullpointer exception.

 

Infact try to use all queries with list and then execute code if list does contain any record only.

 

Thanks a lot !

 

 

BrandiTBrandiT

well the error makes much more sense now, thanks so much!  So how can I update the client group lookup field with the id pulled in the query:

 

trigger:

trigger

AutoCltGroup onAccount (beforeinsert, beforeupdate) {

 

LIST <

Client_Groups__c> CGs = [Select ID FromClient_Groups__cWhereName='UNGROUPED'limit1];

LIST <

Client_Groups__c> CG996 = [Select ID FromClient_Groups__cWhereName='RPM996'limit1];

LIST <

Client_Groups__c> CG997 = [Select ID FromClient_Groups__cWhereName='RPM997'limit1];

 

for (Account a : Trigger.new) {

 

if(a.cmr_number__c == '996'){

a.Client_Group__c = CG996.id;

}

if(a.cmr_number__c == '997'){

a.Client_Group__c =CG997.id;

}

if(a.Client_Group__c==null && a.cmr_number__c != '996' && a.cmr_number__c != '997'){

a.Client_Group__c = CGs.id;

}

}

}

 

Error:

Save error: Initial term of field expression must be a concrete SObject: LIST<Client_Groups__c>

on line

a.Client_Group__c = CG996.id;

 

Thanks again for your help!!

BrandiTBrandiT

Nevermind, I figured it out  :)

 

Here is my completed code:

 

trigger

AutoCltGroup onAccount (beforeinsert, beforeupdate) {

 

//LIST <Client_Groups__c> CGs = [Select ID From Client_Groups__c Where Name='UNGROUPED' limit 1];

//LIST <Client_Groups__c> CG996 = [Select ID From Client_Groups__c Where Name='RPM996' limit 1];

//LIST <Client_Groups__c> CG997 = [Select ID From Client_Groups__c Where Name='RPM997' limit 1];

for (Account a : Trigger.new) {

 

if(a.cmr_number__c == '996'){

 

LIST <

Client_Groups__c> CG996 = [Select ID FromClient_Groups__cWhereName='RPM996'limit1];

 

if(CG996.size() != 0) {

 

a.Client_Group__c = CG996[0].id;

} }

if(a.cmr_number__c == '997'){

 

LIST <

Client_Groups__c> CG997 = [Select ID FromClient_Groups__cWhereName='RPM997'limit1];

 

if(CG997.size() != 0) {

 

a.Client_Group__c =CG997[0].id;

} }

if(a.Client_Group__c==null && a.cmr_number__c != '996' && a.cmr_number__c != '997'){

 

LIST <

Client_Groups__c> CGs = [Select ID FromClient_Groups__cWhereName='UNGROUPED'limit1];

 

if(CGs.size() != 0) {

 

a.Client_Group__c = CGs[0].id;

} }

}

}

JayantJayant

The problem you might face now is that while a batch is in process (eg. a csv import using Data Loader), you would definitely hit the 100 SOQLs governor limit as you actually put the SOQL inside a loop. Instead try this -

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

trigger AutoCltGroup on Account (before insert, before update) {

list <Client_Groups__c> CGs = new list <Client_Groups__c> ();
list <Client_Groups__c> CG996 = new list <Client_Groups__c> ();
list <Client_Groups__c> CG997 = new list <Client_Groups__c> ();

CGs = [Select ID From Client_Groups__c Where Name='UNGROUPED' limit 1];
CG996 = [Select ID From Client_Groups__c Where Name='RPM996' limit 1];
CG997 = [Select ID From Client_Groups__c Where Name='RPM997' limit 1];

for (Account a : trigger.new)
{
    if (a.cmr_number__c == '996' && CG996.size()>0)
        a.Client_Group__c = CG996[0].id;

    else if (a.cmr_number__c == '997' && CG997.size()>0)
        a.Client_Group__c = CG997[0].id;
       
    else if (CGs.size()>0)
        a.Client_Group__c = CGs[0].id;   
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

Infact if you are sure that Client Group is always going to be one corresponding to 996, 997 or UNGROUPED and UNGROUPED would always exist then, you may use just an else instead of else if (CGs.size()>0) in last conditional as it is a generic condition (to ensure all records are covered).

Otherwise, you need to look into the fact that what happens to the records that doesn't meet any of above criteria (including ANDs).

 

If you further want to optimize, you may actually store the Client Group Ids and Names in a  map, so that you just require a single SOQL or else if you are sure that all three Groups would always exist, you may modify the SOQL like  -

/////////////

CGs = [Select ID From Client_Groups__c Where Name='UNGROUPED' or Name = '996' or Name = '997' order by Name];

/////////////

 

Now CGs[0], CGs[1] and CGs[2] would always have records corresponding to  996, 997 and Ungrouped respectively.

So that you just fire 1 query instead of current 3.

 

BrandiTBrandiT

Thanks for the input.  You are right actually about hitting the governor limits.  I revised my code per your suggestions, and it works perfectly now!

 

Thanks again for all your help!