You need to sign in to do that
Don't have an account?
uma52551.3972270309784705E12
Apex trigger to update account rating when the opportunity stage equals closed won
Can any one help me writing the trigger for upating the account rating field to 'hot' when the oppty stage is equal to 'closed won'?
thanks in advance...
thanks in advance...
All Answers
Here is a sample code:
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
Thank you very much for your great help. If you don't mind can you please help me in writting the test class as well.
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
I am having the similar problem with another trigger which I wrote. Can you please check this and please let me knwo what is my mistake. (Don't Mind)
Apex Trigger for Updating Contact Owner same as the Account Owner:
trigger BUIContactOwnerUpdateTrigger on Contact (before insert, before update) {
List <RecordType> rtList = [SELECT Id from RecordType where SobjectType='Contact' and Name='BUI' Limit 1];
List <Id> AccountIds = new List <Id>();
Map<Id,Id> accountOwnerIdMap = new Map<Id,Id>();
for(Contact c : trigger.new)
{
if(c.AccountId !=NULL)
AccountIds.add(c.AccountId);
System.debug('Adding Account Id:'+c.AccountId);
}
List<Account> accountList = [SELECT Id,ownerId from Account WHERE Id IN :AccountIds];
for(Account acc : accountList){
accountOwnerIdMap.put(acc.Id, acc.ownerId);
}
for(Contact c:trigger.new){
System.debug('The Record Type list Id:' +rtList[0].Id);
if(c.recordTypeId ==rtList[0].Id)
{
c.ownerId = accountOwnerIdMap.get(c.accountId);
System.debug('To Know contact OwnerId :'+c.OwnerId);
}
}
}
Apex Test Class for the above Trigger:
@isTest
private class TestBUIContactOwnerUpdateTrigger {
static testMethod void myTestUpdate() {
Account a = new Account(Name = 'Test Account TA1');
User user1 = [Select Id from User where FirstName = 'XYZ' AND LastName = '1234' Limit 1];
a.ownerId =user1.Id;
insert a;
Contact c1 = new Contact(AccountId=a.Id,lastname='Test1',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user1.Id);
insert c1;
System.assertEquals(c1.ownerId,a.ownerId);
User user2 = [Select Id from User where FirstName = 'ABC' AND LastName = '3456' Limit 1];
Contact c2 = new Contact(AccountId=a.Id,lastname='Test2',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user2.Id);
insert c2;
System.assertEquals(c2.ownerId,a.ownerId);
}
}
Assertion is not working gor the second contact. First contact was good, by this it is not passing the test.. please help me in finnding the bug in my code.
@isTest
private class TestBUIContactOwnerUpdateTrigger {
static testMethod void myTestUpdate() {
Profile p = [select id from profile where name ='standard user'];
User user1 = new User(alias = 'test123', email='test123@noemail.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
insert user1;
User user2 = new User(alias = 'test456', email='test456@noemail.com',
emailencodingkey='UTF-8', lastname='Testing2', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='test456@noemail.com');
insert user2;
Account a = new Account(Name = 'Test Account TA1');
a.ownerId =user1.Id;
insert a;
Contact c1 = new Contact(AccountId=a.Id,lastname='Test1',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user1.Id);
insert c1;
System.assertEquals(c1.ownerId,a.ownerId);
System.debug('After assertion:' + c1.ownerId);
System.debug('Before assertion:' + a.ownerId);
Contact c2 = new Contact(AccountId=a.Id,lastname='Test2',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user2.Id);
insert c2;
System.debug('Contact accoutId:' + c2.accountId);
System.assertEquals(c2.ownerId,a.ownerId);
System.debug('After assertion:' + c2.ownerId);
}
}
Contact c1 = new Contact(AccountId=a.Id,lastname='Test1',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user1.Id);
Contact c2 = new Contact(AccountId=a.Id,lastname='Test2',recordTypeId='012E00000001sCE',firstname='Contact', OwnerId=user2.Id);
list<contact> ContactList = new list<contact>();
ContactList.add(c1);
ContactList.add(c2);
insert ContactList;
System.assertEquals(ContactList[0].ownerId,a.ownerId);
System.assertEquals(ContactList[1].ownerId,a.ownerId);
System.AssertException: Assertion Failed: Expected: 005f00000010hCLAAY, Actual: 005f00000010hCKAAY
The Account Rating trigger is not working can you please help to find out the bug or I am missing some thing?
trigger BBWGUpdateAccountRating on Opportunity (after insert,after update) {
List <RecordType> rtList = [SELECT Id from RecordType where SobjectType='Opportunity' and Name='BBWG' Limit 1];
set<ID> AccountIDs = new set<ID>();
for(Opportunity o : Trigger.new){
if(o.StageName == 'Closed Won'){
AccountIDs.add(o.accountid);
System.debug('Account Id:'+ o.accountId);
}
}
list<Account> accounts = new list<account>();
for(account a:[select Id, Rating from account where Id IN :AccountIDs]){
for(opportunity opp:trigger.new){
if(opp.stageName=='closed won'){
a.Rating='hot';
accounts.add(a);
}
}
}
update accounts;
}
@isTest
private class TestBBWGUpdateAccountRatingTrigger {
static testMethod void myTestUpdateRating() {
Opportunity o = new Opportunity(Name = 'New Oppty NP1',stageName='Closed Won',recordTypeId='012E00000001AtQ',closeDate=Date.today());
insert o;
Account a = new Account(Name = 'Test Account TA2',rating = 'Cold',recordTypeId='012E00000001Ata',Id = o.accountId);
insert a;
System.debug('Before Assertion:'+a.rating);
System.assert(a.rating=='Hot');
update a;
System.debug('After Assertion:'+a.rating);
}
}
You should insert the account record first and then insert the opportuniity record, since the trigger is on the opportunity object. Please try something like this:
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
Thanks in Advance
Hope it helps.
Thanks
Smriti Sharan
How to deal with above scenario new one---->
/*Update Account Rating to 'Hot' on account when opp stage equals to 'Closed Won' ELSE update account rating to 'Cold'*/
trigger Accountoppstage2 on Opportunity ( After insert, After update) {
list<Account> acclist= new list<Account>();
Set<Id> accID = new Set<Id>();
Set<Id> ratID= new Set<Id>();
for(Opportunity Opp: Trigger.new){
if(Opp.StageName=='Closed Won'){
AccID.add(Opp.AccountID);
}else{
ratID.add(Opp.ratID);
}
}
for(account acc:[Select Id,Rating from Account where ID In: accID]){
acc.Rating= 'Hot';
acclist.add(acc);
}
for(account acc:[Select Id,Rating from Account where ID In: ratID]){
acc.Rating= 'Cold';
acclist.add(acc);
}
Update acclist;
}
And Facing below error...................>
Variable does not exist: ratID
Thanks in Advance!!!!!
as i can see you used ratID.add(Opp.ratID); in else case instead you can use
ratID.add(Opp.AccountID);
Q-) Apex trigger to update account rating when the opportunity stage equals closed won
Trigger
trigger OpportunityTrigger12 on Opportunity (after insert,after update) {
if(Trigger.isAfter){
if(Trigger.isInsert){
OpportunityTrigger12Handler.beforeInsert(Trigger.New);
}else if(Trigger.isUpdate){
OpportunityTrigger12Handler.afterUpdateMethod(Trigger.New,Trigger.oldMap);
}
}
}
Handler_Class
public class OpportunityTrigger12Handler {
public static void beforeInsert(List<Opportunity> l){
Set<Id> setOfAccountId = new Set<Id>();
Map<Id,Account> mapId_To_Acc;
List<Account> lst = new List<Account>();
for(Opportunity o:l){
if(o.stageName == 'Closed Won' && o.AccountId != null){
setOfAccountId.add(o.AccountId);
}
}
if(!setOfAccountId.isEmpty()){
mapId_To_Acc = new Map<id,Account>([SELECT Id FROM Account WHERE Id IN:setOfAccountId]);
}
if(mapId_To_Acc.size() > 0){
for(Id objA : mapId_To_Acc.keySet()){
Account obj = new Account();
obj.Id = objA;
obj.Rating = 'Hot';
lst.add(obj);
}
}
DataBase.SaveResult[] res = Database.update(lst,false);
}
public static void afterUpdateMethod(List<Opportunity> newLstOf_Opp, Map<Id,Opportunity> mapof_oldOpportunity){
Set<ID> setOfAccId = new Set<ID>();
Map<Id,Account> mapId_To_Acc;
List<Account> lstOfUpdated_Account = new List<Account>();
for(Opportunity objOp : newLstOf_Opp){
if(mapof_oldOpportunity != null && objOp.AccountId != null && objOp.StageName == 'Closed Won' && objOp.StageName != mapof_oldOpportunity.get(objOp.Id).StageName){
setOfAccId.add(objOp.AccountId);
}
}
if(setOfAccId.size() > 0){
mapId_To_Acc = new Map<Id,Account>([Select Id from Account where Id IN: setOfAccId]);
}
for(Account objAccount : mapId_To_Acc.values()){
Account obj = new Account();
obj.Id = objAccount.Id;
obj.Rating = 'Hot';
lstOfUpdated_Account.add(obj);
}
Database.SaveResult[] res = Database.update(lstOfUpdated_Account);
}
}