0

I have a scenario where I am trying to create an Apex Trigger that updates the Owner on a master record based on the selection made on a picklist in a detail record.

My master object is:

 Label: Agreement

 Object Name: APTS_Agreement

 API Name: Apttus__APTS_Agreement__c

My detail object is:

 Label: Bank Account Details

 Object Name: Bank_Account_Details

 API Name: Bank_Account_Details__c

The picklist field is located on the Bank Account Details object and has a list of users in the system:

 Field Label: TL Approver

 API Name: TL_approver__c

Basically, one user will be coming in and creating an Agreement record. They will then create a Bank Account Opening detail record off that Agreement record that will have the TL Approver field on it. Whoever they select from the TL Approver field should then become the Owner of the Agreement record.

Can anyone help me out with this?

Thanks!

2 Answers 2

0

Matt,

you can achieve this with a Cross Object Field Update ( Workflow Rule ) .. you don't need a Trigger for this.

the reference link will help you on how to setup the cross object field updates..

1
  • Vamsi, Wouldn't I have to create a separate Workflow Rule for each picklist value for the TL Approver field? From my knowledge, there's no way to make a Field Update with a formula (which would allow me to compare multiple picklist values to actual users). Therefore, to make a field update to change the Owner to the master record, I would have to do a new Field Update for each user/picklist value. If I have 15 different users (list values) for the TL Approver field, that means I would have to create 15 separate workflow rules. Or am I just missing some functionality?
    – Matt
    Oct 28, 2014 at 23:23
0

Here is the code that I built that appears to be working:

trigger BAO_TL_Approver on Bank_Account_Details__c (before insert, before update) {

    Map<ID, Apttus__APTS_Agreement__c> parentAgmts = new Map<ID, Apttus__APTS_Agreement__c>();
    List<Id> listIds = new List<Id>();
    List<Apttus__APTS_Agreement__c> ApttusAPTSAgreementList = 
        new List<Apttus__APTS_Agreement__c>();


    for (Bank_Account_Details__c childObj : Trigger.new) {
        listIds.add(childObj.Bank_Account_Details__c);
    }

    parentAgmts = new Map<Id, Apttus__APTS_Agreement__c>([SELECT id, Name 
        FROM Apttus__APTS_Agreement__c WHERE ID IN :listIds]);   

    for (Bank_Account_Details__c BAD :trigger.new)
    {
        Apttus__APTS_Agreement__c myParentAgmt = parentAgmts.get(BAD.Bank_Account_Details__c);
        String TLApprover = BAD.TL_Approver__c;   
        map<string,string> userIdbyName = new map<string,string>();  

        for(User u : [select Name, id from User where Name LIKE :TLApprover]){
            userIdbyName.put(u.Name,u.Id);
        }

        if(userIdbyName.containsKey(TLApprover)){
            myParentAgmt.OwnerId = userIdbyName.get(TLApprover);
            ApttusAPTSAgreementList.add(myParentAgmt);
        }
    }

    update ApttusAPTSAgreementList;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .