You need to sign in to do that
Don't have an account?
Oron Mizrachi
Apex class test
hello i worte an apex trigger code, which update custom fields in oppertunity object, with the details of the primary contactrole,
the code works fine , but i cant deploy it cause i just dont know how to builed an apex test ,
could someone help me with that.
the code:
trigger PrimaryContactRole on Opportunity (before update) {
List<String> names = new List<String>();
for (Opportunity o : Trigger.new) {
Integer i = [select count() from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];
if(i==1) {
OpportunityContactRole contactRole = [select Contact.name,Contact.email,Contact.phone ,Contact.Passport_Num_contact__c,Contact.Local_ID_contact__c from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];
o.Opportunity_Contact_email__c = contactRole.Contact.email;
o.Opportunity_Contact_name__c = contactRole.Contact.name;
o.Opportunity_Contact_passport__c = contactRole.Contact.Passport_Num_contact__c;
o.Opportunity_Contact_phone__c = contactRole.Contact.phone;
o.Opportunity_Contact_localid__c = contactRole.Contact.Local_ID_contact__c;
o.Contact_Role_Count__c = i;
}
}
thank you!
the code works fine , but i cant deploy it cause i just dont know how to builed an apex test ,
could someone help me with that.
the code:
trigger PrimaryContactRole on Opportunity (before update) {
List<String> names = new List<String>();
for (Opportunity o : Trigger.new) {
Integer i = [select count() from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];
if(i==1) {
OpportunityContactRole contactRole = [select Contact.name,Contact.email,Contact.phone ,Contact.Passport_Num_contact__c,Contact.Local_ID_contact__c from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];
o.Opportunity_Contact_email__c = contactRole.Contact.email;
o.Opportunity_Contact_name__c = contactRole.Contact.name;
o.Opportunity_Contact_passport__c = contactRole.Contact.Passport_Num_contact__c;
o.Opportunity_Contact_phone__c = contactRole.Contact.phone;
o.Opportunity_Contact_localid__c = contactRole.Contact.Local_ID_contact__c;
o.Contact_Role_Count__c = i;
}
}
thank you!
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
For your case you will have create an Account/Contact(s)/Opportunity test data, as soon as you will insert the opportunity the trigger will fire and will give you the code coverage.
Just be careful as I can see in your code you got select queries running inside the for loop, which is not good practice and will hit the limits for bulk uploads.
As a best practice try not to have select or DML inside the loops and use more of List/Set/Maps (collections)
Haider
Try with below cod eit will definitly help !
Check the below link it will help you to write better test class in future .
http://https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BKUCIA4
Let me know if it helps !
Thanks
Manoj