You need to sign in to do that
Don't have an account?
uma52551.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,
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,
You can first check if Client_Id__c is not null then use contains(). Like shown below -
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
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
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 - Thanks
AR
If you find the reply useful that solves your problem then please mark it as best answer.
Did you try out the solution shared?
AR
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!
You can first check if Client_Id__c is not null then use contains(). Like shown below -
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
Was your issue resolved?
Thanks
AR
Please mark the relevant reply as best answer if it solved the problem.