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
dizzyemdizzyem 

Trigger only working once

Wondering if anybody can help me with this one...

 

I have a custom button on the detail page to approve a record (which just sets an Outcome field to "Approved"). Then I have a trigger that will update all of the other options to "Rejected". The first time I approve an option record it works perfectly. That record's outcome is set to "Approved" and all of the other option records' outcomes are set to "Rejected". However, if I then go in to one of those rejected options and click the Approve button, the outcome is set to "Approved" as it should be but none of the other options' outcomes are changed to be set to "Rejected". Let me know if you have any thoughts...

 

Here is my button:

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 

alert ("Your Housing Option has been Approved. Thanks."); 

var newRecords = []; 

var c = new sforce.SObject("Housing_Options__c"); 
c.id ="{!Housing_Options__c.Id}"; 
c.Outcome__c = "Approved"; 
newRecords.push(c); 

result = sforce.connection.update(newRecords); 

window.location.reload();

 

 

Here is my trigger:

 

trigger setOutcomes on Housing_Options__c (after update) 
{
  List<ID> requestIDs = new List<ID>();
  Boolean needsUpdate = FALSE; 
    for (Housing_Options__c option: trigger.new)
    {
    requestIDs.add(option.Housing_Request__c);
    }
    List<Housing_Options__c> otheroptions = new List<Housing_Options__c>([select id, Outcome__c, Housing_Request__c from Housing_Options__c where Housing_Request__c in: requestIDs]);
    for (Housing_Options__c option: trigger.new)
    {
    for (integer i=1; i < otheroptions.size(); i++)
     {
        if ( ((Trigger.isInsert) ||       (Trigger.isUpdate) && (Trigger.oldMap.get(option.id).Outcome__c != 'Approved')) &&       (option.Outcome__c == 'Approved') ) 
        {
         otheroptions[i].Outcome__c = 'Rejected';
         needsUpdate = TRUE;
        }
     }
    }
  if (needsUpdate){
    update otheroptions;
  }
 
}

 

Thanks!!

Best Answer chosen by Admin (Salesforce Developers) 
dizzyemdizzyem

Thanks, I actually had to make a few changes to make this work. The first is that I changed my query statement so that it would not include the record that is being updated in my otheroptions result set. Another is that I modified the increment statement which was originally i=1 to i=0. I also updated my IF statement slightly. It is working great now.

 

trigger setOutcomes on Housing_Options__c (after update)

{

  List<ID> requestIDs = new List<ID>();

  Boolean needsUpdate = FALSE;

    for (Housing_Options__c option: trigger.new)

    {

    requestIDs.add(option.Housing_Request__c);

    }

    List<Housing_Options__c> otheroptions = new List<Housing_Options__c>([select id, Housing_Request__c from Housing_Options__c where Id NOT in :trigger.New AND Housing_Request__c in: requestIDs]);

    for (Housing_Options__c option: trigger.new)

    {

    for (integer i=0; i < otheroptions.size(); i++)

     {

        if ( ((Trigger.isUpdate) && (Trigger.oldMap.get(option.id).Outcome__c != 'Approved') && (option.Outcome__c == 'Approved') ) )

        {

         otheroptions[i].Outcome__c = 'Rejected';

         needsUpdate = TRUE;

        }

     }

    }

  if (needsUpdate){

    update otheroptions;

  }

 

}

All Answers

UVUV

 

To update the field with "Rejected" this condition should be true..

 

 if ( ((Trigger.isInsert) ||       (Trigger.isUpdate) && (Trigger.oldMap.get(option.id).Outcome__c != 'Approved')) &&       (option.Outcome__c == 'Approved') ) 

 

You trigger looks fine.So,I would suggest you to write a debug statement in the trigger to see whats happening in the background..

dizzyemdizzyem

Thanks, I actually had to make a few changes to make this work. The first is that I changed my query statement so that it would not include the record that is being updated in my otheroptions result set. Another is that I modified the increment statement which was originally i=1 to i=0. I also updated my IF statement slightly. It is working great now.

 

trigger setOutcomes on Housing_Options__c (after update)

{

  List<ID> requestIDs = new List<ID>();

  Boolean needsUpdate = FALSE;

    for (Housing_Options__c option: trigger.new)

    {

    requestIDs.add(option.Housing_Request__c);

    }

    List<Housing_Options__c> otheroptions = new List<Housing_Options__c>([select id, Housing_Request__c from Housing_Options__c where Id NOT in :trigger.New AND Housing_Request__c in: requestIDs]);

    for (Housing_Options__c option: trigger.new)

    {

    for (integer i=0; i < otheroptions.size(); i++)

     {

        if ( ((Trigger.isUpdate) && (Trigger.oldMap.get(option.id).Outcome__c != 'Approved') && (option.Outcome__c == 'Approved') ) )

        {

         otheroptions[i].Outcome__c = 'Rejected';

         needsUpdate = TRUE;

        }

     }

    }

  if (needsUpdate){

    update otheroptions;

  }

 

}

This was selected as the best answer