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
Mony Gueorguiev 11Mony Gueorguiev 11 

Can a rule check a box for newest record and uncheck for the old ones?

Hi,

I have a quick question regarding how to carry over information into a newly created record. How to have a checkbox check carry over to newly created records and old ones have the checkbox unchecked?

Essentially, we have a Proposal object that we create proposals off of. We may have more than 1 proposal for any one Opportunity. But, only 1 of those Proposals will be the Primary one. The Primary field in the Proposal layout is a checkbox. How can I have that box checked automatically each time a new proposal is made and how can the previous (older proposal(s)) have the Primary box unchecked?

I just can't seem to wrap my head around this. Any help is greatly appreciated!

Thanks


 
Bryan JamesBryan James
Im not sure off hand if there is a good way to do this outside of either a trigger or using the process builder.
Here is some code to do it in a trigger. Basically you are getting the opportunity Id of your new proposal and then querying to find all the old proposals that have the same opportunity Id and unchecking their box.

On the actual proposal field I set the default of the box to be checked when a new record is created.


default to checked

 
trigger PrimaryProposal on Proposal__c (before insert) {
    Set<Id> oppIds = new Set<Id>();//create an empty set to hold unique Opportunity Ids
    //Loop through our incoming Proposals
    for(Proposal__c newProp : trigger.new)
    {
        oppIds.add(newProp.Opportunity__c);//Add their Ids to our oppIds
    }
    //Retrieve any old proposals who have an Opportunity Id that 
    matches one in that set and whose Primary field is checked
    List<Proposal__c> oldProposals = new List<Proposal__c>([Select Id,Primary__c
                                                            From Proposal__c
                                                            Where Primary__c = true
                                                            AND Opportunity__c in :oppIds]);
    
    //Loop over our list of old proposals
    for(Proposal__c oldProp : oldProposals) 
    {
        oldProp.Primary__c = false;//change each of the old proposals primary fields to unchecked
    }
    update oldProposals;//update our list of old proposals with their checkboxes now empty.
}

Hope this helps
Mony Gueorguiev 11Mony Gueorguiev 11
Hey Bryan,

I think this defintely on the right track! Just one question,

So I put in the code and attempted to change the object fields to the correct labels we have in our system. Basically I made the following changes:

trigger PrimaryProposal on PPS_Proposal__c (before insert) {

Set<Id> oppIds = new Set<Id>();
    //create an empty set to hold unique Opportunity Ids

    //Loop through our incoming Proposals
    
for(PPS_Proposal__c newProp : trigger.new)

    {

oppIds.add(newProp.Opportunity__c);
    //Add their Ids to our oppIds

    }

    //Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
    
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c

                                                            From PPS_Proposal__c

                                                            Where Primary_Old__c = true

                                                            AND Opportunity__c in :oppIds]);

    //Loop over our list of old proposals

for(PPS_Proposal__c oldProp : oldProposals) 

    {

oldProp.Primary_Old__c = false;
    //change each of the old proposals primary fields to unchecked

    }

update oldProposals;
    //update our list of old proposals with their checkboxes now empty.

}




After trying this out I get the following error: Error: Compile Error: Invalid field Opportunity__c for SObject PPS_Proposal__c at line 12 column 12

Line 12 is oppIds.add(newProp.Opportunity__c);

Looking forward to recieving help!

Thanks!

 
Bryan JamesBryan James
The only thing I can think of is if your org has a namespace you may need to prepend it to your field such as namespace__Opportunity__c or you named the Opportunity lookup/Master Detail field something else. I would have to look at the object in order to figure it out otherwise.
Let me know if there is anything I can do to help.
Mony Gueorguiev 11Mony Gueorguiev 11
Hey Bryan,

So I changed the code to this:



trigger PrimaryProposal on PPS_Proposal__c (before insert) {

Set<Id> oppIds = new Set<Id>();
    //create an empty set to hold unique Opportunity Ids

    //Loop through our incoming Proposals
    
for(PPS_Proposal__c newProp : trigger.new)

    {

oppIds.add(newProp.Opportunity_Name__c);
    //Add their Ids to our oppIds

    }

    //Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
    
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c

                                                            From PPS_Proposal__c

                                                            Where Primary_Old__c = true

                                                            AND Opportunity_Name__c in :oppIds]);

    //Loop over our list of old proposals

for(PPS_Proposal__c oldProp : oldProposals) 

    {

oldProp.Primary_Old__c = false;
    //change each of the old proposals primary fields to unchecked

    }
}



The field in question was named differently, I renamed it and the error disappeared. As it stands, I've triple checked all API field names and they are correct. I activated the trigger.

But, it just automatically checks the Primary Old box every time a new Proposal is created off of our PPS Proposal object. If I create a few new ones, it does NOT uncheck the box from the previous ones. It just keeps the Primary Old box checked for everyone of them. Also, if I go into a Proposal and click the Clone button the Primary Old checkbox does not automatically check, so it seems like this does not work when we try creating a Proposal through cloning?

Any help is appreciated!

Thanks!
Mony Gueorguiev 11Mony Gueorguiev 11
So essentially just to clarify these Proposals are created under an Opportunity. So an opportunity with the same name can have 5-6 proposals and we need the primary box to be checked so we can report correctly, and know which one is the most current/used one. 
Mony Gueorguiev 11Mony Gueorguiev 11
Any additional insight into this??