You need to sign in to do that
Don't have an account?
Ella Fitzpatrick
Require Opportunity Contact Roles - Apex Test Code
Hi All,
I downloaded the free app "Require Opportunity Contact Roles" which installs a field, Apex and Validation rule:
Field = Has Primary Contact (checkbox)
Validation Rule = Has Primary Contact has to be ticked if the Probablity of the Opportunity is 30% or above
Apex = Checks the Has Primary Contact Field once a Primary Contact is added.
HOWEVER, the Apex code needs to be rewritten to match up to our specific stage names etc and im not familiar with Apex at all and am not sure what each part of it means and where I need to put which field name.
Below is the Apex class detail. I would greatly appreciate it if someone could explain to me what exactly I need to replace their field names with. I.e. Role = Business User in the Apex is the name of the specific Role that has to be added to the Opportunity for the checkbox to be ticked:
public class test_updatecontactrolecount
{
public static testMethod void testcreateopptywithconditionandrole()
{
//Insert Opportunities
try
{
Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Stage 1 - Unqualified Prospect',Type ='New Business', CloseDate=System.Today());
insert Oppty;
// insert contact
Contact[] cont = new Contact[]
{
new Contact(LastName = 'testcontact1'),
new Contact(LastName = 'testcontact2')
};
insert cont;
// insert contact role
OpportunityContactRole [] ocr = new OpportunityContactRole[]
{
new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = True),
new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
};
insert ocr;
Oppty.StageName = 'Stage 3 - Eval Request';
//Update opportunity
Test.StartTest();
update Oppty;
Test.StopTest();
Oppty =[SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
system.assert (Oppty.Number_of_Contacts_Roles_Assigned__c == 2);
system.assert (Oppty.Primary_Contact_Assigned__c == True);
}
catch (System.DmlException e)
{
System.assert(false);
}
}
}
Any help you can provide would be much appreciated :)
I downloaded the free app "Require Opportunity Contact Roles" which installs a field, Apex and Validation rule:
Field = Has Primary Contact (checkbox)
Validation Rule = Has Primary Contact has to be ticked if the Probablity of the Opportunity is 30% or above
Apex = Checks the Has Primary Contact Field once a Primary Contact is added.
HOWEVER, the Apex code needs to be rewritten to match up to our specific stage names etc and im not familiar with Apex at all and am not sure what each part of it means and where I need to put which field name.
Below is the Apex class detail. I would greatly appreciate it if someone could explain to me what exactly I need to replace their field names with. I.e. Role = Business User in the Apex is the name of the specific Role that has to be added to the Opportunity for the checkbox to be ticked:
public class test_updatecontactrolecount
{
public static testMethod void testcreateopptywithconditionandrole()
{
//Insert Opportunities
try
{
Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Stage 1 - Unqualified Prospect',Type ='New Business', CloseDate=System.Today());
insert Oppty;
// insert contact
Contact[] cont = new Contact[]
{
new Contact(LastName = 'testcontact1'),
new Contact(LastName = 'testcontact2')
};
insert cont;
// insert contact role
OpportunityContactRole [] ocr = new OpportunityContactRole[]
{
new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = True),
new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
};
insert ocr;
Oppty.StageName = 'Stage 3 - Eval Request';
//Update opportunity
Test.StartTest();
update Oppty;
Test.StopTest();
Oppty =[SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
system.assert (Oppty.Number_of_Contacts_Roles_Assigned__c == 2);
system.assert (Oppty.Primary_Contact_Assigned__c == True);
}
catch (System.DmlException e)
{
System.assert(false);
}
}
}
Any help you can provide would be much appreciated :)
"Try this code for test class of Opportunity
Contact Role, it will help you."
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi