function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike MacLeodMike MacLeod 

Trouble deploying a trigger.

I have this trigger written, which passed validation no problem. THe trigger works in Sandbox but will not deploy in production;

 

Below are the warnings:

 

 

Commission_Calculation__c.Practitioner_Account__cCustom Field Definition7313cannot create two master detail fields with the same domain on one objectGenerateCommissionApex Trigger139Didn't understand relationship

 

 

'Commission_Calculations__r' in FROM part of query call. 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.

 

Here is the trigger:

 

trigger GenerateCommission on Revenue_Event__c ( after insert )
{
    //  build a set of Accounts (doctors) related to each Revenue Event in this trigger
    Set<Id> set_AccountIDs = new Set<Id>();

    for ( Revenue_Event__c event : Trigger.new )
    {
        set_AccountIDs.add( event.Account__c );
    }

    //  query the Account records (doctors) related to the Revenue Events in this trigger
    Map<Id,Account> map_Doctors = new Map<Id,Account>
    (   [   SELECT  Id, (SELECT Id, Practitioner_Account__c, Sales_Agent__c FROM Commission_Calculations__r WHERE Active__c = true)
            FROM    Account
            WHERE   Id IN :set_AccountIDs
        ]
    );
    if ( map_Doctors.isEmpty() ) return;

    List<Commission__c> list_Commissions = new List<Commission__c>();

    //  for each Revenue Event,
    for ( Revenue_Event__c event : Trigger.new )
    {
        //  look up the associated Account (doctor)
        Account doctor = map_Doctors.get( event.Account__c );

        //  for each Commission Calculation related to the Account (doctor),
        for ( Commission_Calculation__c calculation : doctor.Commission_Calculations__r )
        {
            //  create a new Commission record
            list_Commissions.add
            (   new Commission__c
                (   Revenue_Event__c            = event.Id,
                    Sales_Agent__c              = calculation.Sales_Agent__c,
                    Commission_Percentage__c    = calculation.Id
                )
            );
        }
    }
    //  insert the new Commission records into the database
    insert list_Commissions;
}

 

 

If anyone has thougths, I would appreciate them!

CRMScienceKirkCRMScienceKirk

Hi Mike,

 

Does the deployment also include the new Master-Detail field?  It kind of sounds like the Account object already has one in place (1st error) and its Child Relationship Name isn't Commission_Calculations__r.  Check the Account Custom Fields list for an existing Master-Detail and confirm the Child Relationship Name used if there is one.

Mike MacLeodMike MacLeod

I think the problem might be the test class:

 

@istest
public class TestCommissionAutomation2{
static testMethod void insertNewRevenueEvents() {

        //First, prepare 20 Revenue Events for the test data
        Revenue_Event__c[] RevenueToCreate = new Revenue_Event__c[]{};
        for(Integer x=0; x<20;x++){
            Revenue_Event__c r  = new Revenue_Event__c
                (Account__c = '001Z000000gFye0',
                Net_Revenue__c = null,
                Amount__c = 300.00);
                RevenueToCreate.add(r);       
}
Insert RevenueToCreate;
}

}

 

 

It's only testing creating revenue events and only gets 50% coverage. If you have an idea how to edit the test class i'm all ears!

CRMScienceKirkCRMScienceKirk

Even at just 50% you're beyond the 1% required for a trigger.  I'd spend my time figuring out the two errors you originally posted.

 

Commission_Calculation__c.Practitioner_Account__cCustom Field Definition7313cannot create two master detail fields with the same domain on one objectGenerateCommissionApex Trigger139Didn't understand relationship

 

'Commission_Calculations__r' in FROM part of query call. 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.

 

Did you see an existing MD field on your Account Object?  I'm assuming there is a near identical (parent/child) Master-Detail already there which is why you get the first error.  It's details, particularly the Child Relationship Name, are different and that's why you're getting the 2nd __r error.

Mike MacLeodMike MacLeod

Now I'm getting something even weirder:

 

Commission_Calculation__c.Practitioner_Account__c

Custom Field Definition  

cannot create two master detail fields with the same domain on one object

 

Commission__c.Sales_Agent__c

Custom Field Definition  

There is already a Child Relationship named Commissions on Sales Agent.

 

Commission__c.Commission_Percentage__c

Custom Field Definition

There is already a Child Relationship named Commissions on Commission Entitlement.

 

Commission__c.Total_Commission__c

Custom Field Definition

Field Commission_Percentage__r does not exist. Check spelling.

 

GenerateCommission

Apex Trigger

Didn't understand relationship 'Commission_Calculations__r' in FROM part of query call. 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.