+ Start a Discussion
etechcareersetechcareers 

SELF_REFERENCE_FROM_TRIGGER??? Help

Hi thank you firstlly for helping me out...

So I tried doing a simple bulk trigger... I keep getting this error

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger SARQ caused an unexpected exception, contact your administrator: SARQ: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0AA0000000MaYSMA0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0AA0000000MaYS) is currently in trigger SARQ, therefore it cannot recursively update itself: []: Trigger.SARQ: line 22, column 1

 

Help please...

 

trigger SARQ on Subject_Area__c (before insert,before update) {
//Get Ids for Subject Area
Set<ID> said = new Set<ID>{};

for(Subject_Area__c hrr: trigger.new){

    said.add(hrr.Id);
}
System.Debug('said ' + said);
if(said!=null && said.size()>0){
//The ones to update
    List<Subject_Area__c> htoUpdte =  [select id from Subject_Area__c where ID in :said];
    
//The answer I need to update SA.SARQ in the SA object
    List<Subject_Area_RQ__c> SARQ = [SELECT On_Track_Subject_Area__c
                                      FROM Subject_Area_RQ__c where Subject_Area__c IN :said];
    
for (Subject_Area__c c: htoUpdte ){
System.debug('sarq ' + SARQ[0].On_Track_Subject_Area__c);
            c.SARQ__c = SARQ[0].On_Track_Subject_Area__c;

update htoUpdte ;

}
}
}

 I also tried this:

Trigger SARQ on Subject_Area__c (before insert,before update) {
List<Subject_Area__c> htoUpdte = new List<Subject_Area__c>();


Subject_Area__c sa = Trigger.new[0];
List<Subject_Area_RQ__c> SARQ = [SELECT On_Track_Subject_Area__c
FROM Subject_Area_RQ__c where Subject_Area__r.Name=:sa.Name];


for(Subject_Area__c hr: trigger.new)
{

If(SARQ.size()>0){
hr.SARQ__c = SARQ[0].On_Track_Subject_Area__c ;
htoUpdte.add(hr);
}
}

}

 

WilmerWilmer

Hi,

 

I think one solution to this case could be the use of a flag.

 

Please, see this other post

 

This will help you to avoid recursive execution of the trigger.

 

I hope this helps.

 

Regards,

 

Wilmer

David81David81

You are doing an update call in a before insert/update record. No need for that.

 

Also, it looks like Subject_Area__c is a parent to Subject_Area_RQ__c. Is that correct? If so, is there a need to run this on insert? Will there be any Subject_Area_RQ__c children for a newly created Subject_Area__c?

 

And will there always only be one Subject_Area_RQ__c per Subject Area, or can there be multiple children?

 

 

 

 

 

etechcareersetechcareers

Hi David Thank you for helping me out...

So, Subject Area RQ has Subject Area as a lookup in Subject Area RQ Object.

So, what I am trying to do is for every Subject Area there should always be a Subject Area RQ.

 

So for example:

Fine Arts is a Subject Area

Arts is the Subject Area RQ <== Always general term (Math, Science, Social Studies, Arts, electives, etc.)

So I am trying to update this field in the Subject Area that would just place it into a general category ... easier for reports.

So would you have an example of what I am trying to do?

 

Thanks

eTechCareers.com

David81David81

How are Subject_Area_RQ objects created? Manually after the creation of the Subject_Area?

 

Will the Subject_Area_RQ child of a Subject_Area ever change?

 

It might make more sense to update the parent Subject_Area upon creation of the Subject_Area_RQ.

 

 

ahab1372ahab1372

I agree with David, you might want to reconsider the concept.

 

In general, what I think you are doing wrong is:

 

1. You have your records in your trigger

2. You create a set of IDs of these records

3. Then you query these records. 

 List<Subject_Area__c> htoUpdte =  [select id from Subject_Area__c where ID in :said];

This is not necessary. They already are in the trigger, no need to query them.

4. You manipulate the records (well, that's what triggers are for)

5. you try to save the manipulated records in the database. That does not work and is not necessary. This is already inherent in the before trigger. Just skip step 3 and 5 and your manipulated records will be saved to the database automatically.

DML insert or update statements are only necessary if you create or manipulate records which are not in the trigger context (a common scenario is manipluation of parent records).

MMA_FORCEMMA_FORCE

So I did as you stated... but now I have an issue where it stays only on one record and updates every record with the same value??? How do I make it move to new values?

 

trigger SARQ on Subject_Area__c (before insert,before update) {
//Get Ids for Subject Area
Set<ID> said = new Set<ID>{};

for(Subject_Area__c hrr: trigger.new){

    said.add(hrr.Id);
}

if(said!=null && said.size()>0){
 
//The answer I need to update SA.SARQ in the SA object
    List<Subject_Area_RQ__c> SARQ = [SELECT On_Track_Subject_Area__c
                                      FROM Subject_Area_RQ__c where Subject_Area__c IN :said];
   
    for (Subject_Area__c c: trigger.new){
        for(Subject_Area_RQ__c cc: SARQ){ 
            c.SARQ__c = cc.On_Track_Subject_Area__c;
        }
    }
}
}

 

 

David81David81

Ok, lets get back to basics.

 

1. Subject_Area__c is a parent via Lookup from Subject_Area_RQ_c, correct?

2. Is it always a 1 to 1 relationship? Each Subject_Area__c only has one Subject_Area_RQ__c child?

3. If 1 is Yes...If Subject_Area_RQ__c is "more general" than Subject_Area__c, is there a particular reason that Subject_Area_RQ__c is not the parent record?

 

 

 

Ramesh SomalagariRamesh Somalagari
Hi All I have same problem.Can you please click this link http://salesforce.stackexchange.com/questions/43871/system-dmlexception-delete-failed-self-reference-from-trigger See the log I a have got same issue.