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
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Apex trigger on Child Object to Update Parent Object Status Field

Hi All,

I need help on creating Apex trigger Here is the scenario:
I am having two objects 1. Account  2. Account Branch with a master detail relation ship where Account is Master.
Fields:
Account Branch ->  Branch_Status__c.
Account           ->  Status, Client_Id__c and rollup summary field that will calculate number of Branchs under that account called         No_Of_Branches__c

Now My trigger has to update Account status as follows:
If Client_Id__c = 'X' and No_Of_Branches__c = 0 then Status = 'Closed Prospect'
If Client_Id__c contains SAP or PAS and No_of_Branches__c = 0 then status = Active
If Client_Id__c contains SAP or PAS and No_Of_Branches__c !=0 and Branch_Status_c!=Cancel then status = Active
(Here Branch_Status__c is a field of child object and trigger has to check status of Branches and have to determine cancel if all are on cancel state or not)
If Client_Id__c contains SAP or PAS and No_Of_Branches__c !=0 and Branch_Status__c==cancel then status = Inactive
(Here Branch_Status__c is a field of child object and trigger has to check status of Branches and have to determine cancel if all are on cancel state or not)

Can any one help to frame the trigger. The trigger has to be on the child object.

Thanks,

 
 
Best Answer chosen by uma52551.3972270309784705E12
Anupam RastogiAnupam Rastogi
Hi Uma,

You can first check if Client_Id__c is not null then use contains(). Like shown below - 
if(a.Client_Id__c != '')
{
        if(a.Client_Id__c.contains('SAP') || a.Client_Id__c.contains('PAS')) 
        {
             //--- Do Something
        }
}

For the issue that you have resolved the other way, I would suggest that as a best practise, try avoiding code whenever possible. Using configuration always speeds up execution time.

Thanks
AR

All Answers

Shashikant SharmaShashikant Sharma
Hi, You could read 

http://forceschool.blogspot.in/2011/05/writing-apex-trigger-issues-and_25.html

The trigger example on contact in this blog is an example of trigger on child object to update parent object. You could read all trigger examples and content on the blog to understand triggers more.

Thanks
Anupam RastogiAnupam Rastogi
Hi Uma,

You can do the following to implement this scenario - 

1. Create a new formula field (Branch_Status_Code__c) on Account Branch object. Use formula to set this field to 0 if Branch_Status__c = Cancel and 1 if Branch_Status__c != Cancel.
2. Create a new Roll-Up Summary field (All_Branch_Status__c) on Account that calculates the Sum of the above created new formula field. So basically this field should contain 0 if all branches have Cancel in Branch_Status__c field else 1.
3. Within the Trigger, check your conditions that you have mentioned along with the new roll-up field created on Account.

Sample code containing the conditions to be checked - 
...........
...........
//--- Add other lines of code

    List <Account> AccList = new List<Account>();
    
    For(Account_Branch__c aB : Trigger.new) {
        
        //--- Here Account_Id__c is the field in child object storing the Account Id
        Account a = [select Id, Status, Client_Id__c, No_Of_Branches__c, All_Branch_Status__c from Account where Id = : aB.Account_Id__c];
        if(a.Client_Id__c == 'X' && a.No_Of_Branches__c == 0)
            a.Status = 'Closed Prospect';
        else if((a.Client_Id__c == 'SAP' || a.Client_Id__c == 'PAS') && a.No_of_Branches__c == 0)
            a.Status = 'Active';
        else if((a.Client_Id__c == 'SAP' || a.Client_Id__c == 'PAS') && a.All_Branch_Status__c != 0)
            a.Status = 'Active';
        else if((a.Client_Id__c == 'SAP' || a.Client_Id__c == 'PAS') && a.All_Branch_Status__c == 0)
            a.Status = 'Inactive';
        AccList.add(a);
    }
    update AccList;

..........
.........
Thanks
AR

If you find the reply useful that solves your problem then please mark it as best answer.
Anupam RastogiAnupam Rastogi
Hi Uma,

Did you try out the solution shared?

AR
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi Anupam,

First of all thanks for your help.
I worked this trigger in other way. Actually I have taken one list of branches with cancel date and another list which contains all branches. Compared both and updated the status fields. And coming to my above requirment. (If Client_Id__c contains SAP or PAS and No_of_Branches__c = 0 then status = Active) here I have to check whether the client_id__c contains SAP/ PAS in it i.e; Client_Id__c.contains('SAP')|| Client_Id__c.contains('PAS') but not like Client_Id__c = SAP or PAS.
But this 'contains' is having some problem if the Client_Id__c  is empty when I am trying to insert a record if the list is empty. So at present I am working with this issue.
By any chance do you have any idea how we can check in the if condition without wrighting 'contains'.
Ex: if Client_id__c = 2561SAP then how we can check this in if condition?
my soultion for this is if(Client_Id__c.contains('SAP')|| Client_Id__c.contains('PAS')) which is not working in all cases.
Thanks!
Anupam RastogiAnupam Rastogi
Hi Uma,

You can first check if Client_Id__c is not null then use contains(). Like shown below - 
if(a.Client_Id__c != '')
{
        if(a.Client_Id__c.contains('SAP') || a.Client_Id__c.contains('PAS')) 
        {
             //--- Do Something
        }
}

For the issue that you have resolved the other way, I would suggest that as a best practise, try avoiding code whenever possible. Using configuration always speeds up execution time.

Thanks
AR
This was selected as the best answer
Anupam RastogiAnupam Rastogi
Hi Uma,

Was your issue resolved?

Thanks
AR

Please mark the relevant reply as best answer if it solved the problem.
uma52551.3972270309784705E12uma52551.3972270309784705E12
Thank you AR