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
Matt Cooper 7Matt Cooper 7 

Apex Trigger To Update Owner On Master Record Based On Picklist Value On Detail Record

Hi,

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!
Best Answer chosen by Matt Cooper 7
Matt Cooper 7Matt Cooper 7
Here's 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;
}

 

All Answers

Gaurav NirwalGaurav Nirwal
You'd need to first figure out whether or not all projects have status 'completed'. To do that, you need 2 roll-up summary fields on Sale__c.

1. Number_of_projects__c which just counts the number of projects linked to the Sale
2. Number_of_completed_projects__c which counts the number of projects with status 'Completed'

Note that this will only work if the relationship is Master-Detail. If it isn't, you will have to either change it to Master-Detail or revert to an APEX trigger.

Then, based on the contents of the 2 roll-up fields, you can create a workflow rule on Sale that checks the current Sale status and substatus. If those are allright, then check if the 2 roll-up fields are equal (and you might want to check that they are >0 as well). If all of those prerequiresites are OK, then update the Sale status and substatus.
Matt Cooper 7Matt Cooper 7
Hi Matthews,

This answer doesn't seem related to my question.  Were you trying to answer someone else's question?
Matt Cooper 7Matt Cooper 7
So I took a stab at this, but I'm getting an error "FATAL_ERROR|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.Name
Trigger.BAO_TL_Approver: line 13, column 1"

Can someone give me some guidance?
trigger BAO_TL_Approver on Bank_Account_Details__c (before insert, before update) {
	
    Set<String> TLApprover = new Set<String>();
    for (Bank_Account_Details__c BAD :trigger.new)
    {
        TLApprover.add(BAD.TL_Approver__c);   
    }
    
    map<string,string> userIdbyName = new map<string,string>();  
    
    for(User u : [select id from User where Name LIKE :TLApprover])
    {
            userIdbyName.put(u.Name,u.Id);
     }
    
  
    String approverId = userIdByName.get('Matt Test Account 2');
    
    
    Apttus__APTS_Agreement__c objAgmt;
    objAgmt.OwnerId = approverId;
      
}

Thanks!
Matt Cooper 7Matt Cooper 7
Here's 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;
}

 
This was selected as the best answer