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
renurenu 

Apex Trigger in Live Causing Errors

My Apex Trigger in live is causing errors. I am getting error on beforeupdate and afterupdate.Searched the discussion board and the documentation but did not get how to proceed further. please advice!!

BeforeUpdate:

MutualClientUpdates: execution of BeforeUpdatecaused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call Class.MutualClientApex.beforeinsertupdate1:

Afterupdate:

MutualClientUpdates: execution of AfterUpdate caused by: System.ListException: Duplicate id in list:   Class.MutualClientApex.afterinsertupdate1:

beforeinsert and afterinsert classes also contains the same code. This  should be resolved ASAP as this is in Live and we have lot of issues from this morning. Please Advice!!! 

There is a custom object (related) under the Account Object. When a new custom record is inserted or updated few fields on the Account object need to be updated by checking some conditions.

Trigger
trigger MutualClientUpdates on Mutual_Client__c (before insert,before update,after insert,after update) {
if(trigger.isBefore)
{
if(trigger.isinsert){

Mutual_Client__c[] m=Trigger.new;
MutualClientApex.beforeinsertupdate(m);
 }
if(trigger.isupdate){
Mutual_Client__c[] m=Trigger.new;
MutualClientApex.beforeinsertupdate1(m);
 }
}

if(trigger.isafter)
{
if(trigger.isinsert)
{
Mutual_Client__c[] m=Trigger.new;
MutualClientApex.afterinsertupdate(m);
}
if(trigger.isupdate)
{
Mutual_Client__c[] m=Trigger.new;
MutualClientApex.afterinsertupdate1(m);
}
}
}
Apex Class
public class MutualClientApex {
public static void beforeinsertupdate(Mutual_Client__c [] m){
List<Account> acctList = new List<Account>();

 for( Mutual_Client__c o: m){
if(o.Wholesale_Relationship__c==true)
{
Account a=new Account(Id = o.client_prospect_account__c);

    o.Client_Authorization__c=true;
         a.Client_type__c = 'Wholesale';
   
    acctList.add ( a );
    
   
    }
    else
        {
     Account a=new Account(Id = o.client_prospect_account__c);
      a.Client_type__c = '';
     
      acctList.add ( a );
      
        }update acctList;
                   }
}
public static void afterinsertupdate1(Mutual_Client__C[] m){
List<Account> acctList = new List<Account>();
 for( Mutual_Client__c o: m)
 {
Mutual_Client__c[] az=[select Id from Mutual_Client__c where Accounting_Provider__c=.Accounting_Provider__c and Wholesale_Relationship__c =true];

Account ac =[select Id from Account where Id=.Accounting_Provider__c]; 
if(az.size()>0)
{    
         ac.Status__c='Wholesale';
   
      } 
      else
      {
      ac.Status__c='';
      }    
      acctList.add(ac);
    }
    update acctList;
}
}
SuperfellSuperfell
the before one is likely caused by someone creating a custom object without setting the client_prospect_account__c field, and the second one is caused by some doing a bulk update of the custom objects where multiple rows all point to the same account. You should be able to easily write unit tests that reproduce these 2 cases, then work out the fixes.
renurenu

Thanks for the response. client_prospect_account__c is the required field on the custom object. It does not allow to save the record if this fied is empty. Please advise!!!!!

These are the test cases i wrote. Should these tests satisfy the above issues you mentioned.

static testMethod void beforeinsertupdatetest13() {

Account firmAccount=new Account(Name='UnitTest9',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='UnitTest1',RecordTypeid='0123000000002GvAAI');
insert clientAccount ;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount .id,Wholesale_Relationship__c=true,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Client_Authorization__c,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
//System.assert(val.Wholesale_Relationship__c==true);
//System.assert(val.Client_Authorization__c==true);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
//System.assert(val1.Client_type__c=='wholesale');

Mutual_Client__c[] dd=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.beforeinsertupdate1(dd);
}


static testMethod void beforeinsertupdatetest23() {

Account firmAccount=new Account(Name='UnitTest9',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='UnitTest2',RecordTypeid='0123000000002GvAAI');
insert clientAccount;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Wholesale_Relationship__c=false,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
//System.assert(val.Wholesale_Relationship__c==false);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
//System.assert(val1.Client_type__c=='');


Mutual_Client__c[] ff=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.beforeinsertupdate1(ff);
}



static testMethod void afterinsertupdatetest133() {
Account firmAccount=new Account(Name='UnitTest44',Status__c='Wholesale',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;
//ID key2=firmAccount.id;

Account clientAccount =new Account(Name='UnitTest43',RecordTypeid='0123000000002GvAAI');
insert clientAccount;
//ID key1=clientAccount.id;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=true);

Mutual_Client__c[] case6=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.afterinsertupdate1(case6);
}

static testMethod void afterinsertupdatetest221() {
Account firmAccount=new Account(Name='UnitTest5',Status__c='',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;
Account clientAccount =new Account(Name='UnitTest3',RecordTypeid='0123000000002GvAAI');
insert clientAccount;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=false);

Mutual_Client__c[] case1=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.afterinsertupdate1(case1);
}



Message Edited by renu on 08-20-2008 03:17 PM
renurenu
Please respond!!!!!!!!!!
werewolfwerewolf
Required by a page layout, or hard-required at the field level?  There's a definite chance that your assumption is wrong – perhaps someone figured out a way to put a null in a field you thought was required.
werewolfwerewolf
Also, I would note that the code for your method beforeinsertupdate1 is not specified here so we can't see what the problem is with that.  Additionally, you might think about naming your methods and test methods a little more rationally according to what they do rather than where they are called from – this code is really hard to read.  I generally make methods with names like Apple.eat() rather than Apple.whatidoafteriputitinmymouth().
renurenu

Thanks for the reply. Here is the Renaming version of my code. Apologize for this lenghty code. Please Advise!!!!!

Trigger

trigger MutualClientUpdates on Mutual_Client__c (before insert,before update,after insert,after update) {
    if(trigger.isBefore){
        if(trigger.isinsert){
            Mutual_Client__c[] m=Trigger.new;
           MutualClientApex.BeforeInsertCheck(m);
        }
        if(trigger.isupdate){
            Mutual_Client__c[] m=Trigger.new;
            MutualClientApex.BeforeUpdateCheck(m);
        }
    }

    if(trigger.isafter){
        if(trigger.isinsert){
            Mutual_Client__c[] m=Trigger.new;
            MutualClientApex.AfterInsertCheck(m);
        }
        if(trigger.isupdate){
            Mutual_Client__c[] m=Trigger.new;
            MutualClientApex.AfterUpdateCheck(m);
        }
    }
}

Apex Class

public class MutualClientApex {

//--------------------Start BeforeInsertCheck----------------------------------------------------------------------
public static void BeforeInsertCheck(Mutual_Client__c [] m){

List<Account> acctList = new List<Account>();

for( Mutual_Client__c o: m){
if(o.Wholesale_Relationship__c==true)
{
Account a=new Account(Id = o.client_prospect_account__c);

    o.Client_Authorization__c=true;
         a.Client_type__c = 'Wholesale';
  
    acctList.add ( a );
   
  
    }
    else
        {
     Account a=new Account(Id = o.client_prospect_account__c);
      a.Client_type__c = '';
    
      acctList.add ( a );
     
        }update acctList;
                   }
}


static testMethod void BeforeInsertTest1() {

Account firmAccount=new Account(Name='Sample1',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='Sample2',RecordTypeid='0123000000002GvAAI');
insert clientAccount ;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount .id,Wholesale_Relationship__c=true,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Client_Authorization__c,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
System.assert(val.Wholesale_Relationship__c==true);
System.assert(val.Client_Authorization__c==true);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
System.assert(val1.Client_type__c=='wholesale');
Mutual_Client__c[] cc=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.BeforeInsertCheck(cc);

}


static testMethod void BeforeInsertTest2() {

Account firmAccount=new Account(Name='Sample3',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='Sample4',RecordTypeid='0123000000002GvAAI',Client_type__c='');
insert clientAccount;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Wholesale_Relationship__c=false,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
//System.assert(val.Wholesale_Relationship__c==false);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
//System.assert(val1.Client_type__c=='');
Mutual_Client__c[] dd=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.BeforeInsertCheck(dd);


}
//--------------------End BeforeInsertCheck-------------------------------

//----------------------Start AfterInsertCheck-----------------------------
public static void AfterInsertCheck(Mutual_Client__C[] m){
List<Account> acctList = new List<Account>();
for( Mutual_Client__c o: m)
{
Mutual_Client__c[] az=[select Id from Mutual_Client__c where Accounting_Provider__c=:o.Accounting_Provider__c and Wholesale_Relationship__c =true];

Account ac =[select Id from Account where Id=:o.Accounting_Provider__c];
if(az.size()>0)
{   
         ac.Status__c='Wholesale';
  
      }
      else
      {
      ac.Status__c='';
      }   
      acctList.add(ac);
    }
    update acctList;
}


static testMethod void AfterInsertTest1() {
Account firmAccount=new Account(Name='Sample5',Status__c='Wholesale',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;
//ID key2=firmAccount.id;

Account clientAccount =new Account(Name='Sample6',RecordTypeid='0123000000002GvAAI');
insert clientAccount;
//ID key1=clientAccount.id;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=true);
Mutual_Client__c[] case2=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.AfterInsertCheck(case2);
}

static testMethod void AfterInsertTest2() {
Account firmAccount=new Account(Name='Sample7',Status__c='',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;
//ID key2=firmAccount.id;

Account clientAccount =new Account(Name='Sample8',RecordTypeid='0123000000002GvAAI');
insert clientAccount;
//ID key1=clientAccount.id;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=false);
Mutual_Client__c[] case1=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.AfterInsertCheck(case1);
}
//----------------------------End AfterInsertCheck----------------------------

//----------------------------End BeforeUpdateCheck----------------------------
public static void BeforeUpdateCheck(Mutual_Client__c [] m){
List<Account> acctList = new List<Account>();

for( Mutual_Client__c o: m){
if(o.Wholesale_Relationship__c==true)
{
Account a=new Account(Id = o.client_prospect_account__c);

    o.Client_Authorization__c=true;
         a.Client_type__c = 'Wholesale';
  
    acctList.add ( a );
   
  
    }
    else
        {
     Account a=new Account(Id = o.client_prospect_account__c);
      a.Client_type__c = '';
    
      acctList.add ( a );
     
        }update acctList;
                   }
}

static testMethod void BeforeUpdateTest1() {

Account firmAccount=new Account(Name='Sample9',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='Sample10',RecordTypeid='0123000000002GvAAI');
insert clientAccount ;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount .id,Wholesale_Relationship__c=true,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Client_Authorization__c,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
//System.assert(val.Wholesale_Relationship__c==true);
//System.assert(val.Client_Authorization__c==true);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
//System.assert(val1.Client_type__c=='wholesale');

Mutual_Client__c[] dd=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.BeforeUpdateCheck(dd);
}


static testMethod void BeforeUpdateTest2() {

Account firmAccount=new Account(Name='Sample11',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='Sample12',RecordTypeid='0123000000002GvAAI');
insert clientAccount;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Wholesale_Relationship__c=false,Accounting_Provider__c=firmAccount.id);
insert mc;

ID key = mc.id;
Mutual_Client__c val=[select id,Wholesale_Relationship__c from Mutual_Client__c where id= :key];
//System.assert(val.Wholesale_Relationship__c==false);

ID key1 =clientAccount.id;
Account val1=[select id,Client_type__c from Account where id=:key1];
//System.assert(val1.Client_type__c=='');


Mutual_Client__c[] ff=[select Wholesale_Relationship__c,client_prospect_account__c,Accounting_Provider__c from Mutual_Client__c where id=:mc.id];
MutualClientApex.BeforeUpdateCheck(ff);
}


//--------------------------End BeforeUpdateCheck----------------------------------------------

//---------------------AfterUpdateCheck---------------------------------------------
public static void AfterUpdateCheck(Mutual_Client__C[] m){
List<Account> acctList = new List<Account>();
for( Mutual_Client__c o: m)
{
Mutual_Client__c[] az=[select Id from Mutual_Client__c where Accounting_Provider__c=:o.Accounting_Provider__c and Wholesale_Relationship__c =true];

Account ac =[select Id from Account where Id=:o.Accounting_Provider__c];
if(az.size()>0)
{   
         ac.Status__c='Wholesale';
  
      }
      else
      {
      ac.Status__c='';
      }   
      acctList.add(ac);
    }
    update acctList;
}


static testMethod void AfterUpdateTest1() {
Account firmAccount=new Account(Name='Sample13',Status__c='Wholesale',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;

Account clientAccount =new Account(Name='Sample14',RecordTypeid='0123000000002GvAAI');
insert clientAccount;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=true);

Mutual_Client__c[] case1=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.AfterUpdateCheck(case1);
}

static testMethod void AfterUpdateTest2() {
Account firmAccount=new Account(Name='Sample15',Status__c='',RecordTypeId='0123000000005QBAAY',Division='02d30000000000rAAA');
insert firmAccount;
//ID key2=firmAccount.id;

Account clientAccount =new Account(Name='Sample16',RecordTypeid='0123000000002GvAAI');
insert clientAccount;
//ID key1=clientAccount.id;

Mutual_Client__c mc=new Mutual_Client__c(client_prospect_account__c=clientAccount.id,Accounting_Provider__c=firmAccount.id,Wholesale_Relationship__c=false);

Mutual_Client__c[] case2=[select id from Mutual_Client__c where Accounting_Provider__c=:mc.Accounting_Provider__c and Wholesale_Relationship__c=true];
MutualClientApex.AfterUpdateCheck(case2);
}

//--------------------------End AfterUpdateCheck------------------------------
}
werewolfwerewolf
Yep, I agree with Simon – the o.client_prospect_account__c field is probably null which is why you can't create the account.  Put some System.debug messages around the area where it's failing and you will probably be enlightened.
renurenu

Hi -

I am trying to put the system.debug statements but i am understanding the debug log.

Please guide me how to write/use system.debug log. Also seen the documentation for this .But really i am uabale to follow.

please advise

werewolfwerewolf
Go to Setup->Monitoring->Debug Logs and turn on a debug log for your user.  Then do the process that is causing the error.  You'll see an entry in the debug log.  Press the View link next to it.  There you can see how your Apex ran, and any System.debug statements that were hit will show their results in there.
 
Another option is to replicate the problem using a testMethod and then Run Tests in Eclipse.  There you'll see the output of the logs as the tests are running.