2

I have a custom object called "Recurring Storage Revenue". This object is populated via integration so the owner of the revenue records is the system user. I need to build a trigger so that the owner of the revenue records is updated to the owner of the parent record (the standard Account object) whenever the revenue record is created or updated. I am getting some errors to the trigger I wrote - any thoughts? Also would appreciate a test class if possible?

trigger UpdateOwnerID on Recurring_Storage_Revenue__c (before insert, before update) {

Map<Id,Id> accIdToNewOwnerId = new Map<Id,Id>();
for(Recurring_Storage_Revenue__c rsr : [Select 
                        Id, 
                        OwnerId, 
                        Account.OwnerId 
                    From 
                        Recurring_Storage_Revenue__c 
                    Where 
                        Id in :trigger.NewMap.keySet() 
                    AND 
                        OwnerId != Account.OwnerId])
{
    // if the opp owner deviates from the relatedrecord owner, add to the map
    accIdToNewOwnerId.put(rsr.Id,rsr.Account.OwnerId);
}

// now go through the records in the trigger and update where applicable
for(Recurring_Storage_Revenue__c rsr : trigger.new)
{
    if(accIdToNewOwnerId.containsKey(rsr.Id)){
        rsr.OwnerId = accIdToNewOwnerId.get(rsr.Id);
    }
}
}
5
  • 1
    what is the error message you are getting?
    – Anurag
    Jun 28, 2016 at 15:33
  • Looks like a before trigger which is fine but you won't have an Id in Trigger.new how exactly are you pulling the records?
    – d_k
    Jun 28, 2016 at 15:42
  • was wondering if I could get help with that!
    – Paul
    Jun 28, 2016 at 15:48
  • Make the context after insert/update!
    – d_k
    Jun 28, 2016 at 16:02
  • I suggest you to check @Ishwar's answer and go with process builder. Jun 28, 2016 at 16:25

2 Answers 2

0

enter image description hereCheck the snapshot where I managed to update owner of the opportunity to its Account's owner.

You can check this process.

Reference

0

Can you try this, changed the event to after insert

trigger UpdateOwnerID on Recurring_Storage_Revenue__c (after insert) {

List<Recurring_Storage_Revenue__c> updateList = new List<Recurring_Storage_Revenue__c>();
Map<Id,Id> accIdToNewOwnerId = new Map<Id,Id>();
for(Recurring_Storage_Revenue__c rsr : [Select 
                        Id, 
                        OwnerId, 
                        Account.OwnerId 
                    From 
                        Recurring_Storage_Revenue__c 
                    Where 
                        Id in :trigger.NewMap.keySet() 
                    AND 
                        OwnerId != :Account.OwnerId])
{
    // if the opp owner deviates from the relatedrecord owner, add to the map
    accIdToNewOwnerId.put(rsr.Id,rsr.Account.OwnerId);
}

// now go through the records in the trigger and update where applicable
for(Recurring_Storage_Revenue__c rsr : trigger.new)
{
    if(accIdToNewOwnerId.containsKey(rsr.Id)){
        rsr.OwnerId = accIdToNewOwnerId.get(rsr.Id);
        updateList.add(rsr);
    }
}
update updateList;
}
7
  • I tried this but am getting following error: Error: Compile Error: expecting a colon, found 'Account.OwnerId' at line 14 column 35
    – Paul
    Jun 28, 2016 at 16:04
  • whats the error?
    – Anurag
    Jun 28, 2016 at 16:05
  • can you try now, i updated my answer
    – Anurag
    Jun 28, 2016 at 16:08
  • this is the next error: Error: Compile Error: Didn't understand relationship 'Account' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 5 column 40
    – Paul
    Jun 28, 2016 at 16:11
  • what is the childrelationship name on field relation to account?
    – Anurag
    Jun 28, 2016 at 16:14

You must log in to answer this question.

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