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
Abhishek SolankiAbhishek Solanki 

Find topper from student records


Please design solution as per following business scenario :
Consider Account standard object as an Exam and Contact  standard object as a Student.
You need to create following fields for Exam.
Passing Marks : Number
Topper : Lookup(Contact)
You need to create following field for Student.
Marks : Number

Now, you need to develop following functionalities.
1 . On exam record, you need to show Topper for related Student records. It means, you need to identify the student who has highest marks.
Best Answer chosen by Abhishek Solanki
BALAJI CHBALAJI CH
Hi Abhishek,
Please find below Trigger and Screenshots as per your requirement.

Screenshot 1: Exam1 is record in Account which has Contacts (Student1, Student 2, Student3 ....) and Topper is the Custom field of Accouunt which shows the Contact with highest Marks.
User-added image

User-added image

Trigger:
 
trigger ExamMarks on Contact (after insert, after update) {
    
    Set<Id>Accids = new Set<Id>();
    for(Contact c:Trigger.New)
    {
        if(c.AccountId <> null)
            Accids.add(c.AccountId);
    }
    
    Map<Id, Id> Result = new Map<Id, Id>();
    
    for(Id s : Accids)
    {
        Decimal i =0;
        Id cc;
        for(Contact c : [Select id, name, Marks__c from Contact where AccountId =: s])
        {
            if(i<c.Marks__c)
            {
                i=c.Marks__c;
                cc = c.id;
            }
        }
        Result.put(s, cc);
    }
    
    for(Account a : [select id, name, contact__c from Account where Id IN: Result.keySet()])
    {
        a.contact__c = Result.get(a.id);
        update a;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI

All Answers

VineetKumarVineetKumar
Write a trigger on contact to do the below :
  • Fetch all the available contacts on the associated account.
  • Loop through all the contacts and apply the algorithm to get the highest number - Student
  • Make an update on the Account with this student.
BALAJI CHBALAJI CH
Hi Abhishek,
Please find below Trigger and Screenshots as per your requirement.

Screenshot 1: Exam1 is record in Account which has Contacts (Student1, Student 2, Student3 ....) and Topper is the Custom field of Accouunt which shows the Contact with highest Marks.
User-added image

User-added image

Trigger:
 
trigger ExamMarks on Contact (after insert, after update) {
    
    Set<Id>Accids = new Set<Id>();
    for(Contact c:Trigger.New)
    {
        if(c.AccountId <> null)
            Accids.add(c.AccountId);
    }
    
    Map<Id, Id> Result = new Map<Id, Id>();
    
    for(Id s : Accids)
    {
        Decimal i =0;
        Id cc;
        for(Contact c : [Select id, name, Marks__c from Contact where AccountId =: s])
        {
            if(i<c.Marks__c)
            {
                i=c.Marks__c;
                cc = c.id;
            }
        }
        Result.put(s, cc);
    }
    
    for(Account a : [select id, name, contact__c from Account where Id IN: Result.keySet()])
    {
        a.contact__c = Result.get(a.id);
        update a;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
Abhishek SolankiAbhishek Solanki
Thanks Balaji