You need to sign in to do that
Don't have an account?
Michael Bos 3
MyProfilePageControllerTest - Error in Inbound Change Set
Hi,
Trying to deply a small change set from UAT to Production.
Controller "MyProfilePageControllerTest" works fine in UAT, but gives error in Production.
This controller is not affected in the Change Set.
What is going on in this controller?
What can I change to pass the validation?
Error:
Code:
/**
* An apex class that keeps updates of a portal user in sync with its corresponding contact.
Guest users are never able to access this page.
*/
@IsTest public with sharing class MyProfilePageControllerTest {
@IsTest(SeeAllData=true) static void testSetContactFields() {
User u = [select title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
Contact c = new Contact();
MyProfilePageController.setContactFields(c, u);
System.assertEquals(c.firstname, u.firstname, 'firstname should have been set as the firstname of the user for the contact');
System.assertEquals(c.lastname, u.lastname, 'lastname should have been set as the lastname of the user for the contact');
}
@IsTest(SeeAllData=true) static void testSave() {
// Modify the test to query for a portal user that exists in your org
List<User> existingPortalUsers = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess'];
if (existingPortalUsers.isEmpty()) {
User currentUser = [select id, title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(currentUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
Contact c = new Contact();
MyProfilePageController.setContactFields(c, currentUser);
c.LastName = 'TestContact';
// START Company specific values...
c.Company_Code__c = 'AAA';
String rand = '00000000' + String.valueOf(Math.round(Math.random()*100000000));
c.Personnel_Number__c = rand.subString(rand.length() - 8, rand.length());
// END Company specific values...
insert c;
c.title = currentUser.title;
c.firstname = currentUser.firstname;
c.lastname = currentUser.lastname;
c.email = currentUser.email;
c.phone = currentUser.phone;
c.mobilephone = currentUser.mobilephone;
c.fax = currentUser.fax;
c.mailingstreet = currentUser.street;
c.mailingcity = currentUser.city;
c.mailingstate = currentUser.state;
c.mailingpostalcode = currentUser.postalcode;
c.mailingcountry = currentUser.country;
controller.save();
System.assert(Page.ChangePassword.getUrl().equals(controller.changePassword().getUrl()));
} else {
User existingPortalUser = existingPortalUsers[0];
String randFax = Math.rint(Math.random() * 1000) + '5551234';
System.runAs(existingPortalUser) {
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
controller.getUser().Fax = randFax;
controller.save();
System.assert(controller.getIsEdit() == false);
}
// verify that the user and contact were updated
existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
System.assert(existingPortalUser.fax == randFax);
System.assert(existingPortalUser.Contact.fax == randFax);
}
}
}
Trying to deply a small change set from UAT to Production.
Controller "MyProfilePageControllerTest" works fine in UAT, but gives error in Production.
This controller is not affected in the Change Set.
What is going on in this controller?
What can I change to pass the validation?
Error:
- Class Name - MyProfilePageControllerTest
- Method Name - testSave
- Error message - System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: []
Stack Trace: Class.MyProfilePageControllerTest.testSave: line 40, column 1
Code:
/**
* An apex class that keeps updates of a portal user in sync with its corresponding contact.
Guest users are never able to access this page.
*/
@IsTest public with sharing class MyProfilePageControllerTest {
@IsTest(SeeAllData=true) static void testSetContactFields() {
User u = [select title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
Contact c = new Contact();
MyProfilePageController.setContactFields(c, u);
System.assertEquals(c.firstname, u.firstname, 'firstname should have been set as the firstname of the user for the contact');
System.assertEquals(c.lastname, u.lastname, 'lastname should have been set as the lastname of the user for the contact');
}
@IsTest(SeeAllData=true) static void testSave() {
// Modify the test to query for a portal user that exists in your org
List<User> existingPortalUsers = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess'];
if (existingPortalUsers.isEmpty()) {
User currentUser = [select id, title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(currentUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
Contact c = new Contact();
MyProfilePageController.setContactFields(c, currentUser);
c.LastName = 'TestContact';
// START Company specific values...
c.Company_Code__c = 'AAA';
String rand = '00000000' + String.valueOf(Math.round(Math.random()*100000000));
c.Personnel_Number__c = rand.subString(rand.length() - 8, rand.length());
// END Company specific values...
insert c;
c.title = currentUser.title;
c.firstname = currentUser.firstname;
c.lastname = currentUser.lastname;
c.email = currentUser.email;
c.phone = currentUser.phone;
c.mobilephone = currentUser.mobilephone;
c.fax = currentUser.fax;
c.mailingstreet = currentUser.street;
c.mailingcity = currentUser.city;
c.mailingstate = currentUser.state;
c.mailingpostalcode = currentUser.postalcode;
c.mailingcountry = currentUser.country;
controller.save();
System.assert(Page.ChangePassword.getUrl().equals(controller.changePassword().getUrl()));
} else {
User existingPortalUser = existingPortalUsers[0];
String randFax = Math.rint(Math.random() * 1000) + '5551234';
System.runAs(existingPortalUser) {
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
controller.getUser().Fax = randFax;
controller.save();
System.assert(controller.getIsEdit() == false);
}
// verify that the user and contact were updated
existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
System.assert(existingPortalUser.fax == randFax);
System.assert(existingPortalUser.Contact.fax == randFax);
}
}
}
Please Check for the duplicate rules:
reference
https://docs.copado.com/article/5o1neaowtv-data-deployment-error-duplicates-detected
https://docs.gearset.com/en/articles/4923465-resolving-data-deployment-errors-duplicates_detected
If it helps please mark it as best answer.
Thanks!
All Answers
Please Check for the duplicate rules:
reference
https://docs.copado.com/article/5o1neaowtv-data-deployment-error-duplicates-detected
https://docs.gearset.com/en/articles/4923465-resolving-data-deployment-errors-duplicates_detected
If it helps please mark it as best answer.
Thanks!
Thanks for your reply.
Solution was to temporarily disable the duplicate rule as found in the documented help.
Setup > Duplicate Management > Duplicate Rules > disable contact rule
Can you explain to me what happens here?
Does it create a new contact? What has the current user to do with it?
Does it try to create a contact, with a random personnel number?
If the personnel number already exists, it gives the duplicate error?
Contact c = new Contact();
MyProfilePageController.setContactFields(c, currentUser);
c.LastName = 'TestContact';
// START Company specific values...
c.Company_Code__c = 'AAA';
String rand = '00000000' + String.valueOf(Math.round(Math.random()*100000000));
c.Personnel_Number__c = rand.subString(rand.length() - 8, rand.length());
// END Company specific values...
insert c;
Yes its creating test data for contact,but record thats created in testMethod does not gets saved to database.
Regarding user role You have to check MyProfilePageController class setContactFields method how its implemented.
As its uses Math.random() ,so every time its run it create different number.
You can check this by executing this from anonymous window
Regarding duplicate error you have to check how your duplicate rule is defined.
Thanks!