I am trying to set the value of one object in a custom object to be the same as the value of another object before deleting the first object. When I use the assignment operator, nothing happens although I'm certain that the Account__r of dbc is populated. Does anybody know why this doesn't work?

for ( Customer__c dbc : [SELECT Account__c, Account__r.Priority__c, Unique_Id__c FROM Customer__c WHERE Unique_Id__c IN :customers.keySet( )])
            Customer__c c    = customers.get( dbc.Unique_Id__c );
            // replace the account of the last added customer if the priority is less
            // than that of the previous entry   
            // I could not find the solution to this problem. I cannot access
            // c.Account__r.Priority__c without using a DB call and could find no solution online.
            if ( accountMap.get( c.Account__c ).Priority__c.compareTo( dbc.Account__r.Priority__c ) 
                 < 0 )
                c.Account__r = dbc.Account__r;
            entriesToBeDeleted.add( dbc );

thanks in advance

  • why are you using < 0 in the compareTo(..) method? This doesn't test for equality; it tests for lexicographically precedes. – cropredy Sep 10 '14 at 21:08
  • Yes, I'm trying to do that :) thanks. Also I found my solution (for future references): c.Account__r = dbc.Account__r; should be: c.Account__c = dbc.Account__r.Id; – De Heer Jør van Oerle Sep 10 '14 at 21:12
  • yes, of course -- good work – cropredy Sep 10 '14 at 21:13

Instead of using the c.Account__r, I should have used c.Account__c and I also should have used the c.Account__r.Id rather than c.Account__r.

c.Account__r = dbc.Account__r;

should be

c.Account__c = dbc.Account__r.Id;

maybe someone else is also struggling with this.

| improve this answer | |
  • Jør, know that you should also be able to accept your own answer. This can be done by checking the (V) button on the left of your answer. – Samuel De Rycke Oct 7 '14 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.