+ Start a Discussion
GrrrrrrrrrrrrrGrrrrrrrrrrrrr 

Trigger for Child Records

Hello everyone.

   I am pretty sure that what I need to do is write a trigger to accomplish my goal, but I have not written one before and need some help with the code.  I will try to outline this as best I can, and any help is appreciated.

 

I have a parent Custom Object called Job__c

I have a child Custom Object called Bid__c

   On Bid__c I have a field called Status__c that is a picklist with the values "Awarded" and "Lost"

 

One Job__c can have 1 to many Bid__c

 

When one Bid__c Status__c is changed to "Awarded" I need for all remaining Bid__c Status__c to change from "Pending" to "Lost"

 

I have absolutely no idea how to accomplish this.  I have read through some of the posts about triggers in here, but I have never done anything with one before, and I am pretty lost.

 

Here is what I have so far, which is nothing, and I am already not sure if its right.  (I can't wait to get beat up on a Test Class) 

 

trigger updateBids on Bid__c(after update){
     for(Bid__c c : trigger.old) {

     }

}

 

Can someone please help me to fill in the missing blanks?


Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

just tweaked this so the trigger will only update other children that are in a pending status, since that is in your reqs.  easy to see where to change that (just take out the if statement near the end about the status of the bid = pending)

 

trigger updateOtherChildrenFromChildUpdate on Bid__c (after update, after insert)
{
    Bid__c[] bids = new Bid__c[0];
    Bid__c[] bid;
    Id[] jobIds = new Id[0];

    for ( Bid__c b : trigger.new )
    {
        if ( b.Status__c == 'Awarded')
        {
           bids.add(b);

           jobIds.add(b.Job__c);
        }
    }

    if ( bids.size() > 0 )
    {

        bid = [SELECT Id,Status__c FROM Bid__c WHERE Job__c IN :jobIds AND Id NOT IN :bids];

        if ( bid != null & bid.size() > 0 )
        
            for ( Bid__c b: bid )
                if (b.Status__c == 'Pending'){
                b.Status__c = 'Lost';
 
            update bid;
        }
    }
}

 

All Answers

SFAdmin5SFAdmin5

is the field connecting the job__c to bid__c object a master detail or lookup type field?

GrrrrrrrrrrrrrGrrrrrrrrrrrrr

It is a Master-Detail

SFAdmin5SFAdmin5

i think this does the job.  let me know if it works and then i can help you with the test class

 

trigger updateOtherChildrenFromChildUpdate on Bid__c (after update, after insert)
{
    Bid__c[] bids = new Bid__c[0];
    Bid__c[] bid;
    Id[] jobIds = new Id[0];

    for ( Bid__c b : trigger.new )
    {
        if ( b.Status__c == 'Awarded')
        {
           bids.add(b);

           jobIds.add(b.Job__c);
        }
    }

    if ( bids.size() > 0 )
    {

        bid = [SELECT Id,Status__c FROM Bid__c WHERE Job__c IN :jobIds AND Id NOT IN :bids];

        if ( bid != null & bid.size() > 0 )
        {
            for ( Bid__c b: bid )
                b.Status__c = 'Lost';
 
            update bid;
        }
    }
}

 

 

 

SFAdmin5SFAdmin5

just tweaked this so the trigger will only update other children that are in a pending status, since that is in your reqs.  easy to see where to change that (just take out the if statement near the end about the status of the bid = pending)

 

trigger updateOtherChildrenFromChildUpdate on Bid__c (after update, after insert)
{
    Bid__c[] bids = new Bid__c[0];
    Bid__c[] bid;
    Id[] jobIds = new Id[0];

    for ( Bid__c b : trigger.new )
    {
        if ( b.Status__c == 'Awarded')
        {
           bids.add(b);

           jobIds.add(b.Job__c);
        }
    }

    if ( bids.size() > 0 )
    {

        bid = [SELECT Id,Status__c FROM Bid__c WHERE Job__c IN :jobIds AND Id NOT IN :bids];

        if ( bid != null & bid.size() > 0 )
        
            for ( Bid__c b: bid )
                if (b.Status__c == 'Pending'){
                b.Status__c = 'Lost';
 
            update bid;
        }
    }
}

 

This was selected as the best answer
GrrrrrrrrrrrrrGrrrrrrrrrrrrr

Ross -

   That is fantastic!!!  It works wonderful!  I am giggling like a crazy lady!

 

  I didnt think about it before, but I have multiple picklist values in my Status__c field.  I made the if statement to say:

 

 if (b.Status__c != 'Awarded') {

    b.Status__c = 'Lost' ;

  update bid;

}

 

 and it worked great. 

Is there any way you can help me with the Test too?  (You have already saved a crazy amount of hours for us!!)

ForceLoverForceLover

Hi  SFAdmin

 

I have two custom objects Employee (Master) , submission (Detail).My rollup in Employee gives no of submissions made by each .I need to filter the rollup as submissions based on current month and for each employee... for this  i'm planning to write a trigger . here i created a count field on employee everytime if a record enter into submission object the count should be incremented.but i need employee wise submissions count on monthly basis could you please help me out.Every time the count should be reset to zero on next month.clould you please help out me.This was what i written based on your previous post

 

 

trigger Rollup1 on Submission__c (after update, after insert)
{
Submission__c [] Sub = new Submission__c [0];
Employee__c [] Emp;
Set<String> submittedIds = new Set<String>();

Id[] jobIds = new Id[0];

for ( Submission__c S : trigger.new )
{
if ( S.DateMonth__c == S.Current_Month__c)
{
Sub.add(S);

}

submittedIds.add(S.Submittedby_del__c);
}

if ( Sub.size() > 0 )
{

Emp = [SELECT Id, Count__c FROM Employee__c WHERE id IN : submittedIds];

for ( Employee__c E:Emp)
{
E.count__c=Sub.size();
}
}
}

SFAdmin5SFAdmin5

Terrific.  Sure, I'll work on the test class and post it when I'm done.  

SFAdmin5SFAdmin5

something like this would get test coverage.  if i have time later i'll try to write a real test but this would get coverage i think at least

 

@isTest

private class testBidTrigger{
static testMethod void verifyBidStatuses(){

Job__c j = new Job__c();
j.Name = 'Job1';
insert j;

Bid__c b = new Bid__c();
b.Name = 'Bid1';
b.Job__c = j.Id;
b.Status__c = 'Pending';
insert b;

Bid__c b2 = new Bid__c();
b2.Name = 'Bid2';
b2.Job__c = j.Id;
b2.Status__c = 'Pending';
insert b2;

Test.startTest();
b.Status__c = 'Awarded';
update b;
Test.stopTest();


}
}

SFAdmin5SFAdmin5

if you have a master detail relationship on those 2 objects you can use rollup type fields to aggregate the child field values on the parent, and then use custom formula fields to do that monthly tallying.  should have no reason for an aggregation trigger if you can take advantage of m-d rollup fields

ForceLoverForceLover

Hi Ross Thanks for replying

 

                      i created rollup summary field when i'm trying to fillter that based on date it is not supporting. Some searching led me to this idea

 

http://success.salesforce.com/ideaView?id=08730000000BrUA

 

which asks for filtering roll-up summaries based on formulas.then i started writing trigger.my exact requirement is my rollup should  give monthly wise count

SFAdmin5SFAdmin5

hey grrrr...would you mind marking my solution as a resolution to this thread?  appreciate it and thanks

GrrrrrrrrrrrrrGrrrrrrrrrrrrr

Thank you Ross.

  This worked fantastic.  I appreciate your help.  I have marked your Trigger as the resolution, but for anyone looking, the Trigger and the Test Method are both solutions.  :)

 

  The only changes I had to make to the Test was to include required fields and modify the field names to match my custom object!