function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChrisColeChrisCole 

Exclude certain record type from trigger

Hello!
I'm new to Apex (and develoment in general). I have inherited an org that has a trigger on a custom object called "Support Request." Support Requests (SRs) havea  look-up relationship with Opportunities (and Account). In the past, all SRs were associated with an Oppty. I am creating a new record type for SRs that, although they are associated with an account, are not necessarily assocaited with an opportunity.

The following Trigger has been in place for years to auto populate the Account ID based on the associated Opportunity but now I need to prevent it from running when the user creates a SR with my new record type (01I500000006ehC). I tried a few different things (like putting an IF statement around the whole trigger), but - since I don't really know what I'm doing and I couldn't find a similar use case on the forum, nothing worked :)

Here is the exisitng trigger :

trigger MapAccountName on Support_Request__c (before insert) {
Support_Request__c obj = Trigger.new[0];
Opportunity opp = [select AccountId from Opportunity where Id = : obj.Opportunity_Name__c]; obj.AccountName__c = opp.AccountID;
}

How do I prevent it from running for my new record type? Any help you could give would be greatly appreciated!!
Thanks,
Christine
StephaneTStephaneT
you need a record type query, something like this:

list<recordtype> recordTypes = new list<recordtype>( [select id,developername from recordtype where (developername = 'Record type name') and sobjecttype='Object API name']);

    map<string,Id> recordTypeMap = new map<string,Id>();
    for(RecordType rt : recordTypes)
    {
            recordTypeMap.put(rt.developername, rt.Id);
        }
ChrisColeChrisCole
Thanks very much for your reply! I changed my trigger to this:

trigger MapAccountName on Support_Request__c (before insert) {

list<recordtype> recordTypes = new list<recordtype>( [select id,developername from recordtype where (developername = 'Customer Request') and sobjecttype='Support_Request__c']);

    map<string,Id> recordTypeMap = new map<string,Id>();
    for(RecordType rt : recordTypes)
    {
            recordTypeMap.put(rt.developername, rt.Id);
        }
            
Support_Request__c obj = Trigger.new[0];
Opportunity opp = [select AccountId from Opportunity where Id = : obj.Opportunity_Name__c]; obj.AccountName__c = opp.AccountID;
}

The trigger still works for the other Record Types but I get the following error when I try to create a "Customer Request" Support Request:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MapAccountName caused an unexpected exception, contact your administrator: MapAccountName: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.MapAccountName: line 12, column 1

I guess I was supposed to put some sort of conditional logic around it in the cases where there is no associated opportunity (I've seen the <list> thingy before but don;t really know what it does :) )  I tried a couple different things but to no avail....