1

I have Contact records - of RecordType = AiM CR Writer - that when created I want the Account Name to automatically populate with a specific account. Here's the trigger I've written so far...This works, but I'd like to use a variable for the accountid so I don't have to hard code it. Is there a way to do this? Thanks

// if this is an AiM CR Writer contact then populate customer name to AiM CR Writer Master Customer

trigger UpdateCRWriterMasterCustomer on Contact (before insert) {
Id rtId1 = [select Id,name from RecordType where name='AiM CR Writer' and SObjectType='CONTACT' limit 1].id;
          for (Contact cont : Trigger.new) {    
                 If (cont.RecordTypeId == rtId1 ){         
            for (Contact c1 : Trigger.new){                  
                     c1.accountid = '001e000000Vw5Pb';     
            }                 
         }
    }
}
2
  • Amy, are you saying you have multiple Accounts which could be written to this custom contact.account field? Do you have multiple RecordTypes for Contacts, at which point you want a different account to populate your Custom Contact.account field? – Fifedog Mar 17 '15 at 15:53
  • I'm saying I only have one Account that will be populated in the accountid field, and instead of using the hard coded accountid, I want the trigger to dynamically find the accountid. I only have 1 RecordType for my Contact, and they will always have the same 1 accountid...Does that make sense? Sorry if I'm not explaining this well. I'm very new to doing this. Thank you so much for responding! – Amy Maxwell Mar 17 '15 at 16:09
1

You can use a List Custom Setting for this. In your Sandbox, go to Develop > Custom Settings, and create a new Custom Setting (list type), let's say you call it "AiMSetting". You then add a text field to the Custom Setting (AiMAccountID) and in the Manage view you can create a new record ("AiMRecord") with the desired account ID as the value in the AimAccountID field.

Within your trigger, you would just put this somewhere higher in the trigger:

AimSetting__c myAimID = AimSetting__c.getValues('AiMRecord');
Id idToUse = Id.valueOf(myAimID.AimAccountId__c);

and then later:

c1.accountid = idToUse;

Now, if you want to update the id used in the trigger, you can just change the ID value in the Custom Setting record (within the declarative settings interface) rather than pushing a change set every time.


Added after discussion in comments

If you're confident you can prevent multiple accounts from having the master customer name, you can use this code to query for the account's ID within your trigger:

trigger UpdateCRWriterMasterCustomer on Contact (before insert) {
    Id rtId1 = [select Id,name from RecordType where name='AiM CR Writer' and SObjectType='CONTACT' limit 1].id;

    List<Account> masterCustomer = [SELECT Id, Name FROM Account WHERE Name = '<master customer name>' LIMIT 1];
    Id masterCustomerAccountId;
    for (Account a : masterCustomer) masterCustomerAccountId = a.id;

    for (Contact cont : Trigger.new) {    
        If (cont.RecordTypeId == rtId1 ){         

            c1.accountid = masterCustomerAccountId;     
        }
    }
}
9
  • Thanks, DJ for responding! Doesn't this still mean I'm hard coding that value - albeit indirectly - using this approach? If I push this to the test environment -which I will have to do - I'll still have to update the ID value. I'm trying to get around always having to update the ID and instead have it get the id based on the name of the Account... – Amy Maxwell Mar 17 '15 at 16:33
  • Ah, I see what you're getting at. In that case, you don't want a Custom Setting at all. I will edit this answer to address your issue better. – OBerm Mar 17 '15 at 16:38
  • On second thought, what you propose is not such a great idea. Since the Name field on Account does not have to be unique, in theory you can have multiple Accounts with the master customer name you're searching for, which would break this logic. – OBerm Mar 17 '15 at 16:54
  • Yes, you're right. In theory, there could be multiple Accounts with that name, but it's very highly unlikely that there will be another Account with that name...If not this solution, what do you suggest? Back to the Custom Setting? – Amy Maxwell Mar 17 '15 at 17:19
  • If you create the Custom Setting, it can hold different values in your Sandbox and in your Production orgs, which should help you get around the issue with your unit testing, though it is a rather involved fix for a relatively simple problem. Otherwise, if you're confident you can control duplicate accounts with the same Master Customer name, then you can go ahead and query for that Account's ID right in the trigger. – OBerm Mar 17 '15 at 18:21

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.