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
Mahi123Mahi123 

If I am updating the child record then parent update when all child records equal to same Status.

Hi,

 

  I would to write a Bulkify Trigger.

 

Here is my criteria,

 

             I have two objects 1. Parent 2. Child

 

Child and Parent both have the same field Status values( Open, Closed And Completed)

 

If All child records have the same value Open then the parent status should update to Open. IF all child records status is Closed then my parent status should update to Closed.

 

I can write for one record it is very simple but I would like to write for Bulkify Records.

@anilbathula@@anilbathula@

Hi ,

 

Writing bulk triggers are best practices of apex.

To bulkify code use collections like (list,set,map)

check this link how to bulkify your trigger.

http://techblog.appirio.com/2009/08/writing-bulk-triggers-for-salesforcecom.html

http://salesforcetrekbin.blogspot.in/2010/04/bulk-triggers-in-salesforce.html

http://sivatejaforce.wordpress.com/2011/02/03/hello-world/

 

 

Mahi123Mahi123

Thanks for your reply 

 

I have done in this way. It is working fine but i didn't use relationships excatly . every exanple is Parent to child but not child to parent

 

Please modify my triiger or let em know where i have to  do changes

 

trigger UpdateServiceOrder on Activitie__c (after update)
{
public List<Activitie__c> listOfAct=trigger.new;
List<ID> listOfSoIds=new List<ID>();
public List<Service_Order__c> updateListOfSo=new List<Service_Order__c>();
for(Activitie__c act : listOfAct)
{
listOfSoIds.add(act.Service_Order__c);
}
public List<Service_Order__c> listOfSo=[select id,
name,
Status__c,
No_Of_Activities__c from Service_Order__c where ID IN :listOfSoIds];
public List<Activitie__c> lisOfact=[select id,Name, Status__c, Service_Order__c from Activitie__c where Service_Order__c IN:listOfSoIds];
for(Service_Order__c so : listOfSo)
{
Integer canCount=0;
Integer compCount=0;
Integer closCount=0;
for(Activitie__c a : lisOfact )
{
if(a.Service_Order__c == so.id)
{
if(a.Status__c=='Cancelled')
canCount++;
else if(a.Status__c=='Completed')
compCount++;
else if(a.Status__c=='Closed')
closCount++;
}

}
if(so.No_Of_Activities__c==canCount || so.No_Of_Activities__c==compCount || so.No_Of_Activities__c==closCount )
{
if(so.No_Of_Activities__c==canCount)
so.Status__c='Cancelled';
else if(so.No_Of_Activities__c==compCount)
so.Status__c='Completed';
else if(so.No_Of_Activities__c==closCount)
so.Status__c='Closed';
}
so.Problem_Description__c=String.valueOf(canCount);
//so.Status__c='Closed';
updateListOfSo.add(so);
}
update updateListOfSo;

}

PremanathPremanath

Can you Exaplain with Exact Scenario.

Here Which is parent and which is child.

When you want to perform this Action, I mean When parent is updating or child is updating Let me know.

 

 

Prem

Mahi123Mahi123

Ok, Thanks So much for your reply.

 

I have two objects 1.Service_Order__c (parent) is having Status Field    2. Activitie__c (Child) is also having Status field(values Completed, Closed adn Cancelled).

 

  One Service_Order__c have multiple Activites.

 

my criteria is if all Activitie__c Status is Equal to "Closed" then the Parent object(Service_Order__c) Status Field update to "Closed". If all Activitie__c Status__c is equal to "Completed" then Service_Order__c Status__c field shoud update to "Completed".

 

    If you didn't understand anything please let em know.

PremanathPremanath

You can try like this

May be you will get i hope

 

trigger UpdateServiceOrder on Activitie__c (after update)
{
    List<ID> listOfSoIds=new List<ID>();
    Map<,String> varmap=New Map<,String>();
    Map<,String> varmap2=New Map<,String>();
    public List<Service_Order__c> updateListOfSo=new List<Service_Order__c>();
    for(Activitie__c act : listOfAct)
    {
    listOfSoIds.add(act.Service_Order__c);
    }
    public List<Service_Order__c> listOfSo=[select id,
    name,
    Status__c,
    No_Of_Activities__c from Service_Order__c where ID IN :listOfSoIds];
    public List<Activitie__c> lisOfact=[select id,Name, Status__c, Service_Order__c from Activitie__c where Service_Order__c IN:listOfSoIds];
    for(Activitie__c a: lisOfact)
    {
        if(a.Status__c=='Completed' || a.Status__c=='Closed'){
            if(a.Status__c=='Completed'){
                varmap.put(a.id,a.Status__c);
            }
            else if(a.Status__c=='Closed'){
                varmap2.put(a.id,a.Status__c);
            }
        }
        else{
            break;
        }

    }
    if(varmap.size()>0 && varmap1.size()==0){
        for(Service_Order__c so:listOfSo){
            so.Status__c='Completed';
            updateListOfSo.add(so);
        }
    }
    else if(varmap.size()==0 && varmap1.size()>0){
        for(Service_Order__c so:listOfSo){
            so.Status__c='Closed';
            updateListOfSo.add(so);
        }
    }
    
    update updateListOfSo;

}

 

 

Prem

Mahi123Mahi123

Hi,

 

   My code is working perfectly but i am writting 2 soql quries insted of 2 can't write a single Query.

 

and you have written the logic 

 

 if(varmap.size()>0 && varmap1.size()==0){
        for(Service_Order__c so:listOfSo){
            so.Status__c='Completed';
            updateListOfSo.add(so);
        }
    }
    else if(varmap.size()==0 && varmap1.size()>0){
        for(Service_Order__c so:listOfSo){
            so.Status__c='Closed';
            updateListOfSo.add(so);
        }

 

if 1 activity then logic is correct if more than one Activity i think it won't be work,

 

ex: if 2 actvities with status open, Closed respectively, then varmap2 size definitely size is 1 and greater than 0 eventhough you are updating the Service Order Status to Closed.

 

Any way my logic is working fine but 

 

 public List<Service_Order__c> listOfSo=[select id,
                                                    name,
                                                     Status__c,
                                                      No_Of_Activities__c from Service_Order__c where ID IN :listOfSoIds];
    public List<Activitie__c> lisOfact=[select id,Name, Status__c, Service_Order__c from Activitie__c where Service_Order__c  IN:listOfSoIds];            

 

 

instead of writting above 2 soql quiries can't we write a single query.

PremanathPremanath

If your code is working then you can follow that code only

 

But i have used Map here i think it will work for all..

First understood what i have done.. Then reply me

 

 

 

Prem

Mahi123Mahi123

ok, can you help me in this.

 

public List<Service_Order__c> listOfSo=[select id,
                                                    name,
                                                     Status__c,
                                                      No_Of_Activities__c from Service_Order__c where ID IN :listOfSoIds];
    public List<Activitie__c> lisOfact=[select id,Name, Status__c, Service_Order__c from Activitie__c where Service_Order__c  IN:listOfSoIds];            

 

 

instead of writting above 2 soql quiries can't we write a single query.

PremanathPremanath

Based on your requirement you need Both

 

You should know what you are doing..

 

If you want single query write a inner query .

 

public List<Service_Order__c> listOfSo=[select id,
                                                    name
                                                     ,Status__c,(select id,Name, Status__c, Service_Order__c from Activities__r where Service_Order__c  IN:listOfSoIds)
                                                      ,No_Of_Activities__c from Service_Order__c where ID IN :listOfSoIds];

 

 

 

Regards

Prem

Mahi123Mahi123

Premanath I am excpeting this answer whether am i right or not.

 

I tried many times to solve with only query i don't know exactly can i do with one query or not. my wish is i would become a very good programmer that's why i an trying to write as many as less lines.

 

Any way thanks a lot Premanath.

PremanathPremanath

I already told to you

Based on Requirement you need to write quries see my code and your code

compare both

Both queries why your writing know about that first

Then remove as your wish

 

Mahi123Mahi123

Ok Sure,

 

it is giving below error

 

ErrorError: Compile Error: Method does not exist or incorrect signature: varmap1.size() at line 32 column 27
PremanathPremanath

Oh

that is

 

 

varmap2.size();

 

See the code , i didn't seen that.. very small error you didn't observe that??/

Mahi123Mahi123

Sorry

 

 please, Can you explain a little bit about below code

 

if(varmap.size()>0 && varmap2.size()==0){
for(Service_Order__c so:listOfSo){
so.Status__c='Completed';
updateListOfSo.add(so);
}
}

 

here every service order Status you are changing to Completed. you are not saying to add only that Activity Service Order.

How it is possible.

PremanathPremanath

See here

 

for(Activitie__c a: lisOfact)
    {
        if(a.Status__c=='Completed' || a.Status__c=='Closed'){
            if(a.Status__c=='Completed'){
                varmap.put(a.id,a.Status__c);
            }
            else if(a.Status__c=='Closed'){
                varmap2.put(a.id,a.Status__c);
            }
        }
        else{
            break;
        }

    }

 

In above code Those who 'completed' all records will come into varmap

if some records 'Closed' that will come to varmap2

 

so i have just given the condition like that

 

if(varmap.size()>0 && varmap2.size()==0){
for(Service_Order__c so:listOfSo){
so.Status__c='Completed';
updateListOfSo.add(so);
}
}

 

 

In parent object i am just updating the field

 

Understood the code your self

no one explai n you

How can you become good programer,, Don't irritate me