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
Sangeetha TSangeetha T 

Trigger to check a checkbox

Hi I have two custom fields subscription_start_date and subscription_end_date on Opportunity object

Have a two custom checkbox customer1 and customer2 on Contact Object (These two checkboxes are checked from another workflow)

I have a requirement to that these checkboxes should be unchecked if today date does not lies between subscription_start_date and subscription_end_date

No i have to write a trigger with the condition

if (today date should be greater than or equal to subscription_start_date) and (lesser than or equal to subscription_end_date) and (customer1 checkbox should be checked)

{customer1=true }//should be checked

else 

{customer1=false}//should not be checked

 

 

The following is my trigger but does not gives expected result 

what is wrong 

 

trigger checkthecheckbox on Contact (before insert) {

Set<id> oppids = new set<id>();

for (Contact opp: Trigger.new)
{
 oppids.add(opp.id);
}

Map<id, opportunity> oppMap = new Map<id, opportunity>();
for(opportunity o : [select Subscription_Start_Date__c,Subscription_End_Date__c from opportunity where id in: oppids])
{
 
oppMap.put(o.id, o); 
}

for (Contact opp: Trigger.new)
{
if(oppMap.get(opp.Id).Subscription_Start_Date__c>=date.Today())
{
opp.Customer_of_PubKCyber__c=False;
}
 
}
}

 

ArmouryArmoury
The trigger is on Contact Object. So the Ids returned are Contact Id's and the SOQL query on Opportunity should be on Contact instead of Id field. 
Between what is the relationship between Contact and Opportunity is used? Is there a custom relationship or the standard OpportunityContactRole is used?

 
Sangeetha TSangeetha T
Hi Armoury 

Contact and Opportunity have standard realtionship

Checking this condition when a lead is converted to Opp,contact and acc