You need to sign in to do that
Don't have an account?
Mony 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
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
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.
Hope this helps
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!
Let me know if there is anything I can do to help.
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!