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
Surya@soa.comSurya@soa.com 

Problem while Adding Contact using Apex Code? Urgent

 

I'm Trying to add contact using apex code and when i'm running test it is showing successful but Contacts are not adding

Please suggest me the solution soon.....

Here is my Apex Code

 

@isTest
Public class Enterprise{
static testMethod void AddContact()
{
   Account newAccount = new Account (name='XYZ Organization',
BillingCity ='TestCity',
BillingCountry ='TestCountry',
BillingStreet ='TestStreet',
BillingPostalCode ='t3stcd3'
);

insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
Account = newAccount,
Email = 'xyzmail@mail.com'
);
}
}

Best Answer chosen by Admin (Salesforce Developers) 
WaveOCWaveOC
Take a look - i've changed 1 line of code and added 1 line
global without sharing class ContactUtil {
  webService static Boolean AddContact(String AcName) {
    
    try {
      Account newAccount = new Account (name = AcName,
BillingCity ='TestCity',
BillingCountry ='TestCountry',
BillingStreet ='TestStreet',
BillingPostalCode ='t3stcd3'
);

insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
//line changed - waveoc
AccountId = newAccount.Id,
Email = 'xyzmail@mail.com'
);

//line added - waveoc
insert newContact;

      return true;
    } catch (Exception e) {
      return false;
  }
}

}
I've save this as a class within my org and executed from developer console using - ContactUtil.AddContact('test1233'); and it worked

All Answers

WaveOCWaveOC
You are not inserting the contact? also newAccount.Id need to be used instead of newAccount.
Surya@soa.comSurya@soa.com

Thanks for your reply... Even though after making changes as u said both Account as well as contact are not getting added.....Kindly reply soon

WaveOCWaveOC
So you want to insert records to Database from test class? You can not add data in Database within the @Test class only using default apex class or developer console or execute anonymous from IDE
Surya@soa.comSurya@soa.com

Thanks wake OS for ur Fast reply....

 

Here  is my Requirement....

 

i want to add contact  using my apex code......

 

please can u suggest me the code that i need to write on my developer console,....

 

Please kindly help me i'm very new to this salesforce world.....

WaveOCWaveOC
This worked for me
Account a = new Account(Name = 'test');
insert a;
Contact c = new Contact(
	FirstName = 'FName',
	LastName = 'LName',
	Email = 'demo@email.com',
	AccountId = a.Id);
insert c;
Surya@soa.comSurya@soa.com

Thanks for ur reply waveOS ...when i copy this code in my developer console it is throwing error "UNEXPECTED TOKEN ACCOUNT"

WaveOCWaveOC
I am not sure what is the issue - could you please give a screenshot?
Surya@soa.comSurya@soa.com

Heyy thank you so much for your help WAVE OC.....it's working fine now but i need to create a wsdl for this apex code so that i can use it in tibco framework....

 

Please help me waveOC in  building wsdl

WaveOCWaveOC
Try this - go to setup page and then "Develop" --> "API" and there you will find the list of all WSDL that may be generated by your org.
Surya@soa.comSurya@soa.com

THANKS FOR YOUR REPLY WAVEOC.....

 

i generated  wsdl file using apex class as shown below but when i'm invoking from tibco i'm getting response as "TRUE" but contact is not  getting created in salesforce....Please suggest me soon waveOC

 

 

 

global without sharing class ContactUtil {
  webService static Boolean AddContact(String AcName) {
    
    try {
      Account newAccount = new Account (name = AcName,
BillingCity ='TestCity',
BillingCountry ='TestCountry',
BillingStreet ='TestStreet',
BillingPostalCode ='t3stcd3'
);

insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
Account = newAccount,
Email = 'xyzmail@mail.com'
);
      return true;
    } catch (Exception e) {
      return false;
  }
}

}

WaveOCWaveOC
Take a look - i've changed 1 line of code and added 1 line
global without sharing class ContactUtil {
  webService static Boolean AddContact(String AcName) {
    
    try {
      Account newAccount = new Account (name = AcName,
BillingCity ='TestCity',
BillingCountry ='TestCountry',
BillingStreet ='TestStreet',
BillingPostalCode ='t3stcd3'
);

insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
//line changed - waveoc
AccountId = newAccount.Id,
Email = 'xyzmail@mail.com'
);

//line added - waveoc
insert newContact;

      return true;
    } catch (Exception e) {
      return false;
  }
}

}
I've save this as a class within my org and executed from developer console using - ContactUtil.AddContact('test1233'); and it worked
This was selected as the best answer
Surya@soa.comSurya@soa.com

Thanks alot waveOC for your valuable suggestions ..It's working fine now.....