function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
briano.ax404briano.ax404 

Not able to save/run testMethod

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);
//}
}
aalbertaalbert

What errors are you getting? Are you getting errors when deploying your code to a Production org and the test methods are failing?

 

I do recommend avoiding the hardcoded Id in your test method. Instead, create a new Deal_Registration__c record within your test method.The hardcoded Id might not be valid in other orgs where you deploy this code so its ideal to create all data within your test method and not depend on existing data. 

 

 

briano.ax404briano.ax404

I can't save the class in Eclipse with the testMethod.

 

I followed your suggestion and now use a newly create object (Deal_Registration__C)

 

Hopefully you can make some sense of the following... 

 

 

  • The Error in Eclipse is:
    Save error: Unable to perform save on all files: An unexpected error has occurred. Please try again, or check the log file for details.

 

  • In the Force.com IDE Log Viewer, I can see the error:

(NullPointerException) Unable to perform save on all files.(Open Log file for full message and/or stacktrace)

 

  • The log file has the following entries:

ERROR [2009-02-05 09:52:56,250] (BuilderController.java:handleException:132) - Unable to perform save on all files.
java.lang.NullPointerException
 at com.salesforce.ide.core.services.ProjectService.setRunTestFailureMarker(ProjectService.java:2155)
 at com.salesforce.ide.core.services.ProjectService.handleRunTestMessages(ProjectService.java:2131)
 at com.salesforce.ide.core.services.ProjectService.handleRunTestResult(ProjectService.java:2052)
 at com.salesforce.ide.core.services.ProjectService.handleDeployResult(ProjectService.java:1789)
 at com.salesforce.ide.core.project.BuilderController.handleSaves(BuilderController.java:116)
 at com.salesforce.ide.core.project.BuilderController.build(BuilderController.java:95)
 at com.salesforce.ide.core.project.BuilderController.build(BuilderController.java:75)
 at com.salesforce.ide.core.project.OnlineBuilder.incrementalBuild(OnlineBuilder.java:79)
 at com.salesforce.ide.core.project.OnlineBuilder.build(OnlineBuilder.java:49)
 at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:624)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:166)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:197)
 at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:246)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:249)
 at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:302)
 at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:334)
 at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:137)
 at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:235)
 at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
 WARN [2009-02-05 09:55:29,328] (ProjectService.java:handleDeployResult:1777) - Save failed!
 WARN [2009-02-05 09:55:29,328] (ProjectService.java:handleRetrieveResult:1906) - Nothing to save to project - retrieve result is empty
ERROR [2009-02-05 09:55:29,328] (BuilderController.java:handleException:132) - Unable to perform save on all files.
java.lang.NullPointerException
 at com.salesforce.ide.core.services.ProjectService.setRunTestFailureMarker(ProjectService.java:2155)
 at com.salesforce.ide.core.services.ProjectService.handleRunTestMessages(ProjectService.java:2131)
 at com.salesforce.ide.core.services.ProjectService.handleRunTestResult(ProjectService.java:2052)
 at com.salesforce.ide.core.services.ProjectService.handleDeployResult(ProjectService.java:1789)
 at com.salesforce.ide.core.project.BuilderController.handleSaves(BuilderController.java:116)
 at com.salesforce.ide.core.project.BuilderController.build(BuilderController.java:95)
 at com.salesforce.ide.core.project.BuilderController.build(BuilderController.java:75)
 at com.salesforce.ide.core.project.OnlineBuilder.incrementalBuild(OnlineBuilder.java:79)
 at com.salesforce.ide.core.project.OnlineBuilder.build(OnlineBuilder.java:49)
 at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:624)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:166)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:197)
 at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:246)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
 at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:249)
 at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:302)
 at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:334)
 at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:137)
 at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:235)
 at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55) 

 

 

 

 

  • My TestMethod is:
static testMethod void myTest() {

Deal_Registration__c test_d = new Deal_registration__C();test_d.Full_Company_Name__c =

'zz company';

test_d.Contact_City__c = 'zz city';test_d.Contact_Street_Address__c =

'zz street';

test_d.Contact_Zip_Postal_Code__c = 'zz postcode';test_d.Contact_State_Province__c =

'AL';

test_d.Contact_Country__c = 'US';test_d.Contact_Phone__c =

'999-999';

test_d.Contact_Fax_Number__c = '999-999';test_d.Industry_Type__c =

'aa';

test_d.Primary_Contact__c = 'first last';Date myDate = date.valueOf(

'2010-01-01');

test_d.Estimated_Close_Date__c = myDate;

insert test_d;

 

//test_d = [select Id from Deal_Registration__c where Id = 'a0I70000000UCpk'];

Boolean bnResult = ConvertDealReg1(test_d);

System.assertEquals(true, bnresult);

}

NaishadhNaishadh
I think you are using older version of eclipse plugin. I had also faced the same error. Try to update eclipse plugin and it will work fine.
briano.ax404briano.ax404

went through the Help-> Find and and Install... -> search for updates of the currently install features.

 

That update didn't change anything.

 

I'm running Eclipse Platform 3.3.2

teknicsandteknicsand

I'm running into the same issue. Did you manage to resolve it? 

 

~Sand 

MaxaMaxa
hi i'm gettigs same error, its fine in sandbox but not prod, anyone managed to fix it?
teknicsandteknicsand
@Maxa: What do your logs say? I get it because of two things - not enough code coverage, and an error in the test class!!
MaxaMaxa

it deos not say mutch at all

ERROR [2009-07-29 10:09:31,846] (BuilderController.java:handleException:132) - Unable to perform save on all files.

java.lang.NullPointerException

 

i think you right it is the test method because i changed the querry i used before, i play around a bit with it see if it works

teknicsandteknicsand
Trace through your debug statement values in the log, you might find something!!!
briano1briano1

I never got my test working with this class i posted above. Instead, I created visualforce pages and a controller class and i have a test class created for this.  here is the test class code in case that helps you:

 

@isTest
private class ConvertDealRegTest {

 private static Deal_Registration__c prepareDealReg(ID customerAcctId, ID customerContId) {

  Deal_Registration__c dealReg =
   new Deal_Registration__c(
    Full_Company_Name__C = 'Johnsonz Inc.', Contact_City__c = 'Minneapolis',
    Contact_Street_Address__c = 'Wayzata Blvd.', Contact_Zip_Postal_Code__c = '55054',
       Contact_State_Province__c = 'MN', Contact_Country__c = 'United States',
       Contact_Phone__c = '321-517-1253', Contact_Fax_Number__c = '321-517-1254',
       Industry_Type__c = 'Information Services', Customer_Name__c = customerAcctId,
       Primary_Contact__c = 'Jane Doe', Contact_Title__c = 'Dev Manager',
       Contact_Email__c = 'john@example.com', Customer_Contact__c = customerContId,
       Estimated_Close_Date__c = Date.today().addDays(30));
     insert dealReg;
     return dealReg;
 }

 private static Account prepareAccount() {
  Account acct =
   new Account(
    Name = 'Johnsonz Inc.', Site = 'Manufacturing', BillingState = 'MN',
    BillingCountry = 'United States', BillingCity ='Minneapolis',
    RecordTypeId = Constants.ID_ACCOUNT_REC_TYPE_MSD);
  insert acct;
  return acct;
 }
 
 private static Contact prepareContact(ID customerAcctId) {
  Contact cont =
   new Contact(
    FirstName = 'John', LastName = 'Doe', AccountId = customerAcctId);
  insert cont;
  return cont;
 }
 
 public static testmethod void testConvertDealReg() {

  ConvertDealRegController cntrlr = new ConvertDealRegController();
  cntrlr.getAccounts();
  cntrlr.step2();

  Account custAcct = prepareAccount();
  Contact custCont = prepareContact(custAcct.Id);

  Deal_Registration__c dealReg = prepareDealReg(custAcct.Id, custCont.Id);

  PageReference pageRef = Page.convertDealReg1;
  Test.setCurrentPage(pageRef);
     Map<String, String> requestParams = pageRef.getparameters();
     requestParams.put('drid', dealReg.Id);

  cntrlr.getAccounts();
  cntrlr.getSelected();
  cntrlr.GetSelectedAccounts();
  cntrlr.getAccount();
  cntrlr.getContact();
  cntrlr.getOpportunity();
  cntrlr.step1();
  cntrlr.step2();

  cntrlr.save();
  cntrlr.Cancel();
 }
}