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
Rohit Vaidya 3Rohit Vaidya 3 

trigger on account to update contact field

  • Write a trigger on Account to perform the logic described below whenever “Budget” changes. Note that the “Budget” field may sometimes be empty. 
  • The Account “Budget” is to be divided in 2 portions (60% for Sales and 40% for Marketing). The Sales portion is to be equally distributed by all Contacts of that Account that have “Use Sales Budget” checked. The Marketing portion is to be equally distributed by all Contacts of that Account that have “Use Marketing Budget” checked.
SwethaSwetha (Salesforce Developers) 
HI Rohit,
You can use below code to get started
trigger AccountBudgetDistribution on Account (after update) {
    List<Account> updatedAccounts = new List<Account>();
    List<Contact> salesContacts = new List<Contact>();
    List<Contact> marketingContacts = new List<Contact>();
    for(Account acc: Trigger.new){
        Account oldAcc = Trigger.oldMap.get(acc.Id);
        if(acc.Budget__c != oldAcc.Budget__c){
            updatedAccounts.add(acc);
        }
    }
    if(updatedAccounts.size()>0){
        for(Account acc: updatedAccounts){
            //Dividing budget into two portions
            Decimal salesBudget = 0;
            Decimal marketingBudget = 0;
            if(acc.Budget__c!=null && acc.Budget__c!=0){
                salesBudget = acc.Budget__c * 0.6;
                marketingBudget = acc.Budget__c * 0.4;
            }
            //Getting all contacts that have Use Sales Budget checkbox checked
            salesContacts = [SELECT Id, Use_Sales_Budget__c FROM Contact WHERE AccountId = :acc.Id AND Use_Sales_Budget__c = true];
            if(salesContacts.size()>0){
                //Calculating Sales budget per Contact
                Decimal salesBudgetPerContact = salesBudget/salesContacts.size();
                //Updating the budget for each Contact
                for(Contact con: salesContacts){
                    con.Budget__c = salesBudgetPerContact;
                }
            }
            //Getting all contacts that have Use Marketing Budget checkbox checked
            marketingContacts = [SELECT Id, Use_Marketing_Budget__c FROM Contact WHERE AccountId = :acc.Id AND Use_Marketing_Budget__c = true];
            if(marketingContacts.size()>0){
                //Calculating Marketing budget per Contact
                Decimal marketingBudgetPerContact = marketingBudget/marketingContacts.size();
                //Updating the budget for each Contact
                for(Contact con: marketingContacts){
                    con.Budget__c = marketingBudgetPerContact;
                }
            }
        }
        //Updating Contacts with new budget values
        if(salesContacts.size()>0 || marketingContacts.size()>0){
            update salesContacts;
            update marketingContacts;
        }
    }
}

This trigger on Account that divides the budget into two portions (60% for Sales and 40% for Marketing), and then equally distributes each portion among the Contacts of that Account that have the appropriate checkbox checked.

I have created custom fields Budget__c(number), Use_Marketing_Budget__c(checkbox),Use_Sales_Budget__c(checkbox) on contact object that are used in this code

If this information helps, please mark the answer as best. Thank you
Rohit Vaidya 3Rohit Vaidya 3
in the same logic if maketing budget and salesbudget both are true then the equal share of maketing budget and equal share of sales budget should be added and updated in contact budget field