-4

Good morning, I have written the following APEX Trigger and it does seem to work in my Full Sandbox. My question being I am trying to learn on the fly here is can anyone help me with TestCode for it PLEASE so that I can move it into production? Thank you in advance!!!

trigger newContactRequestConvert on Account_Request__c (before Update) 
{
    List<Contact> contacts = new List<Contact>();
    List<Contact> Update_contacts = new List<Contact>();
    List<Account_Request__c> aRequest = Trigger.new;
    for (Account_Request__c ar : Trigger.new) {
        if (ar.Contact_Request_Status__c == 'Convert') {
            Boolean Enterprise = ar.Contact_Type__c == 'Enterprise';
            Boolean Media = ar.Contact_Type__c == 'Media';
            if (Enterprise || Media) {
                Id rtId = Enterprise ? '012000000000lHu' : '012000000000lGm';
                Contact converted = new Contact(
                        RecordTypeId = rtId,
                        FirstName = ar.First_Name__c,
                        LastName = ar.Last_Name__c,
                        Title = ar.Contact_Title__c,
                        Email = ar.Contact_Email__c,
                        Phone = ar.Contact_Phone_Number__c,
                        Contact_Requested_By__c = ar.Contact_Requested_By__c,
                        Contact_Requested__c = 'Yes'
                        );
                 List<Contact> con = [select id from Contact where FirstName=:ar.First_Name__c and LastName= :ar.Last_Name__c];
                 if( con.isEmpty() ){
                     System.debug('Check if the contact already exists');
                     contacts.add(converted);
                  }
                                    }    
                                                         }    
                                                }

insert contacts;
  • 8
    It is likely this question will be closed because you've not included any details about anything you've tried. This is not a charity; people here will not generally write your code for you. – Jonathan Hersh Jan 5 '15 at 18:00
  • Do you have anything to show? developer.salesforce.com/page/… and sfdc99.com – EricSSH Jan 5 '15 at 18:19
  • You need to write code that creates test records (Contacts, perhaps Accounts, and Account Requests). Since this is an Update trigger, you'll need to insert, then manipulate and update your Account Requests to make the trigger fire. Then use System.assert to see if you got the expected results. For full coverage, you'll need to have records that cover your different conditions. – Thomas Taylor Jan 5 '15 at 18:54
1

For one thing I can see a problem where you are using hardcoded Ids for your record types, which will not work when you attempt to migrate to production. You should write another query before the loop which will find those recordType Ids

Not the answer you're looking for? Browse other questions tagged or ask your own question.