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
Avinash@salesforceAvinash@salesforce 

Trigger to make only one check box True

Hi friends,

I have 1 custom object (Dimensions__c) which has a lookup relationship with Account standard object (Dimensions__c is Child).

There is a chechbox on Dimension__c (Repayment_Enabled__c).

Now wehenever i create a dimension this check box is made true automatically.

What my need is that, there should be only one check box true on for a record related to account.
Even though there may be 5 dimension records which are related to 1 account but checkbox should be true for only 1 dimension record. 

I have tried this one, But struck.
As i am new to development Please help me.


trigger Primarycheck on Account (before insert,before update,after insert) {
List<Account> acct = new List<Account>();
    Set<Id> vwoid = new Set<Id>();
    List<Dimensions__c> vwo = new List<Dimensions__c>();   // my custom object
    for(Account a : trigger.new){
       if(Repayment_Enabled__c[0] == 'true'){   // this is the check box field which needs to be true for only one record related account
            Repayment_Enabled__c[1] = false;
        }
            }


How to solve this one.
Any help friends..!
        
        
    }
Best Answer chosen by Avinash@salesforce
mritzimritzi

I am assuming that you want to keep the most recently updated/inserted Dimensions__c record to have checkbox=true & all other records to have checkbox = false.
trigger paymentcheck on Dimensions__c(after update,after insert) {
    // trigger on Dimensions__c object, not on Account
    // works only AFTER update & insert, so that we can have record's Id
    List<Dimensions__c> vwo = new List<Dimensions__c>();   // my custom object
    // Sets holding account ids (unique) and dimensions ids (unique) respectively
	Set<Id> accountIds = new Set<Id>();
	Set<Id> dimensionsIds = new Set<Id>();
    for(Dimensions__c d : trigger.new){
       if(d.Repayment_Enabled__c == true){
            // replace "lookupfieldApiName" with actual lookup field Api name
            accountIds.add(d.lookupFieldApiName);
			dimensionsIds.add(d.id);
        }
    }
    // get those records from Dimensions__c object that have their parent Account Id's record...
    // stored in accountIds and their id not present in dimensionsIds
	vwo = [SELECT id,Repayment_Enabled__c FROM Dimensions__c 
	      WHERE Repayment_Enabled__c=true AND lookupFieldApiName IN:accountId AND Id NOT IN:dimensionsIds];
	for(Dimensions__c d:vwo)
		d.Repayment_Enabled__c=false;
    update vwo;
}


Mark this as Best Answer, if this solves your problem.

All Answers

mritzimritzi

I am assuming that you want to keep the most recently updated/inserted Dimensions__c record to have checkbox=true & all other records to have checkbox = false.
trigger paymentcheck on Dimensions__c(after update,after insert) {
    // trigger on Dimensions__c object, not on Account
    // works only AFTER update & insert, so that we can have record's Id
    List<Dimensions__c> vwo = new List<Dimensions__c>();   // my custom object
    // Sets holding account ids (unique) and dimensions ids (unique) respectively
	Set<Id> accountIds = new Set<Id>();
	Set<Id> dimensionsIds = new Set<Id>();
    for(Dimensions__c d : trigger.new){
       if(d.Repayment_Enabled__c == true){
            // replace "lookupfieldApiName" with actual lookup field Api name
            accountIds.add(d.lookupFieldApiName);
			dimensionsIds.add(d.id);
        }
    }
    // get those records from Dimensions__c object that have their parent Account Id's record...
    // stored in accountIds and their id not present in dimensionsIds
	vwo = [SELECT id,Repayment_Enabled__c FROM Dimensions__c 
	      WHERE Repayment_Enabled__c=true AND lookupFieldApiName IN:accountId AND Id NOT IN:dimensionsIds];
	for(Dimensions__c d:vwo)
		d.Repayment_Enabled__c=false;
    update vwo;
}


Mark this as Best Answer, if this solves your problem.
This was selected as the best answer
LorrMcLorrMc
Hi,

I have a similar request but mine is for a related list called Placements on the contact where I have populated all historical data marking the record with the earliest start date as true, however I need to look the this field so that if a new record is created then the field called First Scotland Placement is not available. 
I would be grateful for any advice. 

Lorr
Syed Subhan 9Syed Subhan 9
Hi mritzi,

Your code works perfect even i had same kind of scenario. Thanks.