You need to sign in to do that
Don't have an account?
Jyosi jyosi
To Compare the old and New values in Apex class
Hello Every one,
I need to compare the old and new values in apex class.It works fine if i write the code in trigger.But i need this validation to be incroporated in handler class
Below is my trigger and Apex class
if(Trigger.Isupdate && Trigger.Isbefore)
{
for(Contact Contacts : trigger.new)
{
if((Contacts.Secure_Start_Owner__c != Trigger.oldMap.get(Contacts.Id).Secure_Start_Owner__c) || (Trigger.oldMap.get(Contacts.Id).Secure_Start_Owner__c==null))
{
AssignSecureStartOwnerHandler.assignSecureStartOwneronUpdate(Trigger.newMap,trigger.new);
}
}
}
Method Name
public void assignSecureStartOwneronUpdate(Map<Id, contact> leadMap, List<contact> Listlead){
List<Contact> updateLeads = new List<Contact>();
List<Contact> updateContacts = new List<Contact>();
List<Account> updateAccounts = new List<Account>();
List<User> updateUsers = new List<User>();
List<Id> LeadIds = new List<Id>();
Boolean cc = false;
List<Id> userIds = new List<Id>();
Id AccountId;
integer last;
List<Account> accountIds;
Set<Id> Nurse_Clinician = new set<id>();
for(Contact lead:Listlead)
{
Nurse_Clinician.add(lead.Nurse_Clinician__c);
}
List<Contact> thisClinician = [select AccountId, Account1__c, Id from Contact where Id IN:Nurse_Clinician];
System.debug('thisClinician@@@'+thisClinician);
for(Contact leads:Listlead)
{
if(leads.Secure_Start_Owner__c==null)
{
system.debug('SecureStartOwner' +lead.Secure_Start_Owner__c);
final String conrecname = '(US) Patient/Consumer';
RecordType conr = [Select Id,Name From RecordType where Name = :(conrecname)];
System.debug('conr Id = '+conr.Id);
if(thisClinician!=null && thisClinician.size()>0)
{
System.debug('acc on nurse = '+thisClinician[0].Account1__c);
AccountId = thisClinician[0].Account1__c;
}
if(leads.Place_of_Surgery_Hospital__c==null)
{
leads.Place_of_Surgery_Hospital__c = AccountId;
updateLeads.add(leads);
}
}
if(AccountId !=null){
System.debug('accountid on contact = '+AccountId);
accountIds = [select Id, Last_Round_Robin_ID__c from Account where Id=:AccountId limit 1];
system.debug('accountIds@@@@'+accountIds);
if(accountIds!=null){
if(accountIds[0].Last_Round_Robin_ID__c==null)
{
last = 0;
}
else{
last = Integer.valueOf(accountIds[0].Last_Round_Robin_ID__c);
}
List<AccountTeamMember> atm = [Select UserId, TeamMemberRole, Id, AccountId From AccountTeamMember where AccountID=:accountIds[0].Id AND (TeamMemberRole = 'US Secure Start Patient Coord - US OST 1'OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 2'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 3'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 4'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 5')
];
System.debug('atm = '+atm);
if(atm!=null && atm.size()>0){
for(AccountTeamMember a:atm){
System.debug('atm.UserId = '+a.UserId);
userIds.add(a.UserId); //all these users can be the secure start owner for this Lead
}
}
else{
//AccountTeam is not set then assign Secure Start ownership to the logged In user
Id loggedInUserId = UserInfo.getUserId();
userIds.add(loggedInUserId);
}
if(userIds!=null && userIds.size()>0){
//get the Round Robin ID from the user records
List<User> users = [select Name, Round_Robin_ID__c, Last_Round_Robin_Assigned_At__c,Not_Assigned_SS_Owner_Recently__c, Id from User
where Id IN:userIds ORDER BY Last_Round_Robin_Assigned_At__c ASC,Round_Robin_ID__c ASC];
System.debug('users = '+users);
for(contact lead:Listlead){
if(last==0){
/* never started assignment on this team - pick the first user from users and update the Last_Round_Robin_ID__c on contact */
integer assigned;
//
for(integer i=0;i<users.size();i++){
if(users[i].Not_Assigned_SS_Owner_Recently__c==true){
lead.Secure_Start_Owner__c = users[i].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[i].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[i].Not_Assigned_SS_Owner_Recently__c = false;
users[i].Last_Round_Robin_Assigned_At__c = System.now();
assigned = i;
updateLeads.add(lead);
updateUsers.add(users[i]);
system.debug('assigned ! :'+assigned);
break;
}
if(assigned==null ||assigned==0){
System.debug('no user had flag checked');
//reset and pick first user // sort Users on the Last_Round_Robin_Assigned_At__c and pick first user
lead.Secure_Start_Owner__c = users[0].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[0].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[0].Not_Assigned_SS_Owner_Recently__c = false;
users[0].Last_Round_Robin_Assigned_At__c = System.now();
assigned = 0;
updateLeads.add(lead);
updateUsers.add(users[0]);
}
}
integer counter = -1;
for(User u:users){
counter++;
if(counter!=assigned){
u.Not_Assigned_SS_Owner_Recently__c = true;
updateUsers.add(u);
}
}
}
else{
lead.Secure_Start_Owner__c = users[0].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[0].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[0].Not_Assigned_SS_Owner_Recently__c = false;
users[0].Last_Round_Robin_Assigned_At__c = System.now();
System.debug('adding = '+users[0].Name);
updateUsers.add(users[0]);
updateLeads.add(lead);
integer index = 0;
integer endIndex = users.size()-1;
System.debug('index = '+index+' till endIndex = '+endIndex);
while(endIndex>index){
users[endIndex].Not_Assigned_SS_Owner_Recently__c = true;
System.debug('adding = '+users[endIndex].Name);
updateUsers.add(users[endIndex]);
endIndex--;
}
}
}
}
}
}
if(updateAccounts!=null && updateAccounts.size()>0){
System.debug('updateAccounts = '+updateAccounts[0]);
update updateAccounts;
}
if(updateUsers!=null && updateUsers.size()>0){
update updateUsers;
}
}
// return null;
}
Thanks for help.
Regards,
jyo
I need to compare the old and new values in apex class.It works fine if i write the code in trigger.But i need this validation to be incroporated in handler class
Below is my trigger and Apex class
if(Trigger.Isupdate && Trigger.Isbefore)
{
for(Contact Contacts : trigger.new)
{
if((Contacts.Secure_Start_Owner__c != Trigger.oldMap.get(Contacts.Id).Secure_Start_Owner__c) || (Trigger.oldMap.get(Contacts.Id).Secure_Start_Owner__c==null))
{
AssignSecureStartOwnerHandler.assignSecureStartOwneronUpdate(Trigger.newMap,trigger.new);
}
}
}
Method Name
public void assignSecureStartOwneronUpdate(Map<Id, contact> leadMap, List<contact> Listlead){
List<Contact> updateLeads = new List<Contact>();
List<Contact> updateContacts = new List<Contact>();
List<Account> updateAccounts = new List<Account>();
List<User> updateUsers = new List<User>();
List<Id> LeadIds = new List<Id>();
Boolean cc = false;
List<Id> userIds = new List<Id>();
Id AccountId;
integer last;
List<Account> accountIds;
Set<Id> Nurse_Clinician = new set<id>();
for(Contact lead:Listlead)
{
Nurse_Clinician.add(lead.Nurse_Clinician__c);
}
List<Contact> thisClinician = [select AccountId, Account1__c, Id from Contact where Id IN:Nurse_Clinician];
System.debug('thisClinician@@@'+thisClinician);
for(Contact leads:Listlead)
{
if(leads.Secure_Start_Owner__c==null)
{
system.debug('SecureStartOwner' +lead.Secure_Start_Owner__c);
final String conrecname = '(US) Patient/Consumer';
RecordType conr = [Select Id,Name From RecordType where Name = :(conrecname)];
System.debug('conr Id = '+conr.Id);
if(thisClinician!=null && thisClinician.size()>0)
{
System.debug('acc on nurse = '+thisClinician[0].Account1__c);
AccountId = thisClinician[0].Account1__c;
}
if(leads.Place_of_Surgery_Hospital__c==null)
{
leads.Place_of_Surgery_Hospital__c = AccountId;
updateLeads.add(leads);
}
}
if(AccountId !=null){
System.debug('accountid on contact = '+AccountId);
accountIds = [select Id, Last_Round_Robin_ID__c from Account where Id=:AccountId limit 1];
system.debug('accountIds@@@@'+accountIds);
if(accountIds!=null){
if(accountIds[0].Last_Round_Robin_ID__c==null)
{
last = 0;
}
else{
last = Integer.valueOf(accountIds[0].Last_Round_Robin_ID__c);
}
List<AccountTeamMember> atm = [Select UserId, TeamMemberRole, Id, AccountId From AccountTeamMember where AccountID=:accountIds[0].Id AND (TeamMemberRole = 'US Secure Start Patient Coord - US OST 1'OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 2'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 3'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 4'
OR TeamMemberRole = 'US Secure Start Patient Coord - US OST 5')
];
System.debug('atm = '+atm);
if(atm!=null && atm.size()>0){
for(AccountTeamMember a:atm){
System.debug('atm.UserId = '+a.UserId);
userIds.add(a.UserId); //all these users can be the secure start owner for this Lead
}
}
else{
//AccountTeam is not set then assign Secure Start ownership to the logged In user
Id loggedInUserId = UserInfo.getUserId();
userIds.add(loggedInUserId);
}
if(userIds!=null && userIds.size()>0){
//get the Round Robin ID from the user records
List<User> users = [select Name, Round_Robin_ID__c, Last_Round_Robin_Assigned_At__c,Not_Assigned_SS_Owner_Recently__c, Id from User
where Id IN:userIds ORDER BY Last_Round_Robin_Assigned_At__c ASC,Round_Robin_ID__c ASC];
System.debug('users = '+users);
for(contact lead:Listlead){
if(last==0){
/* never started assignment on this team - pick the first user from users and update the Last_Round_Robin_ID__c on contact */
integer assigned;
//
for(integer i=0;i<users.size();i++){
if(users[i].Not_Assigned_SS_Owner_Recently__c==true){
lead.Secure_Start_Owner__c = users[i].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[i].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[i].Not_Assigned_SS_Owner_Recently__c = false;
users[i].Last_Round_Robin_Assigned_At__c = System.now();
assigned = i;
updateLeads.add(lead);
updateUsers.add(users[i]);
system.debug('assigned ! :'+assigned);
break;
}
if(assigned==null ||assigned==0){
System.debug('no user had flag checked');
//reset and pick first user // sort Users on the Last_Round_Robin_Assigned_At__c and pick first user
lead.Secure_Start_Owner__c = users[0].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[0].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[0].Not_Assigned_SS_Owner_Recently__c = false;
users[0].Last_Round_Robin_Assigned_At__c = System.now();
assigned = 0;
updateLeads.add(lead);
updateUsers.add(users[0]);
}
}
integer counter = -1;
for(User u:users){
counter++;
if(counter!=assigned){
u.Not_Assigned_SS_Owner_Recently__c = true;
updateUsers.add(u);
}
}
}
else{
lead.Secure_Start_Owner__c = users[0].Id;
if(cc==false){
accountIds[0].Last_Round_Robin_ID__c = Integer.valueOf(users[0].Round_Robin_ID__c);
updateAccounts.add(accountIds[0]);
}
users[0].Not_Assigned_SS_Owner_Recently__c = false;
users[0].Last_Round_Robin_Assigned_At__c = System.now();
System.debug('adding = '+users[0].Name);
updateUsers.add(users[0]);
updateLeads.add(lead);
integer index = 0;
integer endIndex = users.size()-1;
System.debug('index = '+index+' till endIndex = '+endIndex);
while(endIndex>index){
users[endIndex].Not_Assigned_SS_Owner_Recently__c = true;
System.debug('adding = '+users[endIndex].Name);
updateUsers.add(users[endIndex]);
endIndex--;
}
}
}
}
}
}
if(updateAccounts!=null && updateAccounts.size()>0){
System.debug('updateAccounts = '+updateAccounts[0]);
update updateAccounts;
}
if(updateUsers!=null && updateUsers.size()>0){
update updateUsers;
}
}
// return null;
}
Thanks for help.
Regards,
jyo
Kannan N 10
Jyo: Can you elaborate your business case for doing this? Can you use Formula / Validation Rule {Priorvalue} and shift the functionality in configuration?