I have created a 5 Days Salesforce based programming and configuration assignments for practice and improving your skills. This blog have the second day assignment. The assignment is in format of a typical salesforce requirement, comparable to a small size project. To design and develop this project it is recommended that you register a fresh salesforce developer org. Please go through the requirements which are well defined and should be easily understood if you have a basic understanding of salesforce sales and service cloud. You data model design or solution approach to the requirements might be different from the answers provided here. Every solution is correct if you are fulfilling all the requirements.
Requirement: We have a university “St Judes” which wants its salesforce account to get configured by us. Following are the requirement of this client.
1.1. A Student can register to multiple courses and A Class can have multiple Courses and each Course can have multiple Students.
Hint: It can easily be done by simple Datamodel configuration.
For this you have to create some custom objects like University, Class, Student, Course, etc.
Click here to learn more
Go to setup, click on the search bar and type objects or go to setup, then go to Create than select objects
than create the required objects. Here is one example of creating a custom object:
Than click on new button you’ll able to see the page shown below.
After creating all the required objects you do need to create a flow diagram or Datamodel for further implementation. Here is one with which i have created you can design it in your style and in your own way.
For more information on master-detail realtionship and junction object please follow the Link. Junction Object
For creating field in your dev org you have to follow these steps:
Go to setup>search objects than click on the the custom object in which you want to create the field.
Let us go to Course object which you must have created by following the datamodel. Now when we go to our object we will find custom field and relationship extention there now click on the new button.
Now select the type of field which you want to create what we are going to do is to create a lookup field in Course object.
Than you will get a page to select a related to object in which you have to pick your related object in it like i have picked professor as my related to object.
now click next button than write a name and create the field according to the given instructions.
1.2. A Student cannot register for more than 8 Courses.
Hint: Create a rollup summary field in Student custom object you have created from previous question and create a Validation Rule for it.
Click here to learn more
1.3. A Professor can take multiple courses.
Click here to learn more
If you have followed the previous steps correctly than you don’t have to worry at all because you have already done this step.
1.4. A professor cannot take more than 4 Course at a time.
Hint: For this you have to know how to create a trigger. You can learn them from here >> Trigger and for more help you can learn triggers from my another blog training of triggers. follow this link: blog on trigger.
Click here to learn more
trigger professorSelectCourse on Course__c (after insert,after update) {
if (trigger.isAfter) // trigger professor can’t have more than four courses
{
set<id> ids=new set<id>();
list<course__c> crList=[SELECT Name,Professor__c FROM Course__c WHERE Professor__c != NULL];
for(Course__c cr: crList)
{
ids.add(cr.professor__c);
}
List<Course__c> profCourseList = [SELECT Name, Id, Professor__c
FROM Course__c
WHERE Professor__c IN: ids
AND Professor__c != NULL];
Map<Id, Integer> ProfCountMap = new Map<Id, Integer>();
for (Course__c course : profCourseList) {
Integer numberOfProf = profCountMap.get(course.Professor__c);
if (NULL == numberOfProf) {
profCountMap.put(course.Professor__c, 1);
} else {
profCountMap.remove(course.Professor__c);
profCountMap.put(course.Professor__c, numberOfProf + 1);
}
}
for (Course__c course : Trigger.new) {
System.debug(‘>>>>>>>>>>>>’ + course + ‘ has ‘ + profCountMap.get(course.Professor__c) + ‘ Professors’);
if (profCountMap.get(course.Professor__c) >= 5) {
course.addError(‘A Professor cannot take more than 4 courses at a time.’);
}
}
} }
1.5. All the Classes will have atleast one Professor as its class teacher.
Click here to learn more
For this problem you just need to create a lookup field in class and don’t forget to make it mandatory.
1.6. A Class teacher can view all the students he/she is teaching.
Hint: For this question you must know how to write a Apex class and how to write aVisualforce page. For Visual force page you can take help from my blog on Visualforce blog. What we are going to do here is to create an inline vf page. Don’t worry we are going to do it together.
Click here to learn more
To create an inline vf page first you have to make a controller class which i am sure you must have understood by now by trying the above link. Now our controller is like this:
public with sharing class InlineVfPageProfessorController {
public string Ids{get; set;}
public boolean flag{get;set;}
public InlineVfPageProfessorController(ApexPages.StandardController controller) {
Ids = controller.getId();
}
public List<Student__c> getAllStudents() {
Class__c classId = [
SELECT Id, Name, Class_Teacher__c
FROM Class__c
WHERE Class_Teacher__c =: Ids];
List<Course__c> selectedCourses = [SELECT Id, Name
FROM Course__c
WHERE Class__c =: classId.Id
AND Professor__c =: Ids];
List<Id> CourseListIdList= new List<Id>();
for(Course__c course : selectedCourses) {
CourseListIdList.add(course.Id);
}
List<CourseStudent__c> JunctionObj = [
SELECT Id, Name, Student__c
FROM CourseStudent__c
WHERE Course__c IN: CourseListIdList];
System.debug(‘>>>>>>>>>>>>>>>>>’ + JunctionObj);
List<Id> studentsId = new List<Id>();
for(CourseStudent__c junction : JunctionObj) {
studentsId.add(junction.Student__c);
}
List<Student__c> Students = [SELECT Id, Name, Number_of_Courses__c
FROM Student__c
WHERE Id IN: studentsId];
if (Course__c.Professor__c != NULL){
flag = true;
}
else{
flag = false;
}
return Students;
}
}
This controller will help you finding the students of the professor. Now we have to create the Visualforce page for the controller.
<apex:page standardController=”Professor__c” extensions=”InlineVfPageProfessorController”>
<apex:sectionHeader title=”All student names”/>
<apex:outputpanel rendered=”{!flag}” > These are the students which are teached by teacher. </apex:outputpanel>
<apex:pageBlock >
<apex:pageBlockTable value=”{!AllStudents}” var=”student”>
<apex:column value=”{!student.Name}”/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
this will do the trick. But we are not done yet now we have show this page in our professors layout. It can be done like this:
Go to your professor object and press on edit link on the page layout section like this:
Now drag a section from here
and drop it any where when it allows you to drop it.
Now type you section name and number of colums you want in your section select it than click ok. Now you have your section to show your page in it.
Now select visualforce pages from the list which will show all the pages you have created realated to that object. Than similarly drag the page you have created and drop into the section you have created jst now.
Yipee now you can see the list of students in the detail page.
1.7. A Course will have list of all the students in it enrolled.
Click here to learn more
Again you can do this by data model. By changing the layout page of the Course object.
Now that you already have edited a layout page i don’t think i have to tell you how to search layout page for Course object so i will do the quick review again go to your object>edit page layout. Thats it you have done it. Now that you are in page layout portion go to its related lists and select related list properties.
Now the field you have created in student with the junction object.
Then add it in selected field list than press ok. Now the student list will be shown in the detail page.
1.8. A Course will have the following fields “Start Date” “End date” “Total Addendance required” & “Number of Students registered (Rollup Summary field)”.
Click here to learn more
I am sure you know how to make field now from question number-1.1 these are all just fields that are needed to be formed in Course object.
December 15, 2015
These are very useful assignments for people just starting with Salesforce. Thanks!
January 2, 2016
than you this assignment is very helpfull to us
February 3, 2016
Hi,
Could you please let me explain what are the best practice to push territory hierarchy and Account assignment rules from sandbox to production
Thanks
SS
February 24, 2017
Hi Experts,
“A Student can register to multiple courses and A Class can have multiple Courses and each Course can have multiple Students.”
According to above statment “STUDENT” and “COURSE” should have Junction object. Because one STUDENT can apply for more then one COURSE and one course Assigned to more then STUDENT.
STUDENT ———–[JUNCTION OBJECT]——– COURSE
Please suggest
Thanks
Mukesh