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
BehzadBehzad 

How to run a trigger to uncheck an active check box?

Hey all,

 

I'm trying to uncheck the active check box for a number of locations object records.

 

The setup of my objects is as follow:

 

Locations --> Hardware (Both are custom objects which have parent child relationship)

 

I have a related list of Hardware for every Location record.

And, location records have an Active check box status which identifies if that location is currently active or not.

 

The logic behind the Active check box:

It is assumed to be unchecked if there are no Hardware records in the respective Locations related list.

And, it is assumed to be checked if there is one or more Hardware records in the respective Locations related list.

 

I believe this is viable through the Triggers.

 

Any help or comment is appreciated.

 

Thank you,

 

Behzad

 

mattdarnoldmattdarnold

Yes, this is feasible with triggers. Adding a check to locations is straightforward. If you need logic to remove a check from locations - say if you remove the last hardware record from a locations record, you'll need some additional logic to handle that (it will have to first determine if the hardware record being deleted represents the last hardware record associated with that location). Here is some sample code to get you started on the first part. You'll need to create a before or after delete trigger to handle the second part.

 

trigger UpdateLocation on Hardware__c (after insert, after update) {

// Create a set of Location__c IDs to use as a query parameter
Set<String> locationIDs = new Set<String>();
for(Hardware__c h : Trigger.new) {
locationIDs.add(h.locationId__c); // Use whatever your custom look-up field is to Location__c from Hardware__c
}

// Create a list of locations to update
List<Location__c> locationsToUpdate = new List<Location__c>();

// Select and iterate through any locations associated with set of hardware records being inserted or updated
for(Location__c l : [select id, name, isactive__c from Location__c where id in : locationIDs]) {
l.isactive__c = true;
locationsToUpdate.add(l);
}

update locationsToUpdate;

}

 

 Hope this helps.

 

-- Matt

 

BritishBoyinDCBritishBoyinDC

If the objects are in a master-detail relationship, you could consider just using a roll up summary to count the amount of hardware at a location, and then have a formula field that says "Active" when count > 0

 

 

DScottRDScottR

I have a similar scenario and haved tried adapting the code below to my situtaion but am not familiar enough with triggers to make it work.

 

Here is my situation:

 

Lead = Parent Object

Hot List = Child Object (from a managed install)

 

My main goal is to reference the field "Hot__c", a true/false, from the child object "Hot List" [Hot__List__c.Hot] via an advanced formula.  However this will not work when trying to go down to a child from a parent in the advanced formula builder.

 

So, I figured out I need a trigger to update a custom field "RC_Hot__c" on the parent object "Lead" [Lead.RC_Hot__c].

 

1.  Where should the trigger fire on the Lead or the Child object Hot__List__c?  I want the field Lead.RC_Hot__c to update when the value for  Hot__List__c.Hot__c changes.

 

2.  If the trigger must live on the custom object "Hot__List__c" and that object is from a managed install will this even work?

 

Here is the code that I tried to adapt and it does not work.  try not to laugh I have never done this before ;-)

 

trigger UpdateRCHot on Lead (after insert, after update) {

    // Create a set of Lead IDs to use as a query parameter
    Set<String> LeadIDs = new Set<String>();
    for(Hot__List__c h : Trigger.new) {
        LeadIDs.add(h.Hot_Lead_id__c); // Use whatever your custom look-up field is to Lead from Hot__List__c
    }

    // Create a list of locations to update
    List<Lead> LeadsToUpdate = new List<h.Hot_Lead_id__c>();

    // Select and iterate through any Hot__List__c associated with set of Lead records being inserted or updated
    for(Lead l : [Select Hot_Lead_id__c, Hot__c FROM Hot__List__c WHERE Hot__c = true]) {
    l.RC_Hot__c = false;
    LeadsToUpdate.add(l);
    }

    update LeadsToUpdate;

}

 

 

crop1645crop1645

DScottr:

 

The easiest solution is to use a RSF (roll up summary field) on Lead.  You can count the number of Hot__List__c.Hot on your Detail record which are checked true and store in the Lead.

 

As detail records come and go or have their Hot fields switch on/off, the Lead is automatically updated.  Managed packages won't interfere at all.

Roger GriloRoger Grilo
Hi @mattdarnold , I'm trying to uncheck a checkbox in Lightning, do you know if the following problem (see link) is potentially solvable with a trigger to uncheck the checkbox?  https://success.salesforce.com/answers?id=9063A000000iXgQ   many thanks,