2

I am in the process of writing a trigger to create a child record when a parent record is created. I have it mostly working but need some help. Below is my code:


trigger AutoCreateCommission on Contract_to_Close__c (after insert) {
    List<Commission__c> commission = new List<Commission__c>();

    for (Contract_to_Close__c newC2C: Trigger.New) {
        if (newC2C.Buyer_Agent_Name__c != null) {
            commission.add(new Commission__c(
                        Contract_to_Close__c = newC2C.Id,
                        Agent__c = newC2C.Buyer_Agent_Name__c,
                        Commission__c = newC2C.Buyer_Broker_Commission__c,
                        Buyer_Seller_Agent__c = 'Buyer',
                        Office__c = newC2C.Buyer_Agent_Name__r.Department, //this isn't working (lookup to Department field on User record.)
                        Commission_Percent__c = newC2C.Buyer_Broker_Commission_Percent__c));
                        //Owner = newC2C.Buyer_Agent_Name__c)); --need to update owner with same Id as Agent__c of new record
        }
    }
        for (Contract_to_Close__c newC2C: Trigger.New) {
        if (newC2C.Seller_Agent_Name__c != null) {
            commission.add(new Commission__c(
                        Contract_to_Close__c = newC2C.Id,
                        Agent__c = newC2C.Seller_Agent_Name__c,
                        Commission__c = newC2C.Seller_Broker_Commission__c,
                        Buyer_Seller_Agent__c = 'Seller',
                        Office__c = newC2C.Seller_Agent_Name__r.Department, //this isn't working (lookup to Department field on User record.)
                        Commission_Percent__c = newC2C.Seller_Broker_Commission_Percent__c));
                        //Owner = newC2C.Seller_Agent_Name__c)); --need to update owner with same Id as Agent__c of new record
        }
    }        

    insert commission;
}

I've notated where I am having issues but here are some notes:

  • The Buyer_Agent_Name__c and Seller_Agent_Name__c are lookups to the User object. Basically I am trying to set the Office__c equal to the Department field on the user object.
  • I need to update the Owner field of the new child record equal to either the Buyer_Agent_Name__c or Agent__c field but am getting errors that I cannot update that field. I'm assuming it's because the record hasn't been updated yet. Any thoughts on how to accomplish this?

Thanks in advance,

Maria

4
  • 1
    The API field name of standard owner fields on all Salesforce sObjects is typically OwnerId. I wonder if that is your issue? – greenstork Sep 12 '14 at 18:38
  • Awesome, that resolved the Owner part of my issue. Thanks! – JustAGirlyGeek Sep 12 '14 at 18:42
  • 1
    Office__c = newC2C.Buyer_Agent_Name__r.Department These are too far away from the contents of Trigger.new. You'll need to query for these. If Buyer_Agent_Name__r is the Owner, use a collection of OwnerID's, if not, get a collection of contract ID's from which to create your queries. – crmprogdev Sep 12 '14 at 18:46
  • Ok, thanks. 99% of the time the creator of the parent record will end up changing the ownership of the record after record creation. I will try querying those fields. – JustAGirlyGeek Sep 12 '14 at 18:52
2

I see a couple of issues that you're facing.

  1. You're trying to set Office__c equal to Buyer_Agent_Name__r.Department but the problem is that the related parent Buyer_Agent_Name__r is not available in Trigger.New. Trigger.New only contains fields on the object in the trigger set, so in this case, only the Id in the lookup field Buyer_Agent_Name__c. In order to get fields off the related parent object, you would need to SOQL for those fields.
  2. The second issue is that the owner is typically indicated via standard Salesforce fields with the API name OwnerId. Try that instead. OwnerId would not be a field that you could set on an object that is the child in a master-detail relationship, it's also worth noting.
1
  • 1) That makes sense, thanks! Now I just need to figure out the logic. 2) This works perfectly, thanks! And it's a lookup relationship so I'm all set there. Thanks for the tip! – JustAGirlyGeek Sep 12 '14 at 19:47

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.