1

I have been trying to use the FFLIB enterprise service layer module to both insert a new record and update an existing record while registering relationships between both but I am unable to update the old record, other fields update on the record so I assume its got to do with either the registering of the relationship itself, the order which it is done or the initial setup of the UnitOfWork record. I cant get it to work and would really appreciate some insight into what i need to do to achieve it..

Note: Split_From_Payment__c is updated correctly, the new payment record is created correctly and all fields in oldLPP are updated correctly except for the Merged_From_Payment__c field which is the one that I cant get to update.

code:

//more code above to validate records...
oldLPP.Status__c= 'Cancelled';
oldLPP.Cancelled_Reason__c = 'Rescheduled';
oldLPP.Is_Superseded__c = dueAmt == 0 ? false : true;
oldLPP.Amount__c = dueAmt == 0 ? dueAmt : oldLPP.Amount__c;

Schema.SObjectType[] sobjsList = new Schema.SObjectType[] { Payment__c.SObjectType};
uow = new fflib_SObjectUnitOfWork(sobjsList);

if(oldLPP != null) 
    uow.registerRelationship(newLPP, Payment__c.Split_From_Payment__c, oldLPP);

newLPP.Amount__c = dueAmt;
newLPP.Due_Date__c = newDueDate;

system.debug('### newLPP: ' + newLPP);
uow.registerNew(newLPP);

if(oldLPP != null){
    uow.registerRelationship(oldLPP, Payment__c.Merged_Into_Payment__c, newLPP);
    uow.registerDirty(oldLPP,Payment__c.Merged_Into_Payment__c, newLPP);
}
uow.commitWork();
3
  • 2
    Is this the FinancialForce library? Have you checked with their support resources? May 6, 2019 at 18:31
  • Yes it is, did check the website but couldn't find anything on the subject, it shows how it's supposed to be used and I would think i'm using it in the same way but obviously not else I wouldn't have this issue.
    – user64485
    May 6, 2019 at 22:44
  • currently the workaround is to do two commitWork() method calls, one for the newLPP payment and another for the oldLPP record (register relationship and register dirty). BUT i'm sure that there has to be a way to do all in one commitWork() call. Would really want someone to point me in the right direction with this.
    – user64485
    May 8, 2019 at 4:14

1 Answer 1

1

So, UnitOfWork will do inserts first, then updates, then deletes

I would simplify your code to look like this:

if (oldLPP != null {...} // set oldLPP fields
// set newLPP fields
...
// define scope of UoW
Schema.SObjectType[] sobjsList = new Schema.SObjectType[] { Payment__c.SObjectType};
uow = new fflib_SObjectUnitOfWork(sobjsList);

uow.registerNew(newLPP);

// build xrefs
if (oldLPP != null) {
   // new -> old
  uow.registerRelationship(newLPP,Payment__c.Merged_Into_Payment__c,oldLPP);
  // old -> new
  uow.registerDirty(oldLPP,Payment__c.Merged_Into_Payment__c,newLPP);
}
uow.commitWork();

There is of course, no rule against explicitly setting the relationship fields yourself

newLPP.Merged_Into_Payment__c = oldLPP.Id;

You must log in to answer this question.

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