0

Following trigger code not set opportunity owner based on opportunity's account billingCountry, it is working when i inserted single record but it's not working in bulk data import.

All record get in ELSE condition and set the default owner but that's wrong. Please have a look into it and tell me what i have done in this code,

Apex trigger :

trigger CorrectOpportunityFieldValues on Opportunity (Before Insert) {

Set<id> objSet = new Set<id>();    

for(Opportunity op : Trigger.New){
    objSet.add(op.AccountId);
}

List<Account> AccountBillingCountry = [select BillingCountry, BillingState from Account where Id in: objSet];

for(Opportunity op : Trigger.New){

    for(Account objAcc: AccountBillingCountry){

        if((objAcc.BillingCountry != Null || objAcc.BillingCountry != '') && (op.Country_of_Destination__c == Null || op.Country_of_Destination__c == '')){
            op.Country_of_Destination__c = objAcc.BillingCountry;
        }

        if(op.Sync_With_OTT__c == True && op.OTTOppBusinessUnit__c == 'BU Adcon'){

            String BC = objacc.BillingCountry;

            if(BC == 'Botswana' || BC == 'Mozambique' || BC == 'South Africa'){
                op.OwnerID='005360000025jMN';   
            }else if (BC == 'United States'){
                op.OwnerID='00536000004DLnC';   
            }else if (BC =='France' || BC =='Spain' || BC =='Greece' || BC =='Italy' || BC =='Luxembourg' || BC =='Malta' || 
                    BC =='Monaco' || BC =='Portugal' || BC =='Albania' || BC =='Bosnia-Herzegovina' || BC =='Bulgaria' || BC =='Croatia'){
                op.OwnerID='00536000006JZHK';   
            }else if (BC =='Austria' || BC =='Austria' || BC =='Switzerland' || BC =='Austria' || BC =='Czech Republic' || BC =='Hungary' ||
                    BC =='Slovakia' || BC =='Canada' || BC =='Costa Rica' || BC =='El Salvador' || BC =='Guatemala' || BC =='Honduras'){
                op.OwnerID='005360000025jKC';
            }else{
                op.OwnerID='005360000025jJs';   
            }
        }
    }
}
}

1 Answer 1

2

This is not working with multiple records because you never select the account related to the current opportunity in your for loop.

To select the right account linked to the opportunity you first have to declare your account list as a Map. Like this, each account record is mapped to the account id:

Map<Id, Account> accountById = new Map<Id, Account>([select BillingCountry, BillingState from Account where Id in: objSet]);

Then, in your for loop through the opportunities, select the right account using the previously declared map:

for(Opportunity op : Trigger.New){
    Account objAcc = accountById.get(op.AccountId);

Now, you don't need to loop through your account list. Just add your current code.

Note

Please not that using hard coded Id's is a bad practice. Your code won't work in another Salesforce org.

2
  • Thanks Martin !! I know that hard coded Id is bad practice but in this case what should i use because ID is only option to assign. correct me if i'm wrong.
    – Nail David
    Sep 26, 2017 at 9:09
  • 1
    You're welcome. Actually you could query the user's ids depending on its username for example to assign the opportunities owner ids. Sep 26, 2017 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .