You need to sign in to do that
Don't have an account?
Pankaj Vithal 13
Copy a trigger from contacts object and replicate it on Leads object - need developer help
Guys, I am an Admin who needs help from a developer. I have an apex trigger that was written and implemented by a contractor. It is on Contacts object. What is does is assign the contact to an owner based on the first three digits of the postal code. I need to have the same assignment triggger on Leads object.
Updated Test:
All Answers
I once tried to write an apex trigger not allowing a particular user to post on chatter feed following instruction from helpful people like you just like an amateur. i was able to make that code work on sandbox but when i moved to production, it gave errors like not enough code coverage and missing test class at which point i just gave up. I though i need to start learning this developer thing from beginning to deal with it.
so, that are the kind of things I'd need some help with to successfully implement this code.
CODE:
Purpose: This trigger changes ownership of contact as per mapping defined in user territory mapping object.
If a contact is being created and [override contact owner] is unchecked, then change contact owner to user as defined in territory mapping object.
If a contac tis beign created or updated and is set to active/inactive then append/remove "_do not use" text from contact name.
Original Date: Mar 24, 2017
*/
trigger ContactUpdateOwner on Contact (before insert, before update) {
if(Utils.firstRun || Test.isRunningTest()){
Utils.firstRun=false;
set<string> setPC= new set<string>();
if(Trigger.isBefore && Trigger.isInsert){
for(Contact c : Trigger.New){
if(c.MailingPostalCode != null && !c.Override_Owner_Rules__c){
setPC.Add(c.MailingPostalCode.left(3).trim());
}
// If contact status is being set then change contact name as well.
if(c.Status__c != null ){
if(c.Status__c=='Inactive'){
c.LastName=c.LastName + '_DoNotUse';
} else if(c.Status__c=='Active') {
if(c.LastName.contains('_DoNotUse')){
c.LastName=c.LastName.remove('_DoNotUse');
}
}
}
}
}
if(Trigger.isBefore && Trigger.isUpdate){
for(Contact c : Trigger.New){
// If a contacts postal code was changed and not set to override rules
if(c.MailingPostalCode != null && Trigger.OldMap.get(c.id).MailingPostalCode != c.MailingPostalCode && !c.Override_Owner_Rules__c){
setPC.Add(c.MailingPostalCode.left(3).trim());
}
// If contact status is being changed then change contact name as well.
if(c.Status__c != null && Trigger.OldMap.get(c.id).Status__c != c.Status__c){
if(c.Status__c=='Inactive'){
c.LastName=c.LastName + '_DoNotUse';
} else if(c.Status__c=='Active') {
if(c.LastName.contains('_DoNotUse')){
c.LastName=c.LastName.remove('_DoNotUse');
}
}
}
}
}
if(setPC.size()>0){
List<contact> lstCon= new List<contact>();
Map<string, Id> mapPCOwnerId = Utils.GetPCOwnerIdMap(setPC);
if(Trigger.isBefore && Trigger.isInsert){
for(Contact c : Trigger.New){
if(c.MailingPostalCode != null && !c.Override_Owner_Rules__c){
if(mapPCOwnerId.containsKey(c.MailingPostalCode.left(3).trim())){
c.OwnerId=mapPCOwnerId.get(c.MailingPostalCode.left(3).trim());
lstCon.Add(c);
}
}
}
}
if(Trigger.isBefore && Trigger.isUpdate){
for(Contact c : Trigger.New){
if(c.MailingPostalCode != null && Trigger.OldMap.get(c.id).MailingPostalCode != c.MailingPostalCode && !c.Override_Owner_Rules__c){
if(mapPCOwnerId.containsKey(c.MailingPostalCode.left(3).trim())){
c.OwnerId=mapPCOwnerId.get(c.MailingPostalCode.left(3).trim());
lstCon.Add(c);
}
}
}
}
}
}
}
That's what the community is here for!
The process to develop Apex is create it in the sandbox, get it all working, write a test, and deploy to Production. The tests is where you got stuck. See if you can find the test for the code you posted. As a developer I always prefix my tests with Test_, see if your contractor did the same.
I'll update this code for you this afternoon to reflect Lead instead of Contact.
ALSO, there is custom field (Override_Owner_Rules__c) in your Contact object which may not be in the Lead. Could you see if that field exists in the Lead? Do you use the '_DoNotUse' append on the Lead?
2. _doNotUse part we are not going to use on leads so we can delete off that part from the code
I kinda looked up and this is what i found, not sure if this is test class:
public class Utils{
public static boolean firstRun = true;
public static Map<string, Id> GetPCOwnerIdMap(set<string> setPC){
Map<string, Id> mapRet= new Map<string, Id>();
for(User_Territory_Map__c um : [Select POSTAL_CODE__c, Assigned_User__c from User_Territory_Map__c Where POSTAL_CODE__c in :setPC]){
mapRet.put(um.POSTAL_CODE__c.left(3).trim(), um.Assigned_User__c);
}
return mapRet;
}
}
/**
* Test class for class Utils
*/
@IsTest public class UtilsTest {
@IsTest public static void testGetPCOwnerIdMap() {
set<string> setPC=new set<string>();
setPC.Add('M4X');
User_Territory_Map__c um= new User_Territory_Map__c(POSTAL_CODE__c='M4X', Assigned_User__c=UserInfo.GetUserId());
insert um;
Utils.GetPCOwnerIdMap(setPC);
}
}
The test you found is for the Utils class, no need to update it
See if there is another test for the ContactUpdateOwner
Let me know if you need help implementing the code!
@isTest(SeeAllData=true)
public class ContactUpdateOwnerTest{
static testMethod void testContactUpdateOwner() {
set<string> setPC=new set<string>();
setPC.Add('M4X');
User_Territory_Map__c um= new User_Territory_Map__c(POSTAL_CODE__c='M4X', Assigned_User__c=UserInfo.GetUserId());
insert um;
Account[] acc = new Account[] {
new Account(Name = 'Test Account1')
};
insert acc;
Contact[] con = new Contact[] {
new Contact(LastName = 'Test contact1_DoNotUse',AccountId = acc[0].Id, Status__c='Inactive',
MailingStreet='100 King St W', MailingCity='Toronto', MailingState='ON', MailingPostalCode='M4X 1A1', MailingCountry='Canada'),
new Contact(LastName = 'Test contact1_DoNotUse',AccountId = acc[0].Id, Status__c='Active',
MailingStreet='100 King St W', MailingCity='Toronto', MailingState='ON', MailingPostalCode='M4X 1A1', MailingCountry='Canada')
};
insert con;
con[0].Status__c='Active'; con[0].MailingPostalCode='M4X 1A3';
con[1].MailingPostalCode='M4X 1A2'; con[1].Status__c='Inactive';
update con;
//List<Contact> lstC=new List<([Select Id, Status__c,MailingPostalCode from Contact])>;
//lstC[0].Status__c='Active';
//lstC[1].MailingPostalCode='M4X 1A2'; lstC[1].Status__c='Inactive';
}
}
Whn deploying code to Production you'll need to include the specific tests written for the code. The test you just posted above will be altered to complete the test. I'll post it when updated and give you some help deploying.
Thanks,
Ryan
Updated Test:
Thanks!! I followed your instructions and was able to deploy the code in prod and IT IS WORKING!! my last question to you is: my boss doesn't want me to activate it yet but after a week. so where can I find the de-activate switch?
P.S. I just deployed my first code. HAPPY!!! :)
To deactivate you'll basically need to delete it from production. Easiest way is locate the trigger in the sandbox, deactivate it there, then deploy the code to production. It's been a while since I deactivated apex so I don't remember if you need to deploy the test with it. You could try it both ways and see what happens.
Happy developing!