+ Start a Discussion
Grv JainGrv Jain 

@Jernej Cesar 16

I have Two custom  Object Account And Opportunity 
Account :Parent
Opportunity:Chlid
on the parent object, I have a checkbox:Request,
On the child object, I have a Picklist: Model,Completed,runnig
In opportunity  Selected the Piclist Value Model And Running When Parent Account Checkbox is True otherwise Select Picklist Value Runnng When Parent Account Checkbox is False
how  to write a Apex Trigger Code

thanks in advance 
Best Answer chosen by Grv Jain
santanu boralsantanu boral
Hi,

Please find the code below.

Trigger
 
trigger AccountTrigger on Account (after update) 
{
   AccountTriggerHelper.executeAction(Trigger.oldMap,Trigger.newMap);
}

Apex class
 
public class AccountTriggerHelper
{
	
	public static void executeAction(Map<Id,Account> oldMap, Map<Id,Account> newMap)
    {
        Set<Id> accSetIds = new Set<Id>(); //holds the updated Account Ids
        for(Account acc:newMap.values())
        {
             Account oldAccount = oldMap.get(acc.Id);
            if(acc.Request__c != oldAccount.Request__c)
            {
                accSetIds.add(acc.Id);
            }           
        }
        
        //retrieve the list of Opportunity based on updated accounts
        List<Opportunity> lstOpportunity = [SELECT Id, Picklist__c, Account.Request__c FROM Opportunity WHERE AccountId in:accSetIds];
        if(lstOpportunity.size()>0)
        {
            for(Opportunity opptyObj:lstOpportunity)
            {
                if(opptyObj.Account.Request__c == true)
                {
                    opptyObj.Picklist__c = 'Model;Running';
                }
                else
                {
                    opptyObj.Picklist__c = 'Running';
                }
            }
            update lstOpportunity;
        }
    }
}

This code is perfectly running. If you are happy with this, please mark this answer.

All Answers

santanu boralsantanu boral
You can use process buider to achive this functonality, no need to write apex code.
Grv JainGrv Jain
Thanks Santanu,

I know seneraio is solved by functionality. But i write a apex code So please help me.because I am new i Salesforce Apex Coding Area.
Thaks In Advance
santanu boralsantanu boral
Hi,

Please find the code below.

Trigger
 
trigger AccountTrigger on Account (after update) 
{
   AccountTriggerHelper.executeAction(Trigger.oldMap,Trigger.newMap);
}

Apex class
 
public class AccountTriggerHelper
{
	
	public static void executeAction(Map<Id,Account> oldMap, Map<Id,Account> newMap)
    {
        Set<Id> accSetIds = new Set<Id>(); //holds the updated Account Ids
        for(Account acc:newMap.values())
        {
             Account oldAccount = oldMap.get(acc.Id);
            if(acc.Request__c != oldAccount.Request__c)
            {
                accSetIds.add(acc.Id);
            }           
        }
        
        //retrieve the list of Opportunity based on updated accounts
        List<Opportunity> lstOpportunity = [SELECT Id, Picklist__c, Account.Request__c FROM Opportunity WHERE AccountId in:accSetIds];
        if(lstOpportunity.size()>0)
        {
            for(Opportunity opptyObj:lstOpportunity)
            {
                if(opptyObj.Account.Request__c == true)
                {
                    opptyObj.Picklist__c = 'Model;Running';
                }
                else
                {
                    opptyObj.Picklist__c = 'Running';
                }
            }
            update lstOpportunity;
        }
    }
}

This code is perfectly running. If you are happy with this, please mark this answer.
This was selected as the best answer