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
Venkatesh SVenkatesh S 

how to update detail field in self relationship(challenging)

hello all, 
i am newbie to apex coding.

i am stuck in this scenerio.  I cannot able to update the field in a case object which is a detail of the self lookup.  Please help me to break the challenge.  The coding is as follows, the main error is in "update t2", its in last line...some looping problem
Thank yo so much in advance...
trigger complex on Case (before insert, after update) {
    set<id> casid = new set<id>();
    set<id> curid = new set<id>();
    set<String> cassta = new set<String>();
    list<Case> multi=new list<Case>();
            for(Case c:Trigger.new){
                if(c.status__c=='closed'){
                cassta.add(c.status__c);
                curid.add(c.id);
                if(c.Case__c != null)
                casid.add(c.Case__c);
                }
                }
                  if(casid.size()>0)
                {
                  list<Case> t1=[select id,Case__c,status__c from Case Where id IN :casid];
                  for(Case q2:t1)
                   {
                   q2.status__c='closed';
                   }
                    update t1;
                }
                else
                {
                list<Case> t2=[select status__c from Case Where Case__c IN :curid];
                            for(Case q1:t2)
                            {
                            q1.status__c='closed';
                            }
                update t2;
                }            
Best Answer chosen by Venkatesh S
kevin Carotherskevin Carothers
Ahh - you have a recursive trigger....    
You need to implement a static boolean in a  helper class that's created for the instance of the call to your trigger.

This article should help;
http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

All Answers

kevin Carotherskevin Carothers
Hi

You should select the Id from your SOQL statement;

list<Case> t2=[select Id, status__c from Case Where Case__c IN :curid];

Try that and see what happens....
ShikibuShikibu
I agree with Kevin Carothers, but just to clarify his answer: You cannot update a record unless the Id is present in the record. How would Salesforce know which record to update? "Id" is the unique identifier. If not present, you cannot update (or delete or upsert). 

Also, you should have posted the error message in your question. 
Venkatesh SVenkatesh S
Hello kevin, shikibu
i did update soql with id, no use
by the way, the error is as follows
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger complex caused an unexpected exception, contact your administrator: complex: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5009000000NFZvvAAH; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, complex: maximum trigger depth exceeded Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv] Case trigger event AfterUpdate for [5009000000NFZvW] Case trigger event AfterUpdate for [5009000000NFZvv]: []: Trigger.complex: line 30, column 1
Venkatesh SVenkatesh S
If i try to update the child value, then it goes to parent which causes the loop
Venkatesh SVenkatesh S
Following is the debugging code
Status	Update failed. First exception on row 0 with id 5009000000NFZvvAAH; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, complex: maximum trigger depth exceeded
Case trigger event AfterUpdate for [5009000000NFZvW]
Case trigger event AfterUpdate for [5009000	Application	Browser
Request Type	Application	Operation	/5009000000NFZvW/e
Duration (ms)	1,113	Log Size (bytes)	51,045
Log	

30.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:40:14.344 (344583066)|EXECUTION_STARTED
09:40:14.344 (344668014)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000OGf0|complex on Case trigger event AfterUpdate for [5009000000NFZvW]
09:40:14.345 (345815663)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
09:40:14.345 (345865887)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
09:40:14.345 (345983920)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
09:40:14.346 (346009627)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
09:40:14.346 (346036706)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>(Integer)
09:40:14.346 (346056892)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>(Integer)
09:40:14.346 (346157489)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
09:40:14.346 (346184081)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
09:40:14.346 (346245018)|SYSTEM_METHOD_ENTRY|[6]|LIST<Case>.iterator()
09:40:14.346 (346529169)|SYSTEM_METHOD_EXIT|[6]|LIST<Case>.iterator()
09:40:14.346 (346564239)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.346 (346598134)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.346 (346720348)|SYSTEM_METHOD_ENTRY|[8]|SET<String>.add(Object)
09:40:14.346 (346748167)|SYSTEM_METHOD_EXIT|[8]|SET<String>.add(Object)
09:40:14.346 (346796236)|SYSTEM_METHOD_ENTRY|[9]|SET<Id>.add(Object)
09:40:14.346 (346818648)|SYSTEM_METHOD_EXIT|[9]|SET<Id>.add(Object)
09:40:14.346 (346854247)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.346 (346869583)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.346 (346888188)|SYSTEM_METHOD_ENTRY|[14]|SET<Id>.size()
09:40:14.346 (346905658)|SYSTEM_METHOD_EXIT|[14]|SET<Id>.size()
09:40:14.347 (347248929)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|select id, status__c from Case where Case__c = :tmpVar1
09:40:14.384 (384510981)|SOQL_EXECUTE_END|[25]|Rows:1
09:40:14.384 (384605951)|SYSTEM_METHOD_ENTRY|[26]|LIST<Case>.iterator()
09:40:14.384 (384645786)|SYSTEM_METHOD_EXIT|[26]|LIST<Case>.iterator()
09:40:14.384 (384662449)|SYSTEM_METHOD_ENTRY|[26]|system.ListIterator.hasNext()
09:40:14.384 (384685390)|SYSTEM_METHOD_EXIT|[26]|system.ListIterator.hasNext()
09:40:14.384 (384781296)|SYSTEM_METHOD_ENTRY|[26]|system.ListIterator.hasNext()
09:40:14.384 (384797019)|SYSTEM_METHOD_EXIT|[26]|system.ListIterator.hasNext()
09:40:14.384 (384856782)|DML_BEGIN|[30]|Op:Update|Type:Case|Rows:1
09:40:14.425 (425116213)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000OGf0|complex on Case trigger event AfterUpdate for [5009000000NFZvv]
09:40:14.425 (425343773)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
09:40:14.425 (425361419)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
09:40:14.425 (425385697)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
09:40:14.425 (425396930)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
09:40:14.425 (425413665)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>(Integer)
09:40:14.425 (425423831)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>(Integer)
09:40:14.425 (425447085)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
09:40:14.425 (425456612)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
09:40:14.425 (425482591)|SYSTEM_METHOD_ENTRY|[6]|LIST<Case>.iterator()
09:40:14.425 (425496327)|SYSTEM_METHOD_EXIT|[6]|LIST<Case>.iterator()
09:40:14.425 (425508060)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.425 (425526755)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.425 (425611549)|SYSTEM_METHOD_ENTRY|[8]|SET<String>.add(Object)
09:40:14.425 (425627912)|SYSTEM_METHOD_EXIT|[8]|SET<String>.add(Object)
09:40:14.425 (425647126)|SYSTEM_METHOD_ENTRY|[9]|SET<Id>.add(Object)
09:40:14.425 (425658054)|SYSTEM_METHOD_EXIT|[9]|SET<Id>.add(Object)
09:40:14.425 (425728210)|SYSTEM_METHOD_ENTRY|[11]|SET<Id>.add(Object)
09:40:14.425 (425756866)|SYSTEM_METHOD_EXIT|[11]|SET<Id>.add(Object)
09:40:14.425 (425767647)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.425 (425780564)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.425 (425793861)|SYSTEM_METHOD_ENTRY|[14]|SET<Id>.size()
09:40:14.425 (425801539)|SYSTEM_METHOD_EXIT|[14]|SET<Id>.size()
09:40:14.426 (426096383)|SOQL_EXECUTE_BEGIN|[16]|Aggregations:0|select id, Case__c, status__c from Case where id = :tmpVar1
09:40:14.429 (429545864)|SOQL_EXECUTE_END|[16]|Rows:1
09:40:14.429 (429628301)|SYSTEM_METHOD_ENTRY|[17]|LIST<Case>.iterator()
09:40:14.429 (429671443)|SYSTEM_METHOD_EXIT|[17]|LIST<Case>.iterator()
09:40:14.429 (429695256)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:40:14.429 (429720678)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:40:14.429 (429809964)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:40:14.429 (429825722)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:40:14.429 (429881202)|DML_BEGIN|[21]|Op:Update|Type:Case|Rows:1
09:40:14.463 (463637753)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000OGf0|complex on Case trigger event AfterUpdate for [5009000000NFZvW]
09:40:14.463 (463976069)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
09:40:14.463 (463994490)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
09:40:14.464 (464024296)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
09:40:14.464 (464036075)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
09:40:14.464 (464054041)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>(Integer)
09:40:14.464 (464064187)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>(Integer)
09:40:14.464 (464082001)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
09:40:14.464 (464090632)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
09:40:14.464 (464120517)|SYSTEM_METHOD_ENTRY|[6]|LIST<Case>.iterator()
09:40:14.464 (464136388)|SYSTEM_METHOD_EXIT|[6]|LIST<Case>.iterator()
09:40:14.464 (464149514)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.464 (464170656)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.464 (464297086)|SYSTEM_METHOD_ENTRY|[8]|SET<String>.add(Object)
09:40:14.464 (464330124)|SYSTEM_METHOD_EXIT|[8]|SET<String>.add(Object)
09:40:14.464 (464369338)|SYSTEM_METHOD_ENTRY|[9]|SET<Id>.add(Object)
09:40:14.464 (464390735)|SYSTEM_METHOD_EXIT|[9]|SET<Id>.add(Object)
09:40:14.464 (464470494)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.464 (464496414)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.464 (464519918)|SYSTEM_METHOD_ENTRY|[14]|SET<Id>.size()
09:40:14.464 (464533958)|SYSTEM_METHOD_EXIT|[14]|SET<Id>.size()
09:40:14.464 (464997723)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|select id, status__c from Case where Case__c = :tmpVar1
09:40:14.470 (470257246)|SOQL_EXECUTE_END|[25]|Rows:1
09:40:14.470 (470355231)|SYSTEM_METHOD_ENTRY|[26]|LIST<Case>.iterator()
09:40:14.470 (470373878)|SYSTEM_METHOD_EXIT|[26]|LIST<Case>.iterator()
09:40:14.470 (470386825)|SYSTEM_METHOD_ENTRY|[26]|system.ListIterator.hasNext()
09:40:14.470 (470409899)|SYSTEM_METHOD_EXIT|[26]|system.ListIterator.hasNext()
09:40:14.470 (470509561)|SYSTEM_METHOD_ENTRY|[26]|system.ListIterator.hasNext()
09:40:14.470 (470525042)|SYSTEM_METHOD_EXIT|[26]|system.ListIterator.hasNext()
09:40:14.470 (470574331)|DML_BEGIN|[30]|Op:Update|Type:Case|Rows:1
09:40:14.496 (496886190)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000OGf0|complex on Case trigger event AfterUpdate for [5009000000NFZvv]
09:40:14.497 (497205481)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
09:40:14.497 (497237854)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
09:40:14.497 (497290732)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
09:40:14.497 (497312403)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
09:40:14.497 (497345874)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>(Integer)
09:40:14.497 (497366922)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>(Integer)
09:40:14.497 (497401777)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
09:40:14.497 (497416929)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
09:40:14.497 (497457627)|SYSTEM_METHOD_ENTRY|[6]|LIST<Case>.iterator()
09:40:14.497 (497479939)|SYSTEM_METHOD_EXIT|[6]|LIST<Case>.iterator()
09:40:14.497 (497498080)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.497 (497525186)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.497 (497641898)|SYSTEM_METHOD_ENTRY|[8]|SET<String>.add(Object)
09:40:14.497 (497668448)|SYSTEM_METHOD_EXIT|[8]|SET<String>.add(Object)
09:40:14.497 (497705980)|SYSTEM_METHOD_ENTRY|[9]|SET<Id>.add(Object)
09:40:14.497 (497730319)|SYSTEM_METHOD_EXIT|[9]|SET<Id>.add(Object)
09:40:14.497 (497802865)|SYSTEM_METHOD_ENTRY|[11]|SET<Id>.add(Object)
09:40:14.497 (497827297)|SYSTEM_METHOD_EXIT|[11]|SET<Id>.add(Object)
09:40:14.497 (497844059)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.497 (497866773)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.497 (497891183)|SYSTEM_METHOD_ENTRY|[14]|SET<Id>.size()
09:40:14.497 (497907068)|SYSTEM_METHOD_EXIT|[14]|SET<Id>.size()
09:40:14.498 (498340732)|SOQL_EXECUTE_BEGIN|[16]|Aggregations:0|select id, Case__c, status__c from Case where id = :tmpVar1
09:40:14.501 (501249962)|SOQL_EXECUTE_END|[16]|Rows:1
09:40:14.501 (501309113)|SYSTEM_METHOD_ENTRY|[17]|LIST<Case>.iterator()
09:40:14.501 (501324265)|SYSTEM_METHOD_EXIT|[17]|LIST<Case>.iterator()
09:40:14.501 (501335080)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:40:14.501 (501350608)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:40:14.501 (501424732)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:40:14.501 (501464877)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:40:14.501 (501524097)|DML_BEGIN|[21]|Op:Update|Type:Case|Rows:1
09:40:14.539 (539982337)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000OGf0|complex on Case trigger event AfterUpdate for [5009000000NFZvW]
09:40:14.540 (540335148)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>(Integer)
09:40:14.540 (540363953)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>(Integer)
09:40:14.540 (540408524)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)
09:40:14.540 (540427176)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)
09:40:14.540 (540459014)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>(Integer)
09:40:14.540 (540477740)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>(Integer)
09:40:14.540 (540505308)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
09:40:14.540 (540520245)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
09:40:14.540 (540559682)|SYSTEM_METHOD_ENTRY|[6]|LIST<Case>.iterator()
09:40:14.540 (540581500)|SYSTEM_METHOD_EXIT|[6]|LIST<Case>.iterator()
09:40:14.540 (540599854)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.540 (540629645)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.540 (540755486)|SYSTEM_METHOD_ENTRY|[8]|SET<String>.add(Object)
09:40:14.540 (540778452)|SYSTEM_METHOD_EXIT|[8]|SET<String>.add(Object)
09:40:14.540 (540809855)|SYSTEM_METHOD_ENTRY|[9]|SET<Id>.add(Object)
09:40:14.540 (540827477)|SYSTEM_METHOD_EXIT|[9]|SET<Id>.add(Object)
09:40:14.540 (540872235)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
09:40:14.540 (540894403)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
09:40:14.540 (540916349)|SYSTEM_METHOD_ENTRY|[14]|SET<Id>.size()
09:40:14.540 (540929770)|SYSTEM_METHOD_EXIT|[14]|SET<Id>.size()

kevin Carotherskevin Carothers
Ahh - you have a recursive trigger....    
You need to implement a static boolean in a  helper class that's created for the instance of the call to your trigger.

This article should help;
http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

This was selected as the best answer