0

I am an admin, new to Apex development. I will be uploading Opportunities periodically through Dataloader, all of record type "Outbound Call Opportunity." The file will contain the following info for each Opp:

  • RecordTypeID
  • TIN__c (Tax ID Number)
  • Product__c
  • Outbound_Call_Score__c

I need my code to do the following:

  • Query Contacts where the Opp's TIN__c matches Contact's Tax_ID_Number__c and set matching Contact as Opp Owner
  • Confirm before insert that there are no other Outbound Call Opportunities for that Product with the same Contact. In other words, there can only be one Outbound Call Opportunity per Contact per Product.
  • Give error if a Outbound Opportunity exists already with that contact and product, else update pertinent fields

I think I have that sorted out, but open to suggestions on how to clean it up, but I have one remaining requirement:

  • Rollup Outbound_Call_Score__c to related Contact. Each Opp will have a numeric weight signifying how "important" it is. I want that number to aggregate from all Outbound Call Opps and rollup the total to the Contact on a field also named Outbound_Call_Score__c. I am not sure how to accomplish that, any help would be appreciated!

Trigger:

trigger OpportunityTrigger on Opportunity (before insert) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {
      OpportunityHandler.insertOutboundCallLists(Trigger.new);
    } 
  }
}

Handler class:

public class OpportunityHandler {
    
    public OpportunityHandler() { }
    
    public static void insertOutboundCallLists(List<Opportunity> newOpps){
        Set<String> oppTINs = new Set<String>();
        String outboundCallRecTypeID = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Outbound Call Opportunity').getRecordTypeId();
        
        for(Opportunity opp : newOpps){
            oppTINs.add(opp.TIN__c);
        }        
        //system.debug(oppTINs);
        
        List<Contact> contacts = [SELECT Id, OwnerId, Tax_ID_Number__c, Outbound_Call_Score__c 
                                  FROM Contact WHERE Tax_ID_Number__c IN :oppTINs]; 
        List<Opportunity> existingOutboundOpps = new List<Opportunity>([SELECT Id, Product__c, Contact__r.Id, Contact__r.Tax_ID_Number__c 
                                                                        FROM Opportunity WHERE RecordTypeId = :outboundCallRecTypeId 
                                                                        AND Contact__r.Id IN :contacts]);
        system.debug(contacts);
        system.debug(existingOutboundOpps);
        
        if(contacts.size() > 0){
            for(Opportunity opp : newOpps){
                for(Integer i = 0; i < existingOutboundOpps.size(); i++){
                    if(opp.TIN__c == existingOutboundOpps[i].Contact__r.Tax_ID_Number__c && opp.Product__c == existingOutboundOpps[i].Product__c){
                        opp.addError('Outbound Opportunity Already Exists.');
                    } else {
                        for(Integer j = 0; j < contacts.size(); j++){
                            if(opp.TIN__c == contacts[j].Tax_ID_Number__c && opp.RecordTypeId == outboundCallRecTypeID){
                                opp.Name = 'test';
                                opp.CloseDate = system.today() + 14;
                                opp.StageName = 'Prospecting';
                                opp.OwnerId = contacts[j].OwnerId;
                                opp.Contact__c = contacts[j].Id;
                                //contacts[j].Outbound_Call_Score__c = contacts[j].Outbound_Call_Score__c + opp.Outbound_Call_Score__c;
                            }   
                        }
                    }
                }  
            } 
        } 
    }     
}
1
  • for rollup - use DLRS
    – cropredy
    Mar 29 at 22:51

Your Answer

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

Browse other questions tagged or ask your own question.