You need to sign in to do that
Don't have an account?

New to triggers, attempting cross-object update
Good Afternoon,
I am attempting to write an after update trigger that sets the Account status to Active when the Contract is activated. I am new to Apex triggers.
I tried the following, which does not produce a run-time error, but does not change the value of the field:
trigger setAcctActive on Contract (after update) {
if (Trigger.isUpdate) {
for (Contract contract : Trigger.new) {
if ( contract.Status == 'Activated' ) {
Account contractAcct = contract.Account;
contractAcct.AccountStatus__c = 'Active';
}
}
}
}
It seems I need to add a commit or something to the code to put the new field value into the database?
Any help?
I am attempting to write an after update trigger that sets the Account status to Active when the Contract is activated. I am new to Apex triggers.
I tried the following, which does not produce a run-time error, but does not change the value of the field:
trigger setAcctActive on Contract (after update) {
if (Trigger.isUpdate) {
for (Contract contract : Trigger.new) {
if ( contract.Status == 'Activated' ) {
Account contractAcct = contract.Account;
contractAcct.AccountStatus__c = 'Active';
}
}
}
}
It seems I need to add a commit or something to the code to put the new field value into the database?
Any help?
for (Contract contract : Trigger.new) {
if ( contract.Status == 'Activated' ) {
Account contractAcct = contract.Account;
contractAcct.AccountStatus__c = 'Active';
update contractAcct ;
}
}
}
}
trigger setAcctActive on Contract (after update) {
List ac = new List();
for (Contract contract : Trigger.new) {
if ( contract.Status == 'Activated' ) {
Account a = new Account();
a.Id = contract.accountId;
a.AccountStatus__c = 'Active';
ac.add(a);
}
}
update(ac);
}
It's notable that the the accountId is not write-able, so I can't compile:
a.Id = contract.AccountId;
However, the following does work. I see your assumption about multiple accounts, but doesn't each Contract have only one accountId?
Working version:
trigger setAcctActive on Contract (after update) {
for (Contract contract : Trigger.new) {
if ( contract.Status == 'Activated' ) {
Account a = new Account( Id = contract.AccountId );
//a.Id = contract.accountId;
a.AccountStatus__c = 'Active';
update a;
}
}
}
If you update the accounts one at a time inside the loop you're much more likely to run into the governor limits, your much better off to accumulate the set of accounts to update, and do them in one go.
Here is the final working code:
trigger setAcctActive on Contract (after update) {
// Create a list of accounts to update
List<Account> acctList = new List<Account>();
for (Contract contract : Trigger.new) {
// If the contract has been activated...
if ( contract.Status == 'Activated' ) {
Account a = new Account( Id = contract.AccountId );
// ... set the associated Account as active
a.AccountStatus__c = 'Active';
acctList.add ( a );
}
// update the database with the account status changes
update acctList;
}
}