You need to sign in to do that
Don't have an account?
Rogerio Lara 2028
uncheck box based on new related record created
Hi there, Please Help! ;-)
I have a checkbox field that is by default checked when a record is created. (Good)
Let's suppose I have a custom object that relates to the Opportunity object, then I create record1 under the Opportunity (related list).
When I then create record2, I need the check box on record1 to be automatically unchecked.
Please can someone help me with the trigger. I don't really even know where to start ;-(
Thank you,
Rog
I have a checkbox field that is by default checked when a record is created. (Good)
Let's suppose I have a custom object that relates to the Opportunity object, then I create record1 under the Opportunity (related list).
When I then create record2, I need the check box on record1 to be automatically unchecked.
Please can someone help me with the trigger. I don't really even know where to start ;-(
Thank you,
Rog
All Answers
Okay. The object is Opportunity Positioning. So I am getting the following error message:
Error: Compile Error: Invalid type: OpportunityPositioningHandler at line 3 column 52
trigger OpportunityPositioningTrigger on Opportunity_Positioning__c(before insert, after insert)
{
OpportunityPositioningHandler objHandler = new OpportunityPositioningHandler();
if(trigger.isBefore)
{
if(Trigger.isInsert)
{
objHandler.onBeforeInsert(Trigger.New);
}
}
if(trigger.isAfter)
{
if(Trigger.isInsert)
{
objHandler.onAfterInsert(Trigger.NewMap);
}
}
}
rog
Hope you're well.
Sorry to bother, but I was wondering if you could assist further with the above.
The trigger and class are working as expected in the sandbox, but I unfortunately I am having difficulties to push them into production.
Apparently I need a test class to run test first as there is 0% coverage.
Thank you,
rog
@isTest
public class OpportunityPositioningHandlerTest
{
// replace Opportunity__c, checkboxfld__c with actual API names
static testMethod void testForOnlyOneCheckboxSet()
{
Opportunity objOpp = new Opportunity(Name = 'Test Opp', StageName = 'Prospecting', CloseDate = Date.Today()+2);
insert objOpp;
// assuming name is not auto number here
Opportunity_Positioning__c objOppPos1 = new Opportunity_Positioning__c(Name = 'Test Pos1',Opportunity__c = objOpp.Id);
insert objOppPos1;
// verify if newly inserted Opportunity Positioning is having checkboxfld__c = true
system.assertEquals(true, [SELECT Most_current_position__c FROM Opportunity_Positioning__c WHERE Id =: objOppPos1.Id].Most_current_position__c);
// insert one more Opportunity Positioning
Opportunity_Positioning__c objOppPos2 = new Opportunity_Positioning__c(Name = 'Test Pos2',Opportunity__c = objOpp.Id);
insert objOppPos2;
// verify if recent Opportunity Positioning's checkboxfld__c = true
system.assertEquals(false, [SELECT Most_current_position__c FROM Opportunity_Positioning__c WHERE Id =: objOppPos1.Id].Most_current_position__c);
system.assertEquals(true, [SELECT Most_current_position__c FROM Opportunity_Positioning__c WHERE Id =: objOppPos2.Id].Most_current_position__c);
}
}
Hi Nayana,
Test passed now. I have removed the required field. Thank you ever so much.!
Rog
How are you doing?
I was wondering if you could save once again please ;-)
I have the following trigger and a validation rule that helps to enforce contact roles to be created for the opportunities depending on the sales stage and amount of the opportunity. This is working well.
------------------------------------------------------------------------------------------ This is what I have - FYI ---------------------------------------------------------------------------------
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
Boolean isPrimary;
Integer iCount;
Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map
for (Integer i = 0; i < Trigger.new.size(); i++)
{
oppty_con.put(Trigger.new[i].id,
Trigger.new[i]);
}
isPrimary = False;
for (List<OpportunityContactRole> oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
{
if (oppcntctrle .Size() >0)
{
isPrimary = True;
}
}
iCount = 0;
for (List<OpportunityContactRole> oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
{
if (oppcntctrle2 .Size()>0)
{
iCount= oppcntctrle2 .Size();
}
}
for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
{
Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
Oppty.Primary_Contact_Assigned__c =isPrimary;
}
}
Validation rule:
AND(OR(ISPICKVAL(StageName, 'Advanced Proposal'), ISPICKVAL (StageName, 'Final Negotiation'), ISPICKVAL (StageName, 'Contract Signed')), Number_of_Contacts_Roles_Assigned__c <2, Amount >= 1000000)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Now this is what I need:
Standard Object = Opportunity
Custom Object = Strategic Client Opportunity Plan
I created a read only checkbox field on the opportunity = Has_Strategic_Client_Opportunity_Plan__c and left it unchecked
I want the trigger to check the "Has_Strategic_Client_Opportunity_Plan__c" when a new Strategic Client Opportunity Plan is created.
Then I will create a validation rule on the opportunity to check whether this field is checked and the other required values too.
Does this make sense?
Your help is really appreciated. ;-)
Thank you,
Rog