function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David Corns 16David Corns 16 

Apex trigger to update Account "Tier" when the Opportunity Stage equals Closed Won

I am new to Apex triggers. Can anyone help me writing the Trigger for updating a customised Account "Tier" field to Tier 1,2,3 or 4 when the Opportunity Stage is equal to 'Closed Won' and the Tier number is determined by the number of products purchased on the Opportunity e.g. Tier 1 = 10000+,Tier 2 = 5000-10000, Tier 3=2500-4999,Tier 4=<2500?
Thanks in advance.
Best Answer chosen by David Corns 16
Hemant_SoniHemant_Soni
Please try with below code All issues are resolved 
trigger OpportunityTrigger on Opportunity (After insert,After Update) {
    map<Id,Decimal> mapAccIdWiseTotalProduct = new map<Id,Decimal>();
    list<Account> lstAcc = new list<Account>();
    Decimal numberProduct = 0.0;
    Decimal TotalnumberProduct = 0.0;
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
            numberProduct += Opp.NumberofLicenses__c;//'NumberofLicenses__c' Field
            mapAccIdWiseTotalProduct.put(Opp.AccountId,numberProduct);
        }
    }
    
    for(Account oAcc : [Select Id,Name From Account Where Id IN  : mapAccIdWiseTotalProduct.keyset()]){// Tier__c
        if(mapAccIdWiseTotalProduct.containsKey(oAcc.Id)){
            TotalnumberProduct = mapAccIdWiseTotalProduct.get(oAcc.Id);
            if(TotalnumberProduct > 10000){
                // oAcc.Tier__c = 'x Tier 2';
            }else if(TotalnumberProduct >= 5000 && TotalnumberProduct <= 10000){
                // oAcc.Tier__c = 'x Tier 1';
            }else if(TotalnumberProduct >= 2000 && TotalnumberProduct <=4999){
                // oAcc.Tier__c = 'y Tier 2';
            }else if(TotalnumberProduct < 2000){
                // oAcc.Tier__c = 'z Tier 1';                
            }
            lstAcc.add(oAcc);
        }
    }
    if(lstAcc.size() > 0){
        update lstAcc;
    }
}

 

All Answers

Hemant_SoniHemant_Soni
Hi David,
Please try Below Example And Please update Code According To my Comment.\
trigger OpportunityTrigger on Opportunity (After insert,After Update) {
    map<Id,Decimal> mapAccIdWiseTotalProduct = new map<Id,Decimal>();
    list<Account> lstAcc = new list<Account>();
    Decimal numberProduct = 0.0;
    Decimal TotalnumberProduct = 0.0;
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
            numberProduct += Opp.hemant1993__Number__c;
            mapAccIdWiseTotalProduct.put(Opp.AccountId,numberProduct);
        }
    }
    
    for(Account oAcc : [Select Id,Name From Account Where Id IN  : mapAccIdWiseTotalProduct.keyset()]){// Add Your Tire Field In Query For Example Tire__c
        if(mapAccIdWiseTotalProduct.containsKey(oAcc.Id)){
            TotalnumberProduct = mapAccIdWiseTotalProduct.get(oAcc.Id);
            if(TotalnumberProduct > 10000){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 1';
            }else if(TotalnumberProduct >= 5000 && TotalnumberProduct <= 10000){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 2';
            }else if(TotalnumberProduct >= 2500 && TotalnumberProduct <=4999){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 2';
            }
            lstAcc.add(oAcc);
        }
    }
    if(lstAcc.size() > 0){
        update lstAcc;
    }
}
Please let me know if you are facing any issue.
If it works for you then PLEASE MARK THIS SOLVED.
David Corns 16David Corns 16
Hi Hermant,
Thank you for your quick reply. I should have been more precise with my terminology - "number of products purchased" this is just a custom field on an Opportunity that we use to identify the quantity of products that we have sold. How will this change the Trigger that you kindly provided?
Thank you again and apologies for the lack of clarity.
David
Hemant_SoniHemant_Soni
I have marked in trigger where you need to add "number of products purchased". Please update Accordingly.
trigger OpportunityTrigger on Opportunity (After insert,After Update) {
    map<Id,Decimal> mapAccIdWiseTotalProduct = new map<Id,Decimal>();
    list<Account> lstAcc = new list<Account>();
    Decimal numberProduct = 0.0;
    Decimal TotalnumberProduct = 0.0;
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
            numberProduct += Opp.hemant1993__Number__c;// *******HERE******* Replace 'hemant1993__Number__c' Field
            mapAccIdWiseTotalProduct.put(Opp.AccountId,numberProduct);
        }
    }
    
    for(Account oAcc : [Select Id,Name From Account Where Id IN  : mapAccIdWiseTotalProduct.keyset()]){// Add Your Tire Field In Query For Example Tire__c
        if(mapAccIdWiseTotalProduct.containsKey(oAcc.Id)){
            TotalnumberProduct = mapAccIdWiseTotalProduct.get(oAcc.Id);
            if(TotalnumberProduct > 10000){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 1';
            }else if(TotalnumberProduct >= 5000 && TotalnumberProduct <= 10000){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 2';
            }else if(TotalnumberProduct >= 2500 && TotalnumberProduct <=4999){
                // Put Account Field and Assign That Field Value
                // For Example oAcc.Tire__c = 'Tire 2';
            }
            lstAcc.add(oAcc);
        }
    }
    if(lstAcc.size() > 0){
        update lstAcc;
    }
}
David Corns 16David Corns 16
trigger OpportunityTrigger on Opportunity (After insert,After Update) {
    map<Id,Decimal> mapAccIdWiseTotalProduct = new map<Id,Decimal>();
    list<Account> lstAcc = new list<Account>();
    Decimal numberProduct = 0.0;
    Decimal TotalnumberProduct = 0.0;
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
            numberProduct += Opp.NumberofLicenses__c';//'NumberofLicenses__c' Field
            mapAccIdWiseTotalProduct.put(Opp.AccountId,numberProduct);
        }
    }
    
    for(Account oAcc : [Select Id,Name From Account Where Id IN  : mapAccIdWiseTotalProduct.keyset()]){// Tier__c
        if(mapAccIdWiseTotalProduct.containsKey(oAcc.Id)){
            TotalnumberProduct = mapAccIdWiseTotalProduct.get(oAcc.Id);
            if(TotalnumberProduct > 10000){
                // oAcc.Tier__c = 'x Tier 2';
            }else if(TotalnumberProduct >= 5000 && TotalnumberProduct <= 10000){
                // oAcc.Tier__c = 'x Tier 1';
            }else if(TotalnumberProduct >= 2000 && TotalnumberProduct <=4999){
                // oAcc.Tier__c = 'y Tier 2';
            }else if(TotalnumberProduct < 2000){
                // oAcc.Tier__c = 'z Tier 1';                
            }
            lstAcc.add(oAcc);
        }
    }
    if(lstAcc.size() > 0){
        update lstAcc;
    }
}
I received the following error message.
Error: Compile Error: Missing closing quote character ' on string. at line 23 column 46 i.e. the // oAcc.Tier__c = '
 
Hemant_SoniHemant_Soni
Please try with below code All issues are resolved 
trigger OpportunityTrigger on Opportunity (After insert,After Update) {
    map<Id,Decimal> mapAccIdWiseTotalProduct = new map<Id,Decimal>();
    list<Account> lstAcc = new list<Account>();
    Decimal numberProduct = 0.0;
    Decimal TotalnumberProduct = 0.0;
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
            numberProduct += Opp.NumberofLicenses__c;//'NumberofLicenses__c' Field
            mapAccIdWiseTotalProduct.put(Opp.AccountId,numberProduct);
        }
    }
    
    for(Account oAcc : [Select Id,Name From Account Where Id IN  : mapAccIdWiseTotalProduct.keyset()]){// Tier__c
        if(mapAccIdWiseTotalProduct.containsKey(oAcc.Id)){
            TotalnumberProduct = mapAccIdWiseTotalProduct.get(oAcc.Id);
            if(TotalnumberProduct > 10000){
                // oAcc.Tier__c = 'x Tier 2';
            }else if(TotalnumberProduct >= 5000 && TotalnumberProduct <= 10000){
                // oAcc.Tier__c = 'x Tier 1';
            }else if(TotalnumberProduct >= 2000 && TotalnumberProduct <=4999){
                // oAcc.Tier__c = 'y Tier 2';
            }else if(TotalnumberProduct < 2000){
                // oAcc.Tier__c = 'z Tier 1';                
            }
            lstAcc.add(oAcc);
        }
    }
    if(lstAcc.size() > 0){
        update lstAcc;
    }
}

 
This was selected as the best answer
David Corns 16David Corns 16
Thanks again Hemant - it works now!