1

I'm creating my first trigger (i'm an admin). It creates a new custom object,StakeholderContact every time a contact is created where 1-4 boxes are checked in the contact record. Right now I’m focused on the after insert portion, will do the after update part after I get the after insert working right (open to suggestions on that part too though). The trigger currently does what it’s supposed to IF 1 of the 4 boxes is checked but it does NOT create multiple new StakeholderContacts if more than 1 box is checked. I understand due to the if statement it’s stopping at whatever box is checked first that it hits & never goes beyond that. My question is, what should the loop/trigger look like so that it creates 4 new StakeholderContacts if all 4 boxes are checked in the contact record (tried for (Integer i = 0; i < 5; i++) so it would run 4 times but again understand why that’s not right, just not sure what is)? Also, if I run it 4 times how do I make sure I don’t end up with more than 4 records if all boxes are checked? Also open to any suggestions on basic trigger stuff I got wrong...Thanks in advance!

trigger CreateStakeholderContact on Contact (after insert, after update) {

//trigger purpose is to create new Key Buyer records connected to contacts & the Attack plans when new or existing contacts have the buyer box(es) ticked
//every trigger has this loop & a list to hold all new records
List<StakeholderContact__c> newkeybuyers = new List<StakeholderContact__c> ();


//go through the list of contacts and get all the related plans 

//the map to hold all related plans for these contacts
Map <Id, THE_Prospect_Plan__c> Plans = new Map <Id, THE_Prospect_Plan__c>();

// go through the list of plans and add data into the map 
{

For (THE_Prospect_Plan__c Plan : [select id from THE_Prospect_Plan__c where Id in :Plans.keyset ()])
{
Plans.put (Plan.Id, Plan);
}
//Go through the list of contacts and create the stakeholders to write
for (Contact C: Trigger.new) {
//which contacts are we using, only contacts where a buyer field is true & plan id is not null
If (C.ContPlanID__c != null && ((C.BuyerEconDMaker__c == true) || (C.ContactCoach__c == true) || (C.ContProcurementBuyer__c == true) || (C.ContTechnical_Buyer__c == true))){

//create a keybuyer
StakeholderContact__c Keybuyer = new StakeholderContact__c ();
//set the values
Keybuyer.THE_Prospect_Plan__c = C.ContPlanID__c;
Keybuyer.ContactPlan1__c=C.Id;
if (C.BuyerEconDMaker__c == true){
KeyBuyer.KeyBuyerType__c = 'Economic Decision-Maker';}
if (C.ContactCoach__c == true){
KeyBuyer.KeyBuyerType__c = 'Coach';
}if (C.ContProcurementBuyer__c == true){
KeyBuyer.KeyBuyerType__c = 'Procurement';
}if (C.ContTechnical_Buyer__c == true){
KeyBuyer.KeyBuyerType__c = 'Technical';
}


//stick the Keybuyer into the list we created earlier
newkeybuyers.add (Keybuyer);
}

// Save the key buyers
Insert NewKeybuyers;
}
}
}

1 Answer 1

1

You will need to map your checkbox API field names to whatever your corresponding values will be for the key buyer type, then go through each of the fields and check whether the checkbox is selected. If so, get the corresponding value from your map and create a new key buyer record that you will add to a list which you insert later.

In this case if you're looking for a generic method, you need to use Maps and generic sObjects

So your code should look something like this (hasn't been compiled or tested, you might need to tweak it a bit):

Map <String, String> fieldAPINameToValue = new Map <String, String> ();
fieldAPINameToValue.put('BuyerEconDMaker__c', 'Economic Decision-Maker');
fieldAPINameToValue.put('ContactCoach__c', 'Coach');
fieldAPINameToValue.put('ContProcurementBuyer__c', 'Procurement');
fieldAPINameToValue.put('ContTechnical_Buyer__c', 'Technical');

If (C.ContPlanID__c != null)
{
    // Convert the concrete sObject (contact) to generic sObject
    sObject contact = C;

    for (String fieldName : fieldAPINameToValue.keySet())
    {
        // Cast the value from the checkbox to boolean and check if it's true
        if ((Boolean)contact.get(fieldName) == true)
        {
            //create a keybuyer
            StakeholderContact__c keybuyer = new StakeholderContact__c ();
            //set the values
            keybuyer.THE_Prospect_Plan__c = C.ContPlanID__c;
            keybuyer.ContactPlan1__c = C.Id;
            // Get the value from the map of the corresponding checkbox selected 
            keybuyer.KeyBuyerType__c = fieldAPINameToValue.get(fieldName);
            //stick the Keybuyer into the list we created earlier
            newkeybuyers.add (keybuyer);
        }
    }
}

insert newkeybuyers;

Hope that answers your question.

1
  • Thanks (somewhat belated) for responding. When I first read your response, I thought YES I get it (after rereading Map dev info), after playing all afternoon I'm not so sure. Question, do I need to write the string & create block for each of my 4 fields for this to work? (String fieldName : fieldAPINameToValue.keySet())...through newkeybuyers.add (keybuyer)?
    – Vadmin
    Aug 14, 2014 at 1:56

You must log in to answer this question.

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