0

I have two objects - Customer Visit Quota and Contact Report. The contact report object has a "Number of Visits Field" that should roll up on the Customer Visit Quota. I have a master detail look up field on the Contact report object to allow this to happen. My problem is coming in with automatically entering the Customer Visit Quota based on the fields that are already required on the Contact Report. The name of the Customer Visit quota is as follows: External ID Month Year. I have all of those fields on my child object through the account object and a date field. I tried writing a trigger after doing some research - I am really new to coding-, but nothing seems to happen when I create a new Contact Report.

Can someone please help me figure out what I am doing wrong? Let me know if this isn't clear enough and I will try to explain it better.

trigger Quota_Assignment on Contact_Report__c (before insert) {

    Set<Id> quotaIdsSet = new Set<Id>();

    for(Contact_Report__c conRep:Trigger.New)
    {
        quotaIdsSet.add(conRep.Customer__r.JDECustomerNumber__c+' '+conRep.Contact_Date__c.month()+' '+conRep.Contact_Date__c.year());
    }

    Map<Id, String> quotaIdtoQuotaNameMap = new Map<Id, String>();

    for(Customer_Visit_Quota__c quota:[Select Id, Name from Customer_Visit_Quota__c where Id in :quotaIdsSet])
    {
        quotaIdtoQuotaNameMap.put(quota.Id, quota.Name);
    }

    for(Contact_Report__c conRep:Trigger.New)
    {
        conRep.Visit_Quota__c = quotaIdtoQuotaNameMap.get(conRep.Customer__r.JDECustomerNumber__c+' '+conRep.Contact_Date__c.month()+' '+conRep.Contact_Date__c.year());
    }
}
0

This value

conRep.Customer__r.JDECustomerNumber__c

which you are using to construct your unique name values, is null. Triggers don't have access to relationship fields; you must perform a query to get access to them.

This is also backwards:

for(Customer_Visit_Quota__c quota:[Select Id, Name from Customer_Visit_Quota__c where Id in :quotaIdsSet])
{
    quotaIdtoQuotaNameMap.put(quota.Id, quota.Name);
}

You can't populate the Id field with anything other than a Salesforce Id; you seem to be populating the Name field, so your query should filter on Name, and your Map should have the Name as key and the Id as value, rather than the reverse.

To make this work at all, your master-detail relationship has to allow reparenting, but your users will have to populate the field on record creation anyway because you can't create a detail record without a master. I personally would not implement this as a trigger; I would override the UI with Visualforce or Lightning instead.

| improve this answer | |
  • I didn't realize that my user would still have to input an initial value with this. That is what we are trying to avoid to try to prevent the users from getting confused. Thanks. I will look into other options. – ArclinSF Jun 10 '19 at 19:37

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.