You need to sign in to do that
Don't have an account?
Pedro Argenti
Populate Account_Country__c on Opportunity with Apex Trigger
Hello,
I need to create sharing rules for opportunities using the account country as a criterion. I tried doing that by simply creating a new field and using formula to populate the field with the account country, but sharing rules can't use fields with formulas as criteria.
A possible alternative is to populate my custom text field and populate it with an Apex trigger. I have created a trigger which does exactly what I need and now I need to deploy it. Since I have limited experience in Apex, I need some help to create the test required for this field.
Thanks for your help!
trigger AccountCountry on Opportunity (before insert, before update){
Set<Id> ownerIds = new Set<Id>();
Set<Id> AccountIds = new Set<Id>();
for (Opportunity op : Trigger.new) {
ownerIds.add(op.OwnerId);
accountIds.add(op.AccountId);
}
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT BillingCountry FROM Account WHERE Id IN :accountIds]);
String AccountCountryName;
for (Opportunity op : Trigger.new) {
// add the BillingCountry
AccountCountryName = accountMap.get(op.AccountId).BillingCountry;
// set the name :-)
op.Account_Country__c = AccountCountryName;}
static testMethod void testAccountCountry() {
??
}
I need to create sharing rules for opportunities using the account country as a criterion. I tried doing that by simply creating a new field and using formula to populate the field with the account country, but sharing rules can't use fields with formulas as criteria.
A possible alternative is to populate my custom text field and populate it with an Apex trigger. I have created a trigger which does exactly what I need and now I need to deploy it. Since I have limited experience in Apex, I need some help to create the test required for this field.
Thanks for your help!
trigger AccountCountry on Opportunity (before insert, before update){
Set<Id> ownerIds = new Set<Id>();
Set<Id> AccountIds = new Set<Id>();
for (Opportunity op : Trigger.new) {
ownerIds.add(op.OwnerId);
accountIds.add(op.AccountId);
}
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT BillingCountry FROM Account WHERE Id IN :accountIds]);
String AccountCountryName;
for (Opportunity op : Trigger.new) {
// add the BillingCountry
AccountCountryName = accountMap.get(op.AccountId).BillingCountry;
// set the name :-)
op.Account_Country__c = AccountCountryName;}
static testMethod void testAccountCountry() {
??
}
Please follow the below test class sample code which you can further modify as per your requirement:
@isTest
Public class AccountCountryTestClass {
static testMethod void insertOpp() {
Opportunity op= new Opportunity ();
op.Name='Test';
op.StageName='Prospecting';
op.CloseDate=System.now().date() ;
insert op;
}
}
Also, please refer below links that will help you to build the further logic in your test class:
1.https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm
2.http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm
3.https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
4.http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
5.http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html
6.http://salesforce.stackexchange.com/questions/21707/writing-test-classes-for-a-custom-controller
I hope this helps.
Please mark this as a "Best Answer" if this has resolved your issue.