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
Garrett MillerGarrett Miller 

Update Field on Master Detail Relationship?

I am trying to write a class to update an Account field whenever an Opportunity is Closed Won, but the field to update is based on a Detail object. 

Here is the situation. 

  • I have 3 objects: Account, Opportunity, and a custom object, Hand_Off__c
  • There is a master detail relationship between Account - Hand_Off__c
  • There is a master detail relationship between Opportunity - Hand_Off__c
  • A Hand_Off__c record is created whenever an Account's first Opportunity is Closed Won
  • There is a field on the Hand_Off__c record, Pricing_Structure__c 
  • There is a similar field,  Pricing_Structure__c, on the Account object
  • When the Hand_Off__c record is created and or updated, I want the Account.Pricing_Structure__c field to be populated with the Hand_Off__c.Pricing__Structure
What I have so far is:
public class HandsOffController {
    public static void setAccountDetailsFromHandOff(Set<ID> oppIds) {
        list<Account> modifiedAccounts = new List<Account>();
        list<Opportunity> handOffOpps = [SELECT Id, Account.Pricing_Structure__c, ( SELECT Id, Pricing_Structure__c	 FROM Hand_Off_to_Client_Success__r	) 	FROM Opportunity WHERE Id IN: oppIds AND stageName='Closed Won'];
        for (Opportunity o: handOffOpps) {
            if(o.Account.Pricing_Structure__c == null) {
                o.Account.Pricing_Structure__c = o.Pricing_Structure__c;
            }
        }
    }
}


I just don't know how to handle the Account-Detail relationship once I actually want to update the field.

Anyone have any tips?

Thanks!

Best Answer chosen by Garrett Miller
HARSHIL U PARIKHHARSHIL U PARIKH
Hmm! I see.

Try to do the following.
1) On account, make Pricing_Structure__c  as simple text field and not a picklist field. I mean if Account's Pricing_Structure__c field has same picklist values as Hand_Off__c object's Pricing_Structure__c field then you don't need to have Account's Pricing_Structure__c as picklists. It can be text field also.
2) In workflow, when you do the field update, take TEXT () out of Pricing_Structure__c and throw it onto the account.
Here is how.
workflow would be same as I described above post but the final field update formula would be something like below.
Field Needs to be updated : Pricing_Structure__c on Account
Formula:
TEXT(Pricing_Structure__c )

Let me know if this helps!

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Few questions...to understand where we stand in the process.

1) Are you able to auto create a Hand_Off__c object record whenever there is a first closed-won opportunity for an account.
2) Are you able to populate Pricing_Structure__c on Hand_Off__c record once it gets automatically created.

If you answer 1) and 2) Yes then you need a workflow on Hand_Off__c object which would update the Pricing_Structure__c field on Account with same value as Pricing_Structure__c on Hand_Off__c

3) What happens when Account Acme, Inc. has three opportunity with different statues? Does it require to create a 3 different Hand_Off__c records?
Garrett MillerGarrett Miller

Hi Govind Guru, 

1) No, the Hand_Off form is currently not auto-created. As it stands now, when Acme Inc has their first Closed Won Opportunity, someone manually creates a Hand_Off__c record on that Opportunity page. ( This procedure is beyond my control so I just have to work around it )
2) No, Pricing_Structure__c is manually input on Hand_Off__c record. 

3) Acme Inc will only every have 1 Hand_Off__c record. That record is only created when their first opportunity, essentially them becoming a customer, is Closed Won. 

HARSHIL U PARIKHHARSHIL U PARIKH
Ok Isee.
So this is the procedure I am understanding.
1) You have one of user creating an Account called Acme, Inc.
2) Now, you have one of your user creating opportunity named "Sample Opp" with Account name as = Acme, INC and status as Closed - Won
3) Then you have someone creating a Hand_Off__c obejct record with Account = Acme, INC. and opportunity = Sample Opp and manually put the value in Pricing_Structure__c field on Hand_Off__c object.
If above are correct and you are trying to thow  Hand_Off__c object's Pricing_Structure__c field into Account object's Pricing_Structure__c field then you can create a cross object workflow! correct..?

Object would be : Hand_Off__c
Conditions would be : Everytime created or edited.
Formula would be:
ISCHANGED(Pricing_Structure__c)
Field Update:
update Account object Pricing_Structure__c field to Pricing_Structure__c
Activate the workflow.

One thing you need to make sure that users don't create a multiple Hand_Off__c record for one single account otherwise which Hand_Off__c record's Pricing_Structure__c is needed to be thrown at account?
If you want to stop that behavior as well then create a roll-up summary field on account which counts Hand_Off__c record. Now, create a validation rule on Hand_Off__c object which would fire whenever that rollup__c value if greater than 1.
This way users won't be able to create multiple Hand_Off__c records for one Account.

 
HARSHIL U PARIKHHARSHIL U PARIKH
Actually the workflow's formula would be this:
OR(

 !ISBLANK(Pricing_Structure__c),
  ISCHANGED(Pricing_Structure__c)

)

 
Garrett MillerGarrett Miller

Hi Govind, 

Yes that procedure looks correct. My only issue is, the field Pricing_Structure__c is a picklist field, so I can't just assign Account.Pricing_Structure__c to the Hand_Off__c Pricing Strucutre, correct?

 

HARSHIL U PARIKHHARSHIL U PARIKH
Hmm! I see.

Try to do the following.
1) On account, make Pricing_Structure__c  as simple text field and not a picklist field. I mean if Account's Pricing_Structure__c field has same picklist values as Hand_Off__c object's Pricing_Structure__c field then you don't need to have Account's Pricing_Structure__c as picklists. It can be text field also.
2) In workflow, when you do the field update, take TEXT () out of Pricing_Structure__c and throw it onto the account.
Here is how.
workflow would be same as I described above post but the final field update formula would be something like below.
Field Needs to be updated : Pricing_Structure__c on Account
Formula:
TEXT(Pricing_Structure__c )

Let me know if this helps!
This was selected as the best answer
Garrett MillerGarrett Miller

Thanks, Govind! 

I really want to know how to write Apex code between this kind of Master-Detail relationship but this will certainly do for now!

Best,

Garrett

HARSHIL U PARIKHHARSHIL U PARIKH
No problem Garrett!
My experience says whenever you have master-detail relationships, lot can be accomplished via point and clicks. But however, for the lookup relationships sometimes some of the things are not possiable via clicks and you need to write code to do the job.
But however, clicks over code always!

The thing we did above is possible using Trigger as well but stick with the clicks since its possible.

If you really want to learn apex from scratch then here is what I invite you do go for (by the way, this is the path I have taken as well),
Website: http://www.sfdc99.com/
Courses on pluralsight:
  1) https://www.pluralsight.com/courses/apex-absolute-beginner-guide-coding-salesforce?utm_source=sfdc99&utm_medium=video&utm_campaign=authordemo
2) https://www.pluralsight.com/courses/apex-fundamental-coding?utm_source=sfdc99&utm_medium=video&utm_campaign=authordemo
3) https://www.pluralsight.com/courses/apex-soql-salesforce?utm_source=sfdc99&utm_medium=video&utm_campaign=authordemo

Do these three courses WITH HOMEWORKS and you will be in much better position to write code sir!

Hope it helps!