You need to sign in to do that
Don't have an account?
SFDC n12
Trigger combining
Hi,
I need help to combing my 2 trigger to the first one
Not sure how to combine both
MY FIRST TRIGGER :
trigger AF_Approval on AccountExceptions__c (before insert) {
List<Id> accId = new List<Id>();
for(AccountExceptions__c spl:trigger.new)
{
accId.add(spl.Account__c);
}
// Soql Account Team member to get the User ID
List<AccountTeamMember> RegularDOS = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> growthDos = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Growth DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> RVPUsers = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance RVP' AND AccountId =:accId LIMIT 1];
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account'];
//Id profileId=userinfo.getProfileId();
User u = AF_DealerCRM_Utility.getUserInfo();
String profileName= u.Profile.Name;
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c myobj : trigger.new)
{
if(myobj.Account__r.RecordTypeId != rtypes.Id) {
if(profileName=='AF: Sales Specialist' && growthDos.size() > 0){
myobj.DOSUser__c= growthDos[0].UserId;
}
else if(profileName=='AF: Sales Specialist' && growthDos==null && RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
else if( RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
if(RVPUsers.size() > 0){
myobj.RVP_Approver__c= RVPUsers[0].UserId ;
}
}
if(myobj.Total_Amount_of_Current_Exception__c>20000)
{
myobj.Executive_Approval_Reasons__c='Exception Value > $20K';
}
}
}
My Second Trigger that i want to combine to first trigger :
trigger AF_RVP on AccountExceptions__c (after insert) {
List<Id> rvpId = new List<Id>();
Set<Id> adrId = new Set<Id>();
for(AccountExceptions__c spl:trigger.new)
{
rvpId.add(spl.DOSUser__c);
rvpId.add(spl.Account__c);
adrId.add(spl.Id);
}
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account' limit 1];
User RVPUsers =[SELECT Name, ManagerID, Manager.Name FROM User WHERE Id = :rvpId LIMIT 1];
List<AccountExceptions__c> adrExpList = [Select Id,RVP_Approver__c,Account__c,Account__r.RecordTypeId From AccountExceptions__c Where Id IN :adrId];
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c obj : adrExpList)
{
if(obj.Account__r.RecordTypeId == rtypes.Id) {
obj.RVP_Approver__c= RVPUsers.ManagerID;
}
}
update adrExpList;
}
help me how to combine both of them please
I need help to combing my 2 trigger to the first one
Not sure how to combine both
MY FIRST TRIGGER :
trigger AF_Approval on AccountExceptions__c (before insert) {
List<Id> accId = new List<Id>();
for(AccountExceptions__c spl:trigger.new)
{
accId.add(spl.Account__c);
}
// Soql Account Team member to get the User ID
List<AccountTeamMember> RegularDOS = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> growthDos = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Growth DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> RVPUsers = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance RVP' AND AccountId =:accId LIMIT 1];
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account'];
//Id profileId=userinfo.getProfileId();
User u = AF_DealerCRM_Utility.getUserInfo();
String profileName= u.Profile.Name;
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c myobj : trigger.new)
{
if(myobj.Account__r.RecordTypeId != rtypes.Id) {
if(profileName=='AF: Sales Specialist' && growthDos.size() > 0){
myobj.DOSUser__c= growthDos[0].UserId;
}
else if(profileName=='AF: Sales Specialist' && growthDos==null && RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
else if( RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
if(RVPUsers.size() > 0){
myobj.RVP_Approver__c= RVPUsers[0].UserId ;
}
}
if(myobj.Total_Amount_of_Current_Exception__c>20000)
{
myobj.Executive_Approval_Reasons__c='Exception Value > $20K';
}
}
}
My Second Trigger that i want to combine to first trigger :
trigger AF_RVP on AccountExceptions__c (after insert) {
List<Id> rvpId = new List<Id>();
Set<Id> adrId = new Set<Id>();
for(AccountExceptions__c spl:trigger.new)
{
rvpId.add(spl.DOSUser__c);
rvpId.add(spl.Account__c);
adrId.add(spl.Id);
}
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account' limit 1];
User RVPUsers =[SELECT Name, ManagerID, Manager.Name FROM User WHERE Id = :rvpId LIMIT 1];
List<AccountExceptions__c> adrExpList = [Select Id,RVP_Approver__c,Account__c,Account__r.RecordTypeId From AccountExceptions__c Where Id IN :adrId];
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c obj : adrExpList)
{
if(obj.Account__r.RecordTypeId == rtypes.Id) {
obj.RVP_Approver__c= RVPUsers.ManagerID;
}
}
update adrExpList;
}
help me how to combine both of them please
try this:
Best,
Andreas
All Answers
try this:
Best,
Andreas
trigger AF_Approval on AccountExceptions__c (before insert , after insert )
{
if (trigger.isInsert && trigger.isBefore)
{
List<Id> accId = new List<Id>();
for(AccountExceptions__c spl:trigger.new)
{
accId.add(spl.Account__c);
}
// Soql Account Team member to get the User ID
List<AccountTeamMember> RegularDOS = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> growthDos = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Growth DOS' AND AccountId =:accId LIMIT 1];
List<AccountTeamMember> RVPUsers = [SELECT UserId, TeamMemberRole, Id, AccountId FROM AccountTeamMember WHERE TeamMemberRole= 'Management Team - Auto Finance RVP' AND AccountId =:accId LIMIT 1];
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account'];
//Id profileId=userinfo.getProfileId();
User u = AF_DealerCRM_Utility.getUserInfo();
String profileName= u.Profile.Name;
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c myobj : trigger.new)
{
if(myobj.NewRequest__r.RecordTypeId != rtypes.Id) {
if(profileName=='AF: Sales Specialist' && growthDos.size() > 0){
myobj.DOSUser__c= growthDos[0].UserId;
}
else if(profileName=='AF: Sales Specialist' && growthDos==null && RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
else if( RegularDOS.size() > 0)
{
myobj.DOSUser__c= RegularDOS[0].UserId ;
}
if(RVPUsers.size() > 0){
myobj.RVP_Approver__c= RVPUsers[0].UserId ;
}
}
if(myobj.Estimated_Amount_of_Current_Exception__c>20000)
{
myobj.Executive_Approval_Reasons__c='Exception Value > $20K';
}
}
}
if (trigger.isInsert && trigger.isAfter)
{
List<Id> rvpId = new List<Id>();
Set<Id> adrId = new Set<Id>();
for(AccountExceptions__c spl:trigger.new)
{
rvpId.add(spl.DOSUser__c);
rvpId.add(spl.Account__c);
adrId.add(spl.Id);
}
RecordType rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account' limit 1];
User RVPUsers =[SELECT Name, ManagerID, Manager.Name FROM User WHERE Id = :rvpId LIMIT 1];
List<AccountExceptions__c> adrExpList = [Select Id,RVP_Approver__c,Account__c,Account__r.RecordTypeId From AccountExceptions__c Where Id IN :adrId];
List<AccountExceptions__c> oppts = new List<AccountExceptions__c>();
for (AccountExceptions__c obj : adrExpList)
{
if(obj.NewRequest__r.RecordTypeId == rtypes.Id) {
obj.RVP_Approver__c= RVPUsers.ManagerID;
}
}
update adrExpList;
}
}
For the Best Practise you have to write only one trigger in a perticular object.
so just write all the functions in one trigger.
you can write more then one event in a trigger i.e
Trigger AF_Approval on AccountExceptions__c (before insert,After Insert,before Update,After Update) {}
and you can separate them with the help of contaxt variable.
write them on if Condition