You need to sign in to do that
Don't have an account?

apex trigger
Hi I have an contact object and application object. As soon as an contact oject is created , then related application is created.
I hae two fields as math score and english score and field as admission status on application object .
Now, i have written a trigger such that whenever an math and english test scores are updated then automatically the admission status field on application object is updated to placement exam.IT works fine now, but if i change the admission status and then try to update the two field values on contact record it gives me an error. Please find below the code and error.i would appreciate any kind of help.thank you
trigger AdmissionStatus on Contact (after insert,after update) {
List<EnrollmentrxRx__Enrollment_Opportunity__c> application=[Select Id from EnrollmentrxRx__Enrollment_Opportunity__c where EnrollmentrxRx__Applicant__c in: Trigger.New Limit 1];
List<EnrollmentrxRx__Enrollment_Opportunity__c> application2 = new List<EnrollmentrxRx__Enrollment_Opportunity__c>();
for(Contact con : Trigger.New)
{
if(con.Math_Placement_Exam_Score__c!=null && con.English_Placement_Exam_Score__c!=null)
{
if(application.size()>0)
{
for(EnrollmentrxRx__Enrollment_Opportunity__c application1 : application )
{
application1.EnrollmentrxRx__Admissions_Status__c = 'Placement Exam';
application2.add(application1);
}
}
}
}
update application2;
}
Error:
Error:Apex trigger AdmissionStatus caused an unexpected exception, contact your administrator: AdmissionStatus: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0BS0000002zp9SMAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EnrollmentrxRx.EOBeforeUpdate: execution of BeforeUpdate caused by: System.Exception: EnrollmentrxRx:Too many SOQL queries: 21 (EnrollmentrxRx): []: Trigger.AdmissionStatus: line 20, column 1
Hi , I think I solved this problem with the following code.
trigger AdmissionStatus on Contact (after insert,after update) {
List<EnrollmentrxRx__Enrollment_Opportunity__c> application=[Select Id from EnrollmentrxRx__Enrollment_Opportunity__c where EnrollmentrxRx__Applicant__c in: Trigger.New Limit 1];
List<EnrollmentrxRx__Enrollment_Opportunity__c> application2 = new List<EnrollmentrxRx__Enrollment_Opportunity__c>();
for(Contact con : Trigger.New)
{
if(con.Math_Placement_Exam_Score__c<=100 && con.English_Placement_Exam_Score__c<=100)
{
if(application.size()>0)
{
for(EnrollmentrxRx__Enrollment_Opportunity__c application1 : application )
{
application1.EnrollmentrxRx__Admissions_Status__c = 'Placement Exam';
application2.add(application1);
}
}
}
}
update application2;
}
But If I change any other value on the contact record and update this then again it changes the admission status to placement exam.If suppose I change the status to submitted after placement exam and then again update the contact record , the admission status rolls back to placement exam . I do not want that to happen .
thanks ....I need help....
thank you
Hi....I have a requirement ...can u plz help me on this...
There are 2 fields on Oppty - Stage and Disposition
Reason. Disposition Reason is dependent on Stage.
(already built)
2. The following action should happen whenever Stage is
any of the values with the word "Closed", so closed won,
closed lost etc.
3. Say I choose Stage as Closed Won, and Disposition as
Cyhicle.
4. On the Oppty Prod Line item related list i.e
(Products), all products where Primary Quote = TRUE, the
Stage and Disposition Reason should be updated with the
values chosen on the Oppty. Please note ALL line items
should be updated and the condition is Primary Quote
field is checked. So, prodcut should also have Stage as
Closed Won, and Disposition as Cycle.
Hint - there should be a apex trigger written on the
Oppty Object and it should run on AFTER INSERT and AFTER
UPDATE functions.
REKHA
Hi dkn,
Problem is that you do not check in your trigger if values of Math_Placement_Exam_Score__c and/or English_Placement_Exam_Score__c have changed. Result: the trigger fires on every update provided those 2 fields are less or equal to 100. I wouldn't base my logic on field changes in such a case but if you are sure every time the fields values are changed, the EnrollmentrxRx__Admissions_Status__c should be changed as well then here is what you can do.
You can check if changes have been performed to a field with this ex:
Hope this helps
Kr,
Fred