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
jyotijyoti 

Update Parent field on update of child using Apex trigger

I'm writing my first Apex trigger and am running into an error which I am sure is simple to resolve but I can't seem to figure out.
 
Problem: I need to update a field (checkbox) on a parent (Account) record called PFlg when the child's Contract record field, CFlg, is checked.
My Solution: Since workflow field update doesn't allow multiple objects, I thought to use an Apex trigger.
 

trigger Flag on Contract (after update) {

sObject c = new Contract();
Contract d = (Contract)c;

if (trigger.new.CFlg__c.equals(TRUE) )
 
 {
   sObject s = new Account();
   Account f = (Account)s;
   f.PFlg__c = TRUE;

   }

}


This results in an error:

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Contract at line 6 column 5
<SCRIPT type=text/javascript>function initSelectionInEditor() { setSelectionInEditor('Body', 118, 118) }setContentWindow(window);initSelectionInEditor();</SCRIPT>
 
Then I changed it to:
trigger UpdateDocExpressFlag on Contract (after update) {
 
sObject c = new Contract();
Contract d = (Contract)c;
 
for (Contract ddd:Trigger.new)
{

if (ddd.CFlg__c(TRUE) )
 
 {
   sObject s = new Account();
   Account f = (Account)s;
   f.PFlg__c = TRUE;
   }
}
}
 
which resulted in the following error:
Compile Error: Method does not exist or incorrect signature: [SOBJECT:Contract].CFlg__c(Boolean) at line 8 column 5
 
Any thoughts as to what I'm missing?


Message Edited by jyoti on 12-31-2007 07:34 AM
SuperfellSuperfell
the error is on, that's not how you test for true/false condition

if (ddd.CFlg__c(TRUE) )

Also, you need to say which account your trying to update, and actually do the update of the accounts, and i'm not sure why you're doing the sojbect/concrete type casting stuff.

try something like this.

trigger UpdateDocExpressFlag on Contract (after update) {

List ac = new List();

for (Contract c:Trigger.new)
{

if (c.CFlg__c)

{
Account a = new Account();
a.PFlg__c = TRUE;
a.AccountId = c.AccountId;
ac.add(a);
}
}
update(ac);

}
David GreenbergDavid Greenberg
I'm working on a similar problem, so if you find a solution, please do let me know (see my separate thread in the Apex discussion board.)

By the way, I believe Account.Id is not an editable field in Apex.  Is that not correct?


Message Edited by David Greenberg on 01-03-2008 07:17 PM
jyotijyoti
David,
 
Yes you are correct about the Id fields being uneditable.
 
Here is the code I have come up with:
Trigger UpdateDocExpressFlag on Contract (after insert, after update)
{
List<Account> acctList = new List<Account>();
for (Contract contract : Trigger.new) {
  // If the contract has been received from DocExpress
  if (contract.CRT_REC_DOCEXPRESS__c == TRUE ) {
    Account a = new Account(Id = contract.AccountId );
    // Set the DocExpress Flag to TRUE
    a.Rec_d_By_DocExpress__c = TRUE;
    acctList.add (a);
   }
  // update the Accounts' Rec'd from DocExpress flag
  update acctList;
}
}
 
When I execute this code within the application my acctList is 66 and if I do two via an upload the number jumps to 128.  Is this in byte size?  Based on the debug log it looks like it is working correctly but I wanted to clarify that this number is not the actual times it is being sent through the loop.
 
Thanks.


Message Edited by jyoti on 01-09-2008 07:55 AM