You need to sign in to do that
Don't have an account?
Btormar
}
Apex Trigger to update all related child records
I'm new to Triggers and are trying to put in a trigger that updates all related child records on Opportunity. I have a custom object with a Master-Detail to the Opportunity and I'm trying to accomplish the following in a trigger:
Everytime a new record on the custom object is created I'd like it to uncheck a checkbox on all records for that Opportunity ID (and subsequently check that box for the newly created record but since the first part doesn't work I haven't tried to code this yet).
I also haven't put in the loop yet as since it won't even update a single record so far.
Here's my (bad) code:
trigger flag_fsc on Funnel_Scorecard__c (after update, after insert)
{
{
CustomObject__c[] fsc = trigger.new;
CustomObject__c[] fsc_update;
CustomObject__c[] fsc_update;
fsc_update[0] = [SELECT id, Most_Recent__c FROM Custom Object__c WHERE id = :fsc[0].opportunity__c] AND fsc[0].Most_Recent__c = TRUE;
fsc_update[0].Most_Recent__c = False;
update fsc_update[0];
update fsc_update[0];
}
And here's the error message:
Apex trigger flag_fsc caused an unexpected exception, contact your administrator: flag_fsc: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.flag_fsc: line 9, column 14
Any ideas anyone?
Thanks!
fsc_update[0] = [SELECT id, Most_Recent__c FROM Custom Object__c WHERE id = :fsc[0].opportunity__c] AND fsc[0].Most_Recent__c = TRUE;
There are a couple of typos there that I'm assuming are just in your psuedo code for your post, so I'll skip those. Primarily you're trying to assign the result of the query to fsc_update[0] (the first item in the array) and the array wasn't initialized before-hand. Doing it this way also assumes that you'll only ever get one response from your query. If, however, you remove the [0] from fsc_update[0] = , that should work fine as the query will return a full array (even if it's only one item), which will initialize your variable.
The second issue I see is that you're not checking to see if your query actually returned data of if there were no records found, you're just assuming one item is returned. Here's how I'd re-write your trigger:
I made some guesses about what you're trying to do there.
This is great - thanks EJW!
However... I still get the following error message when it triggers - any suggestions?
Apex trigger flag_fsc caused an unexpected exception, contact your administrator: flag_fsc: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a03S0000000EqVJIA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, flag_fsc: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.flag_fsc: line 33, column 38: Trigger.flag_fsc: line 39, column 9
Message Edited by EJW on 01-29-2008 12:28 PM
Works great - Thanks so much EJW!
Message Edited by Btormar on 02-08-2008 12:14 PM
Hello EJW. I have seen most of your replies. hope you can suggest me. I know this is very huge code. But please
help me with some solution.thanks a lot
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.ClientUpdates: line 19, column 1
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.ClientUpdates: line 19, column 1
if(trigger.isBefore)
{
if(trigger.isinsert){
ClientApex.beforeinsertupdate(m);
}
if(trigger.isupdate){
Client__c[] m=Trigger.old;
ClientApex.beforeinsertupdate(m);
}
}
if(trigger.isafter)
{
if(trigger.isinsert)
{
Client__c[] m=Trigger.new;
ClientApex.afterinsertupdate(m);
}
if(trigger.isupdate)
{
Client__c[] m=Trigger.new;
ClientApex.afterinsertupdate(m);
}
}
public static void beforeinsertupdate(Client__c [] m){
List<Account> acctList = new List<Account>();
if(o.Wholesale_Relationship__c==true)
{
Account a=new Account(Id = o.client_prospect_account__c);
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 beforeinsertupdatetest1() {
insert a;
insert b; //Line 35
Client__c val=[select id,Client_Authorization__c,Wholesale_Relationship__c from Client__c where id= :key];
System.assert(val.Wholesale_Relationship__c==true);
System.assert(val.Client_Authorization__c==true);
Account val1=[select id,ADP_Client_type__c from Account where id=:key1];
System.assert(val1.Client_type__c=='wholesale');
}
static testMethod void beforeinsertupdatetest2() {
insert a;
insert b; //Line 54
Client__c val=[select id,Wholesale_Relationship__c from _Client__c where id= :key];
System.assert(val.Wholesale_Relationship__c==false);
Account val1=[select id,ADP_Client_type__c from Account where id=:key1];
//System.assert(val1.ADP_Client_type__c=='');
public static void afterinsertupdate(Client__C[] m){
List<Account> acctList = new List<Account>();
for( Client__c o: m)
{
Client__c[] az=[select Id from Client__c where Accounting_Provider__c=.Accounting_Provider__c and Wholesale_Relationship__c =true];
if(az.size()>0)
{
ac.Status__c='Wholesale';
}
else
{
ac.Status__c='';
}
acctList.add(ac);
}
update acctList;
}
static testMethod void afterinsertupdatetest1() {
Account c=new Account(Name='UnitTest4',Status__c='Wholesale');
insert c;
ID key2=c.id;
insert a;
ID key1=a.id;
insert b;
ID key3 = b.id;
Account val1=[select id,Status__c from Account where id=:key2];
Account c=new Account(Name='UnitTest5',Status__c='');
insert c;
ID key2=c.id;
insert a;
ID key1=a.id;
insert b;
ID key3 = b.id;
Account val1=[select id,Status__c from Account where id=:key2];
//System.assert(val1.Status__c=='');
}
Nice work here!
I have a smiliar situation with a minor caveat...
I have two custom objects (parent & child). on the child, i capture:
1) start date
2) end date
3) charges ($$)
i want to get a rolling 12mo average, starting with the most recent child object. i may have a child from june 2010 >> june 2011, or (if today is june) feb 2010 >> feb 2011.
I'd want to "flag" feb 2011, roll back for 12 months and tally up the charges.
Do you know how I can alter the code to approach this?
Many thanks in advance.
^^ In addition, I should mention that the 12mo average should be captured on the parent object.
Thanks again
As I was building out the solution, I wanted to see on a KSBO record the total amount of revenue at risk represented by the rollup of the associated Escalations. This turned out to be pretty tough to figure out. Why? Well for a number of reasons. The right way to show a running total of something in a parent record is to use a Rollup field. Easy enough, except the
Bottom line is that Process Builder is your friend, and Clicks Not Code is a mantra to live by. But then again, knowing how to write a little bit of APEX code (and perhaps downloading a halfway decent editor like Eclipse to do this work in) will serve you well in the long run and is a useful skill to possess.