• briano.ax404
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    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);
//}
}
I was hoping to log someone into Salesforce.com, retrieve their userinfo and then open a salesforce.com page for them using the sessionid from the api.
Is this possible?
Rgds,
Brian
Is it possible to use the api to log into http://apps.salesforce.com
 
I want to log in as a partner into PRM and retrieve some information using the API
 
thanks,
Brian


Message Edited by briano on 11-07-2008 08:42 AM
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);
//}
}
I am slavishly trying to follow the many code snippets in cookbooks etc, to try an achieve something simple. I wish to generate a record in my custom object "Shipment" which is linked to a standard object Opportunity. Most examples in the documentation are far more complex than this, but I can't get it to work at all.
 
The button is on the Opportunity detail form (is that the wrong place?) and should pass the opportunity record into my apex class to insert the record in Shipment__c.
 
All I get is an error message :
A problem with the OnClick JavaScript for this button or link was encountered:
Object doesn't support this property or method
 
I've had various other error messages whilst creeping forward with versions of this one, some of them seem to "get stuck" with the a given error message appearing, even after I have modified the code to not actually do anything. Is this a bug?
 
Can anyone help me? You might guess I'm new to this, but I'm not stupid, and I've mimicked code supplied in the Apex code guide (haven't I?). I'd be very grateful.
 
Roger
 
Code for button:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var opportunity= sforce.sObject("Opportunity");
var id sforce.apex.execute("Opportunity_Generate_Shipment","makeShip",{opp:opportunity});
 
code for class:

global class Opportunity_Generate_Shipment {
  WebService static Id makeShip(Opportunity opp) {
    try {
      Shipment__c ship = new Shipment__c ();
      ship.name = 'name';//opp.name, 
      ship.Opportunity__c = opp.id;
      insert ship;
      return ship.id;
    }
    finally{
    }
  }
}

 
(the smiley that appears in the code is actually a colon. Why would I ever want to include a **** smiley in any message?)


Message Edited by WrogWrog on 12-12-2008 07:18 AM
I was hoping to log someone into Salesforce.com, retrieve their userinfo and then open a salesforce.com page for them using the sessionid from the api.
Is this possible?
Rgds,
Brian
Is it possible to use the api to log into http://apps.salesforce.com
 
I want to log in as a partner into PRM and retrieve some information using the API
 
thanks,
Brian


Message Edited by briano on 11-07-2008 08:42 AM