You need to sign in to do that
Don't have an account?
Steve Connelly 5
How do I limit my trigger to specific record types and field values?
I have a new trigger that creates a custom object record when an opportunity is created or updated.
Everything works but I need help with one thing. I only want it to run when a specific opportunity type is created or when that type has its "cloe date" field updated.
Here is my code:
How do I limit this so it only runs when a "Sales" opp is created or when an existing Sales Opp has the "Close Date" field updated?
New to coding and apex so any help is greatly appreciated.
Best regards,
Steve
Everything works but I need help with one thing. I only want it to run when a specific opportunity type is created or when that type has its "cloe date" field updated.
Here is my code:
trigger createOq on Opportunity (after insert, after update) { List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c> (); for(Opportunity opportunityList : trigger.New){ Open_Quarter__c oq = new Open_Quarter__C(); oq.Amount_Per_Quarter_oq__c = opportunityList.Amount_per_Quarter_op__c; oq.Close_Date_oq__c = opportunityList.CloseDate; oq.Name = opportunityList.Quarter_Created_op__c; oq.Opportunity_Name_oq__c = opportunityList.Id; oq.Quarters_Spanned_oq__c = opportunityList.Quarters_Spanned_op__c; createOpenQuarter.add(oq); } try { insert createOpenQuarter; } catch (system.Dmlexception e) { system.debug (e); } }
How do I limit this so it only runs when a "Sales" opp is created or when an existing Sales Opp has the "Close Date" field updated?
New to coding and apex so any help is greatly appreciated.
Best regards,
Steve
One more correction here, we want that try catch logic within the conditional as well.
All Answers
Hi Steve,
You can try adding a conditional right after starting the Trigger.new for loop you have. We can us Trigger.isInsert(), Trigger.isUpdate(), and Trigger.oldMap to set the criteria you are looking for.
Here's an example:
trigger IntakeFormTrigger on Apttus__APTS_Agreement__c (before insert, before update) { Map<String, Id> typeMap = New Map<String, Id>(); for(RecordType rt: [Select DeveloperName, Id From Record
Type Where sObjectType = 'Apttus__APTS_Agreement__c'])
{ typeMap.put(rt.DeveloperName, rt.Id);
}
for (Apttus__APTS_Agreement__c agmt : trigger.new) {
recordtype agmtRT = [select id, developername from recordtype where id=:agmt.
recordtypeid]; recordtype IFRT = [select id, developername from recordtype where developername = 'Intake_Form']; \
// If the Record Type = Intake Form
if (agmtRT.id == IFRT.id) { // And the Agreement Category on the record = Services if (agmt.Apttus__Agreement_Category__c == 'Services') {
// Then automatically change the Record Type to Services Agreement.
id recid = typeMap.get('Services_Agreement');
recordtype rectype = [select id, developername from recordtype where id=:recid];
agmt.RecordTypeid = rectype.id;
}
}
}
}
_________________________________________________________________________________________
https://www.prepaidcardstatus.net/
I inserted your changes and I get a couple of errorsd I have not sorted out.
Here is the code:
I get these errors:
Do I need to call this trigger elsewhere? It is interesting that it does not also error out on line 10.
Sc
Sc
Hi Steve, I believe isUpdate and isInsert are actually variables and not methods on Trigger, so the () are likely causing the error. So how about we try removing those parentheses. So it will look like the following:
So, the problem is that while this does limit the trigger to Sales Opps for new opps.
It does not limit the trigger to Close Date updates only for Sales Opps.
That was probably my fault for not clarifying.
I need this to
- Only execute for new "Sales" Opps (accomplished)
- Only execute for existing Sales Opps when the close date field is updated
Right now it runs when any opp has the Close Date field updated.Is there a way to limit that too?
Sc
Hi Steve, yea absolutely. In that case, we just need to update the conditional slightly as such:
One more correction here, we want that try catch logic within the conditional as well.
Sc
to this: (removing this: (Trigger.isInsert ||)
It does limit the change to Close Field on the correct record type only.
I just don't know where that leaves me with only firing on the correct record type when new. It appears that the issue is with the "OR" maybe.
Sc
It seems to work now so I think we got there together.
Marking yuou as Best Answer.
Thanks so much for all the help!
Steve
Hi Steve, that's great! You can also clean it up just a little by adding an "outer" conditional to first check for the Record type, and then proceeding to an "inner" conditional. This would avoid the repeating " RecordTypeId = '01241000001USq9AAG' "
Obviously, if you have it working then I wouldn't want to mess anything thing up. But you could always give it a shot:
Anyway, glad we could work it out. All the best
I feel like I learned something today.
Not bad for a Monday.
Sc