You need to sign in to do that
Don't have an account?
Help with trigger - don't know where to add $RecordType.Name = "License Opportunity"
I have this Apex Trigger which works good. However I want it to only be valid for license opportunities (all other record types should be able to close deal without having to add Delivery Contact), so I want to add $RecordType.Name = "License Opportunity" somewhere, but don't know where or how.
Anyone who has a solution for this? :)
This is the trigger:
trigger Opportunity_DeliveryContact_Required on Opportunity (before insert, before update) {
//map to keep track of the contact_required = 1
Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();
//Trigger.new is an array of opportunities
//and adds any that have Contact_Required = 1 to the oppy_contact Map
for (Integer i = 0; i < Trigger.new.size(); i++) {
System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
if (Trigger.new[i].contact_required__c == 1) {
oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
}
}
//map to keep track of the opportunity contact roles
map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();
//select OpportunityContactRoles for the opportunities with contact role required
List<OpportunityContactRole> roles = [select OpportunityId, Role from OpportunityContactRole
where (OpportunityContactRole.Role ='Delivery Contact') and OpportunityContactRole.OpportunityId
in :oppy_contact.keySet()];
for (OpportunityContactRole ocr : roles) {
//puts the contact roles in the map with the Opportunity ID as the key
oppycontactroles.put(ocr.OpportunityId,ocr);
}
// Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required
for (Opportunity oppy : system.trigger.new) {
//system.debug('List oppy Id - '+oppy.id);
if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
{
oppy.addError('No Delivery Contact exists. Please go to the Contact Roles area and select a delivery contact before you can move on to stage "A6 - Closed Won".');
}
} //for
}
Thanks dphill,
I guess this would work with out any issues.
All Answers
try this.
Thanks dphill,
I guess this would work with out any issues.
Thanks guys! Works perfectly :D
Would it be possible for you to also help me with my test class that is failing? It used to have 4/4 test methods passed, but after my change it only has 2 passed . Even before my change where I had 4/4 test methods passed, it still had 0% code coverage. I have no idea how to fix it so that I can deploy this to my production environment.
Failures:
Method Name testoppyrequired1wodeliverycontact
Pass/Fail Fail
Error Message System.AssertException: Assertion Failed
Stack Trace Class.test_Oppty_DeliveryContact_Required.testoppyrequired1wodeliverycontact: line 124, column 1
Method Name testoppyrequired1
Pass/Fail Fail
Error Message System.AssertException: Assertion Failed
Stack Trace Class.test_Oppty_DeliveryContact_Required.testoppyrequired1: line 62, column 1
My test class code:
I guess you should try creating opportunities with the recordTypeId.
Let me know if it doesnt work.
But how? I am not a developer... yet ;) , so nothing of this makes much sense yet. Could you explain it "for dummies" style? Where in the code should what be added? As you did in your previous post. Even I understood that :)
Same way you set the other fields, except specify what the RecordTypeId is instead. Look at how you have Role='Delivery Contact'. You need to do pretty much the same thing for RecordTypeId and assign it the Id that you queried for to fix your earlier issue.
Ah, I see, thanks! I got all 4 test methods to pass again. However, I still have 0% code coverage. How do I solve that?
No it did not work :( It's still 0%, and when I click the link nothing is blue. The code covered should be blue right?
Go to the trigger, and is the Status marked as Active?
Save the test class what you have right now and can you try with this test class.
Actually, seemed to be some kind of delay. I now have 92% code coverage and are able to deploy. Woohoo!
Thank you so much for your help! I really appreciate you taking the time to help and teach me :)
Take care!