1

Wondering why for the below code I am getting Field Integrity Exception error. Have 2 Lookup fields to Opportunity in Opportunity Object. ParentOpportunity and UltimateParentOpportunity. Trying to update the Ultimate as the current Opportunity when a condition satisfies.

Apex Code Sample

  List<Opportunity> optyList = [Select Id, Name,ParentOpportunity__c, UltimateParentOpportunity__c 
                                from Opportunity 
                                LIMIT 10];  //  'UltimateParentOpportunity__c' and 'ParentOppoertunity__c' are Lookups to Opportunity object             

 for(Opportunity opp: optyList){
    opp.UltimateParentOpportunity__c = opp.Id;
 }

 update optyList;

 System.debug(' optyList after-- '+optyList);
5
  • Check this article: help.salesforce.com/…
    – Json
    Commented Mar 22, 2020 at 7:30
  • Not related to this
    – Brav
    Commented Mar 22, 2020 at 7:32
  • 2
    I think the problem is that you're trying to set lookup field to itself. It's not possible. If that's the problem, please, refer me and I'll post it as answer. Commented Mar 22, 2020 at 13:44
  • Yes. I think it might be. We have a parent relation look up to Opportunity from Opportunity. Trying to set the the Opportunity in the top most hierarchy as the Ultimate Parent of the current Opportunity. Note:
    – Brav
    Commented Mar 22, 2020 at 14:22
  • Note: I am able to do it manually from the record page not progamatically
    – Brav
    Commented Mar 22, 2020 at 14:28

1 Answer 1

1

It appears you have two problems here the first and glaring issue is you cannot set a lookup field to itself typically the head of a hierarchy points to null for its parent. Just to be sure I tried setting a lookup to Opportunity to itself and was presented with this error on the GUI

"You cannot set a hierarchy field self lookup to point to itself or a child record. Either edit one of the child records to not refer to this record, or do not set the field on this record."

I also tried this pragmatically and as expected got a circular dependency error

System.DmlException: Update failed. First exception on row 0 with id 006...; first error: CIRCULAR_DEPENDENCY, attempt to violate hierarchy constraints

Which leads me to the Field Integrity Exception. There is something else going on here that is causing this issue that is difficult to diagnose with the current code given to us. If you post more of the trigger like what context we are in I will attempt to reproduce and hopefully be able to edit my answer

7
  • Just executed the above code anonymously and reproduced the error. I am updating the Look up field to itself (might be someting there). I can do it manually from the record page but not programmatically
    – Brav
    Commented Mar 22, 2020 at 22:26
  • So you are saying manually you are able to set the lookup field value to the record itself? Second the code you posted does that run in a after update? after Insert? Both? Third can you post relevant log information? Are you certain that code is throwing the error?
    – Cowboy
    Commented Mar 22, 2020 at 23:49
  • Yes. 2 lookup fields to Opportunity object. Parent Opportunity and Ultimate Parent Opportunity. I can't set the Parent as the current Opty (self lookup restriction imposed) but able to do for Ultimate Parent Opportunity. And the code runs only in before update whenever there is a change in Parent.
    – Brav
    Commented Mar 23, 2020 at 0:07
  • So you want all children to be able to have the top most Parent Reference?
    – Cowboy
    Commented Mar 24, 2020 at 13:20
  • Yes. That's the point. I have the logic but a part of it requires to update the Ultimate Parent to itself when the parent is null. I could also use a Formula field to display the Ultimate if parent is null but that's not what I am looking for.
    – Brav
    Commented Mar 24, 2020 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .