4

I have a trigger in place that runs for every order record created. I need this trigger to only run when for specific record types. I am not sure if by my trigger and test class needs to be updated of is only the trigger needs to be updated to reflect this change

Test Class:

@isTest
public class Test_orderAfterInsert {
public static testmethod void test_method1(){
    Account a  = new Account();
    a.name = 'test';

    insert a;

    case c = new case();
    c.AccountId = a.Id;
    c.origin = 'Phone';

    insert c;

    Orders__c ord = new Orders__c();
    ord.Related_Account__c = a.Id;
    ord.Related_Case__c = c.Id;
    //Orders__c or = new Orders__c();
    //or.Account__c = a.Id;

    insert ord;


}

Apex Trigger:

trigger orderAfterInsert on Orders__c (after insert) {

string caseId;
list<Case> lstToUpdateCase = new list<Case>();

//Assigning caseid in variable 
for(Orders__c ord : Trigger.New)
{
    caseId = ord.Related_Case__c;
}
//updating case record with the caseid
case c = new case(id = caseId);
c.Order_Created__c = true;
//adding order created into list 
lstToUpdateCase.add(c);

//updating the record through DML operation
if(lstToUpdateCase.Size()>0)
{
    update lstToUpdateCase;
}
}
  • 1
    Hi @Cheryl815, please rephrase the title of your post as a specific question, such a "How to run a trigger for specific record types?" This makes it easier for other users to understand your objective and find this post when they might ask the same question. Thanks! – Doug Ayers Sep 7 '14 at 2:58
6

I need this trigger to only run when for specific record types.

Your orderAfterInsert trigger will run every time an order record is inserted. What you need is to write your trigger such that it qualifies order records in Trigger.new first.

trigger orderAfterInsert on Orders__c (after insert) {

    // Qualify records
    List<Orders__c> qualifiedOrders = new List<Orders__c>();
    for (Orders__c order : Trigger.new) {
        if (order.recordTypeId == '<INSERT DESIRED RECORD TYPE ID(s) HERE>') {
            qualifiedOrders.add(order); 
        }   
    }

    // Now do what you want with the qualified records.
    if (qualifiedRecords.isEmpty() == false) {
        // TODO: Collect case ids from the list of qualified orders.
        // TODO: Set Order_Created__c field of associated cases, and compile in a list of cases.
        // TODO: Perform update of on the list of cases.    
    }
}

Another thing I want to address is your for loop where you're setting caseId:

string caseId;

//Assigning caseid in variable 
for(Orders__c ord : Trigger.New)
{
    caseId = ord.Related_Case__c;
}

You'll get unexpected results if Trigger.new returns more than one record. Consider gathering your caseIds in a list.

List<Id> caseIds = new List<Id>();
for (Order__c order : qualifiedRecords){
    caseIds.add(order.Related_Case__c);
}
| improve this answer | |
  • 1
    You really should not hard code IDs into your code. See salesforce.stackexchange.com/questions/11968/… – eyedar May 11 '15 at 18:27
  • Your first code block isn't working for me. Everytime I insert in 1 record in 1 of the record types .. it will create records for ALL record types still... – compski Oct 10 '18 at 4:38
2

Your trigger definitely needs to be updated and your testmethod should also be updated to reflect the change.

The code would be something like this:

trigger orderAfterInsert on Orders__c(after insert) {

string caseId;
Set<ID> RTIds = new Set<ID>();  //set of Record Type IDs to match
list < Case > lstToUpdateCase = new list < Case > ();

for(RecordType rt : [select id from Recordtype where SobjectType='Orders__c' and Name in ('test','foo','bar')]){
    RTIds.add(rt.id);
}
//Assigning caseid in variable 
for (Orders__c ord: Trigger.New) {
    if(if RTIds.contains(ord.Recordtypeid) ){
        caseId = ord.Related_Case__c;
    }
}
//updating case record with the caseid
case c = new
case (id = caseId);
c.Order_Created__c = true;
//adding order created into list 
lstToUpdateCase.add(c);

//updating the record through DML operation
if (lstToUpdateCase.Size() > 0) {
    update lstToUpdateCase;
}
}
| improve this answer | |
  • 2
    Describes are free now, so you could update this answer to reflect that. – sfdcfox Sep 7 '14 at 5:08

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.