• briano1
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I have created an APEX class with a webservice function. This function should run from a custom button on a custom object.  However, I get errors when creating a testMethod. (It looks like I require a testMethod before I can use the APEX class)

Here is the APEX class I have created with the testMethod commented out. Can you let me know how I can test my APEX class?

Thanks,
Brian

global class ConvertDealReg{
webService static Boolean ConvertDealReg1(Deal_Registration__c d){
// this webservice receives a Deal Reg
// Creates an account for the End Customer, depending on whether or not it already exists
// Creates a Contact for the End Customer, depending on whether or not it already exists
// Creates an opportunity using Account and Contact

// 1: check for duplicate End User account
boolean cCreate = true; // default to true
Account a;
if (d.Full_Company_Name__c != null && d.Contact_City__c != null){
// query to find dupes
if ([select count() from Account where Name = :d.Full_Company_Name__c and Site = :d.Contact_City__c] >= 1) {
cCreate=false;
// set a to be the dupe account so we can get the Id value of the account later
a = [select Id from Account where Name = :d.Full_Company_Name__c and Site = :d.Contact_City__c limit 1];
}
}

// create a boolean to catch any errors in case we need to rollback
boolean err = false;

// create the account if necessary based off previous check
if (cCreate){
try{
a = new Account();
a.Name = d.Full_Company_Name__c;
a.Account_Type1__c = 'Customer';
a.Site = d.Contact_City__c;
a.BillingStreet = d.Contact_Street_Address__c;
a.BillingPostalCode = d.Contact_Zip_Postal_Code__c;
a.BillingState = d.Contact_State_Province__c;
a.BillingCountry = d.Contact_Country__c;
a.Phone = d.Contact_Phone__c;
a.Fax = d.Contact_Fax_Number__c;
a.Industry = d.Industry_Type__c;

insert a;
} catch (System.DmlException e) {
//update our err flag
err = true;
System.debug('error inserting new account record');
for (Integer k = 0; k < e.getNumDml(); k++) {
// Process exception here
System.debug(e.getDmlMessage(k));
}
}
}
//set Account ID in Deal Reg
d.Customer_Name__c = a.Id;

// 2: check for duplicate End User contact
cCreate = true; // default to true
Contact c;
String strFirstName = '';
String strLastName = '';

if (d.Primary_Contact__c != null){

String s = d.Primary_Contact__c;
if(s.indexOf(' ') > 0){
strFirstName = s.subString(0, s.indexOf(' ') - 1);
strLastName = s.subString(s.indexOf(' ') + 1);
}else{
strLastName = s;
}

// query to find dupes
if ([select count() from Contact where AccountID = :a.ID and Name = :d.Primary_Contact__c] >= 1) {
cCreate=false;
// set c to be the dupe contact
c = [select Id from Contact where AccountID = :a.ID and Name = :d.Primary_Contact__c limit 1];
}
}

// create the contact if necessary based off previous check
if (cCreate){
try{
c = new Contact();
c.AccountID = a.Id;
c.FirstName = strFirstName;
c.LastName = strLastName;
c.Contact_Type__c = 'End User';

c.Title = d.Contact_Title__c;
c.MailingStreet = d.Contact_Street_Address__c;
c.MailingPostalCode = d.Contact_Zip_Postal_Code__c;
c.MailingState = d.Contact_State_Province__c;
c.MailingCountry = d.Contact_Country__c;

c.Email = d.Contact_Email__c;
c.Phone = d.Contact_Phone__c;
c.Fax = d.Contact_Fax_Number__c;

insert c;
} catch (System.DmlException e) {
//update our err flag
err = true;
System.debug('error inserting new contact record');
for (Integer k = 0; k < e.getNumDml(); k++) {
// Process exception here
System.debug(e.getDmlMessage(k));
}
}
}
//set Contact ID in Deal Reg
d.Customer_Contact__c = c.Id;

//3. Create a New MSD Opportunity
Opportunity o;
try{
o.Name = d.Full_Company_Name__c + '-' + d.Primary_Contact__c + '-' + Date.Today();
o.AccountId = a.Id;
o.CloseDate = d.Estimated_Close_Date__c;
o.Amount = 1;
o.StageName = 'Prospecting';
o.Probability = 10;
o.Opportunity_Source__c = 'Deal Registration';
o.Deal_registration__c = d.Id;
insert o;
} catch (System.DmlException e) {
//update our err flag
err = true;
System.debug('error inserting new opportunity record');
for (Integer k = 0; k < e.getNumDml(); k++) {
// Process exception here
System.debug(e.getDmlMessage(k));
}
}

// check for errors and return the success flag
if (!err) {
return true;
} else {
// further error handling here
return false;
}
}

// The following is a simple unit test
//static testMethod void myTest() {
//Deal_Registration__c test_d;
//test_d = [select Id from Deal_Registration__c where Id = 'a0I70000000UCpk'];
//Boolean bnResult = ConvertDealReg1(test_d);
//System.assertEquals(true, bnresult);
//}
}