4

Hi i could use a little help and i'm totally stuck. I'm trying to write a trigger to update a custom field on my Account object when a update is made on a field in my custom object. The object is linked in a child - parent relationship by the account name.

Updated with code from prior comment:

trigger Acct_UpdateCreditApprv on Credit_Request__c (before insert, before update) { 
    for(Credit_Request__c : Trigger.new) { 
        Account A= [AccountName__c From Credit_Request__c Where Approved__c = true]; 
        Credit_Request__c.Reefer_Approved__c = Account.Approved__c;    
        account.Credit_Request_Approved__c = true;
    } 
} 
  • 1
    Hi. Could you post the code that you already have, so we can better help you? – Guy Apr 2 '14 at 20:14
  • Could you give some more information about what you need to do? The only object you mentioned was Account.. What is the second object? – EricSSH Apr 2 '14 at 20:16
  • Sorry the custom object is named Credit_Request__c – gogorichie Apr 2 '14 at 20:21
  • trigger Acct_UpdateCreditApprv on Credit_Request__c (before insert, before update) { for(Credit_Request__c : Trigger.new) { Account A= [AccountName__c From Credit_Request__c Where Approved__c = true]; Credit_Request__c.Reefer_Approved__c = Account.Approved__c; set account.Credit_Request_Approved__c = true;} } – gogorichie Apr 2 '14 at 20:23
  • Is AccountName__c a Lookup? – EricSSH Apr 2 '14 at 21:03
3

I agree with James in that you may not be using the correct relationship between the two objects.

Can an account ever have more than one credit request as the requirement for a field on the account "Credit Request Approved" would suggest that it can not? That is unless you just want to determine if an account has ever had any request approved.

If an account can only ever have one credit request then you could even have a CreditRequest__c lookup field on the Account and simply use a formula field on the Account of type checkbox with the simple formula:

CreditRequest__r.Approved__c

If you do decide to keep the same relationship you currently have and go down the trigger approach then you need to bare in mind bulk practices. Your trigger would likely look something like:

trigger Acct_UpdateCreditApprv on Credit_Request__c (after insert, after update)
{
    // List of accounts to update or insert (Bulk)
    list<Account> accountsToUpdate = new list<Account>();

    // Handle insert
    if (Trigger.isInsert)
    {
        for(Credit_Request__c newCreditRequest : Trigger.new)
        { 
            // If the request is approved
            if (newCreditRequest.Approved__c)
            {
                Account acc = new Account(Id = newCreditRequest.AccountName__c);
                acc.Credit_Request_Approved__c = true;
                accountsToUpdate.add(acc);
            }
        }
    }

    // Handle Update
    if (Trigger.isUpdate)
    {
        integer count = 0;
        for(Credit_Request__c updatedCreditRequest : Trigger.new)
        {
            Credit_Request__c origCreditRequest = Trigger.Old[count]; // Get the original value

            // Only bother if the record wasn't approved but is now
            if (!origCreditRequest.Approved__c && updatedCreditRequest.Approved__c)
            {
                Account acc = new Account(ID = updatedCreditRequest.AccountName__c);
                acc.Credit_Request_Approved__c = true;
                accountsToUpdate.add(acc);
            }

            count++;
        }
    }

    // Update all accounts in our list
    try
    {
        update accountsToUpdate;
    }
    catch (DMLException ex) {}
}

Hope that helps.

0

Why not use either a lookup or a Master Detail relationship instead of a "virtual" relationship based on AccountName?

If Credit_Request__c is always associated with an Account, I would suggest using a MD relationship from Credit_Request->Account. If you did this, you could create a Cross-Object workflow field update to do this via configuration instead of having to write a trigger. See here for more info: https://help.salesforce.com/HTViewHelpDoc?id=workflow_cross_object_field_updates.htm&language=en_US

If you cannot use a Master Detal relationship / Cross Object, then currently (at least until Spring '14 finally rolls out) you can only do this via the trigger option.

  • yeah i tried that first it worked on my previous field update needs but this cross object has gotten me jammed up. So i'm perusing the trigger route. – gogorichie Apr 2 '14 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.