You need to sign in to do that
Don't have an account?
Sanjay Vinayak T
How to update field based on comparison of fields of 2 non related objects?
Hi All,
There are 2 objects -
Object 1- Student Details with Field 1- Pan Number, Field 2- Background status, Field 3-Phone number,
Object 2- Backlisted candidates with Field 1 - Pan number and Field 2- Phone number.
My requirement is:
When a record is created/inserted in Object 1- Student Details.
If Field 1- Pan Number of Object 1- Student Details is equal to Field 1 - Pan number of Object 2- Backlisted candidates.
Then update Field 2- Background status of Object 1- Student Details with a message as Student is blacklisted, and also update Field 2- Phone number of Object 2- Backlisted candidates with Field 3-Phone number of Object 1- Student Details.
I have tried with Flows but am unable to fetch the fields to compare.
So tried APEX Class and Triggers, but unable to update the fields.
Below is the Apex code and Trigger I worked on:
APEX Class:
Trigger:
Kindly help me to improvise the code and work on it.
Or What should be the new approach for this problem using Apex and Trigger?
There are 2 objects -
Object 1- Student Details with Field 1- Pan Number, Field 2- Background status, Field 3-Phone number,
Object 2- Backlisted candidates with Field 1 - Pan number and Field 2- Phone number.
My requirement is:
When a record is created/inserted in Object 1- Student Details.
If Field 1- Pan Number of Object 1- Student Details is equal to Field 1 - Pan number of Object 2- Backlisted candidates.
Then update Field 2- Background status of Object 1- Student Details with a message as Student is blacklisted, and also update Field 2- Phone number of Object 2- Backlisted candidates with Field 3-Phone number of Object 1- Student Details.
I have tried with Flows but am unable to fetch the fields to compare.
So tried APEX Class and Triggers, but unable to update the fields.
Below is the Apex code and Trigger I worked on:
APEX Class:
Trigger:
Kindly help me to improvise the code and work on it.
Or What should be the new approach for this problem using Apex and Trigger?
Try the below approcah and please let me know if you face the issue again.
Step1- create two objects i.e. Student_Details__c & Blacklisted_candidate__c and custom fields as per your requirement
Step2- create trigger with name 'StudentRecordTrigger' on Student_Details__c object
Step3- create apex class 'StudentRecordTriggerHandler'
Trigger:
trigger StudentRecordTrigger on Student_Details__c (after insert) {
if(Trigger.isinsert || Trigger.isafter ){
StudentRecordTriggerHandler.checkStudentStatus(Trigger.new);
}
}
-----------------------------------------------------------------------------------------------
Trigger HandlerClass:
public class StudentRecordTriggerHandler {
public static void checkStudentStatus(List<Student_Details__c> newStudent){
try{
Set<Id> StuId = new Set<id>();
if(newStudent!=null){
for(Student_Details__c stu : newStudent){
StuId.add(stu.Id);
}
}
List<Student_Details__c> studentToUpdate = new List<Student_Details__c>();
studentToUpdate = [Select id,Background_status__c,Pan_number__c,Phone__c from Student_Details__c where id in : StuId];
if(StuId!=null){
List<Blacklisted_candidate__c> blackListedStu = [Select id from Blacklisted_candidate__c where Pan_number__c = :studentToUpdate[0].Pan_number__c limit 1];
if(blackListedStu!=null){
blackListedStu[0].Phone_number__c = studentToUpdate[0].Phone__c;
update blackListedStu;
studentToUpdate[0].Background_status__c = 'Student is blacklisted';
update studentToUpdate;
}
}
}
catch(Exception e){
system.debug('failed to update');
}
}
}
Thanks!!
All Answers
1.StudentTriggerHandler - Variable does not exist: Phone_Number__c.
2.StudentTriggerHandler - Variable does not exist: Background_Check_Status__c.
3.StudentTrigger - Method does not exist or incorrect signature: void StudentBlackListCheck(List<Student_Master__c>) from the type StudentTriggerHandler.
Try the below approcah and please let me know if you face the issue again.
Step1- create two objects i.e. Student_Details__c & Blacklisted_candidate__c and custom fields as per your requirement
Step2- create trigger with name 'StudentRecordTrigger' on Student_Details__c object
Step3- create apex class 'StudentRecordTriggerHandler'
Trigger:
trigger StudentRecordTrigger on Student_Details__c (after insert) {
if(Trigger.isinsert || Trigger.isafter ){
StudentRecordTriggerHandler.checkStudentStatus(Trigger.new);
}
}
-----------------------------------------------------------------------------------------------
Trigger HandlerClass:
public class StudentRecordTriggerHandler {
public static void checkStudentStatus(List<Student_Details__c> newStudent){
try{
Set<Id> StuId = new Set<id>();
if(newStudent!=null){
for(Student_Details__c stu : newStudent){
StuId.add(stu.Id);
}
}
List<Student_Details__c> studentToUpdate = new List<Student_Details__c>();
studentToUpdate = [Select id,Background_status__c,Pan_number__c,Phone__c from Student_Details__c where id in : StuId];
if(StuId!=null){
List<Blacklisted_candidate__c> blackListedStu = [Select id from Blacklisted_candidate__c where Pan_number__c = :studentToUpdate[0].Pan_number__c limit 1];
if(blackListedStu!=null){
blackListedStu[0].Phone_number__c = studentToUpdate[0].Phone__c;
update blackListedStu;
studentToUpdate[0].Background_status__c = 'Student is blacklisted';
update studentToUpdate;
}
}
}
catch(Exception e){
system.debug('failed to update');
}
}
}
Thanks!!
Thanks for helping me in solving the above problem.
How can I show the above error message on Visualforce Page?
Requirement: need to show the above error message on the Visualforce page after entering the details in the salesforce object page.
I tried adding Apexpages.addmessage to the above class,
if (studentToUpdate[0].Background_Check_Status__c !=null){
ApexPages.addmessage(new apexPages.Message(ApexPages.severity.WARNING,'<h1 style="color : red;"> Student is blacklisted.'));
}
and created the Visualforce page,
<apex:page controller="StudentRecordTriggerHandler">
<apex:form >
<apex:pageBlock >
<apex:pageMessages id="Message" escape="false" ></apex:pageMessages>
</apex:pageBlock>
</apex:form>
</apex:page>
but got an error stating
StudentRecordTrigger: execution of AfterInsert caused by: System.FinalException: ApexPages.addMessage can only be called from a Visualforce page Class.StudentRecordTriggerHandler.checkStudentStatus: line 30, column 1 Trigger.StudentRecordTrigger: line 4, column 1
Kindly help in fixing the error.
Thank You!
Regards,