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
Shivom Verma 34Shivom Verma 34 

A new Student Enrolment cannot be created before two days of Activity Start Date. For example, Start Date is 20th March, the new enrolments are only allowed to be created before 18th March.

ANUTEJANUTEJ (Salesforce Developers) 
Hi Shivom,

I think you can create a trigger wherein you can fetch the value of the start date of the course and restrict the user from enrollment if the created date is less than 2 days, if this doesn't work can you elaborate on the use case to check further and respond back.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Shivom Verma 34Shivom Verma 34
Hii Anutej ,
Can u provide me a sample code
Thanks in Advance
Shivom Verma 34Shivom Verma 34
Activity (Object Name) 
Max number of Enrolments allowed – Number type 
Number of Enrolments – Number Type 
Number of Students Participated – Number Type 
Start Date – Date Type 
End Date – Date Type 

Student (Object Name) 
Name – Text Type 

Student Enrolment (Object Name) 
Activity – Master Detail to Activity custom object 
Student - Master Detail to Student custom object 
Participated in Activity – Checkbox 


A new Student Enrolment cannot be created before two days of Activity Start Date. For example, Start Date is 20th March, the new enrolments are only allowed to be created before 18th March
ANUTEJANUTEJ (Salesforce Developers) 
You can try something like below and modify it accordingly.
 
trigger StudentTri on Student__c (before insert)
{
	
	if(trigger.isbefore && trigger.isinsert)
	{

	set<id> courseId= new set<id>();
	for(student__c s: trigger.new)
	{
		courseId.add(s.courseid__c);	
	}
	map<id,Cousrse__c> courselist = [select id,StartDate from course__c where id in :courseId];
	for(Student__c Stu: trigger.new)
	{

	Course__c tempc= courselist.get(Stu.CourseId);
	Date SDate= tempc.StartDate;
	if(SDate.daysBetween(Stu.enrollmentDate) >=2)
	{Stu.adderror("You cannot enroll before 2 days of start date of the Course");}

	}

	}

}

Please understand this is a sample snippet and you need to modify it to fit your scenario.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Shivom Verma 34Shivom Verma 34
trigger PreventEnrollment on Student_Enrolment__c (before insert) {

 

   
    set<id> actid = new set<id>();
    for( Student_Enrolment__c std :trigger.new)
    {
        actid.add(std.id);
    }
    
Map<Id,Activity__c>  mapList = new Map<Id,Activity__C>();

    mapList.putAll( [SELECT Name FROM Activity__c WHERE id in :actid]);
    
    
    for(Student_Enrolment__c act : Trigger.new)
    {
       Activity__c temp = mapList.get(act.Id);
        Date SDate= temp.Start_Date__c;
       if(SDate.daysBetween(act.Date__c)>=2)
        {
            act.addError('You can not Enrolled Today');
        }
    }
}
 
Shivom Verma 34Shivom Verma 34
it showing error
PreventEnrollment: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.PreventEnrollment: line 20, column 1
ANUTEJANUTEJ (Salesforce Developers) 
can you specify which line is it and have you made the necessary corrections?
Shivom Verma 34Shivom Verma 34
 for(Student_Enrolment__c act : Trigger.new)
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shivom,

Can you check and make sure the value is being sent i.e., the records in trigger.new have a date value in Date__c field.

Let me know if it works.

Thanks.
Shivom Verma 34Shivom Verma 34
set<id> actid = new set<id>();
    for( Student_Enrolment__c std :trigger.new)
    {
        actid.add(std.id);
    }

Hey our event is before insert
so what is the use of trigger.new here
what u r storing in actid
ANUTEJANUTEJ (Salesforce Developers) 
So, trigger.new is a variable that we use to access the incoming records.

I assumed subject/ course is a object and student is a object that has a lookup to the course object. I was getting the course start date by storing it in a set and getting it using soql.

This is my assumption and not according to your org details that is the reason I mentioned to modify accordingly.
Jennifer SklarJennifer Sklar
How to be ready for the incoming records?
Jennifer SklarJennifer Sklar
The Start Date was not 20th March.
Charles Jackson 2Charles Jackson 2
It was difficult for me to understand such code. In general, learning to code is not that easy these days - I've been studying a lot lately and can't seem to manage everything. Thanks to https://ca.edubirdie.com/descriptive-essay-writing-service of course, I have the opportunity to quickly and free of charge order the best descriptive essay for my projects, but I still spend a lot of time preparing to defend them. It's good that there are sites like salesforce, where people help each other solve problems with the code.