You need to sign in to do that
Don't have an account?
Nicky Torstensson
How Do You Test LeadConvert, When In UI Selecting Existing Contact, But New Account Should Be Created
Hi
How do you test the Lead Convert where you have selected the following from the UI:
How do you test the Lead Convert where you have selected the following from the UI:
- New account should be created
- Existing contact is selected
- No opportunities
public static testMethod void myTest(){ Contact existingContact = TestUtil.createContact(true); Lead ld = TestUtil.createLead(false); ld.FirstName = 'FirstName'; ld.LastName = 'LastName'; ld.ContactPersonFirstName__c = 'NOT FirstName'; ld.ContactPersonLastName__c = 'NOT LastName'; insert ld; Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(ld.id); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); // This is where I chose the existing contact, but as I want a new account created, then I do not specify an account id lc.setContactId(existingContact.Id); Test.startTest(); Database.LeadConvertResult lcr = null; User testUserContext = TestUtil.getTestContextUser(true); System.runAs(testUserContext) { // Here is fails lcr = Database.convertLead(lc); } Test.stopTest();I get the error: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, accountId must be specified if contactId is not null: [Id]
The workaround is to change the AccountId of the existing Contact twice (with all the possible effects).
That works at the end as expected but it is a series of workarounds and the double changes of AccountId for the existing contact could be problematic in some cases ( irreversible processes ).
It should be interesting to test another forum ( stackexchange ) with the same question which seems a basic case for the conversion of leads in order to know if they also use these kind of workarounds because of the strong contrainsts in Apex code.
https://salesforce.stackexchange.com/
All Answers
Did you try this?
Even if I don't know if you have "sons".
Thanks for looking into it. However, the issue does not lie in the missing (required) field Company. The TestUtil class creates the lead properly.
The issue lies in testing the lead convert, where you choose an existing contact (see below screenshot for clarity), but choose a new account to be created
I you add ld.Company='Nicky Torstensson And Sons'; that will create the account from the lead.
I made the tests and that works fine.
You must choose at least the name of the new account in the lead.
The TestUtil Class sets the value of Company = 'Test Company';. This is not where the issue lies.
Please note, the issue is not about creating the lead, it comes at the 3 line from the bottom of my sample code. Have you been able to test that line?
The basic code below works fine in a free developer org:
The account is well created with the name 'Fry And Sons'
Contact existingContact = new Contact(FirstName='titi',lastname='toto');
insert existingContact;
My case is different.
This was also the conclusion I came to via the link to the documentation in my original post.
And to your response "If you want a new account and you need to map the missing fields with new lines of code probably.", I agree, but how? This is what my question is about.
A simple work-around is to create a new account with the required fields equals to the existing fields in the lead (here Account.Name)
The problem is to delete new_company is the conversion failed at least.
To understand my business rationale: Upon lead convert I am updating account contact relations (specifically the roles), based on lead fields. Hence if I choose an existing contact, I still want a new account to be created, but the existing contact gets a role (AccountContactRelation) assigned to the new account, but has it's Primary Account (the Contact.AccountId) unchanged.
if (!lcr.isSuccess()) { delete new_company; }
.. means "if the conversion is unsuccessful then delete the new_company."
It is not a "dummy' company. At the end of the process, you want a new company but you can create it at first like above.
The only contrainsts is to create the new account with the exact same values (when they are required) as the lead uses, here the "name") because these fields are not overriden during the lead conversion.
But then it does not fit my request. I want the lead to be converted to an existing contact (which of course is related to an existing account) and to a new account. The test will not simulate this - on the contrary, it simulates that upon convert I select an existing account and contact (with them being related to each other.
The workaround is to change the AccountId of the existing Contact twice (with all the possible effects).
That works at the end as expected but it is a series of workarounds and the double changes of AccountId for the existing contact could be problematic in some cases ( irreversible processes ).
It should be interesting to test another forum ( stackexchange ) with the same question which seems a basic case for the conversion of leads in order to know if they also use these kind of workarounds because of the strong contrainsts in Apex code.
https://salesforce.stackexchange.com/