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
Ronnie Paton 8Ronnie Paton 8 

custom convert button on Custom object.

HI,

I have created a custom object called "Prospects" and am trying to have a custom oneclick button that once clicked creates an account, opportunity & contacts.

I have the following code

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var ProspectObj = new sforce.SObject("Prospect__c"); 
   ProspectObj.ID = '{!Prospect__c.Id}';   
   ProspectObj.Converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   ProspectObj.Converted_Date__c = dt;
var result = sforce.connection.update([ProspectObj]);
var status = "{!Prospect__c.Status__c}";
var newRecords = [];
{
   var acct = new sforce.SObject("Account");
   acct.OwnerId = '{!Prospect__c.OwnerId}';
   acct.Name = '{!Prospect__c.Name}';
   acct.Company_Name__c = '{!Prospect__c.Name}';
   acct.Phone = '{!Prospect__c.Phone_1__c }';
   acct.Website = '{!Prospect__c.Website__c}';
   acct.ShippingStreet = '{!Prospect__c.Street__c}';
   acct.ShippingCity = '{!Prospect__c.City__c}';
   acct.ShippingState = '{!Prospect__c.State__c }';
   acct.ShippingPostalCode = '{!Prospect__c.Postcode__c}';
   acct.Type = 'Prospect';
newRecords.push(acct);
}
var result = sforce.connection.create(newRecords);

var newRecords = [];
{
   var opp = new sforce.SObject("Opportunity");
   opp.AccountId = '{!Account.Id}';
   opp.OwnerId = '{!Prospect__c.OwnerId}';
   opp.Name = '{!Prospect__c.Description__c}';
   opp.LeadSource = 'Converted from Prospect';
   opp.StageName = 'Perception Analysis';
   opp.Type = 'New Business';
   opp.Prospect_First_Contact_Made__c = 'True';
   opp.Prospect_Initial_Meeting_Scheduled__c = 'True';
   opp.Prospect_Opportunity_Identified__c = 'True';
   opp.Prospect_Requirements_Documented__c = 'True';
   opp.Qualified_Need_to_Buy_Confirmed__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+30);
   opp.CloseDate = dt;
newRecords.push(opp);
}
var result = sforce.connection.create(newRecords);

if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = '/006?fcf=00Bb0000003n9OG';
}

the account is cretaed fine and all fields map over as expected, The opportunity is created but not linked to the account and the account field is blank.

I have tried to create the contacts but they are not created and want to work on 1 thing at a time but the code I addedd is the following

var newRecords = [];
{
   var Con1 = new sforce.SObject("Contact");
   Con1.OwnerId = '{!Prospect__c.OwnerId}';
   Con1.Account = '{!Prospect__c.Name}';
   Con1.FirstName = '{!Prospect__c.First_Name_1__c }';
   Con1.LastName = '{!Prospect__c.Last_Name_1__c}';
   Con1.Title = '{!Prospect__c.Title_1__c}';
   Con1.Email = '{!Prospect__c.Email_1__c}';
   Con1.Phone = '{!Prospect__c.Phone_1__c}';
   Con1.MobilePhone = '{!Prospect__c.Mobile_1__c}';
   Con1.DDI__c = '{!Prospect__c.DDI_1__c}';

newRecords.push(Con1);
}  

I do have 5 sets of above (Con2, Con3, etc) that might no be populated  

I don't get any errors displayed.

Thanks in advance

Ronnie
Best Answer chosen by Ronnie Paton 8
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code below:
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var ProspectObj = new sforce.SObject("Prospect__c"); 
   ProspectObj.ID = '{!Prospect__c.Id}';   
   ProspectObj.Converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   ProspectObj.Converted_Date__c = dt;
var result = sforce.connection.update([ProspectObj]);
var status = "{!Prospect__c.Status__c}";
var newRecords = [];
{
   var acct = new sforce.SObject("Account");
   acct.OwnerId = '{!Prospect__c.OwnerId}';
   acct.Name = '{!Prospect__c.Name}';
   acct.Company_Name__c = '{!Prospect__c.Name}';
   acct.Phone = '{!Prospect__c.Phone_1__c }';
   acct.Website = '{!Prospect__c.Website__c}';
   acct.ShippingStreet = '{!Prospect__c.Street__c}';
   acct.ShippingCity = '{!Prospect__c.City__c}';
   acct.ShippingState = '{!Prospect__c.State__c }';
   acct.ShippingPostalCode = '{!Prospect__c.Postcode__c}';
   acct.Type = 'Prospect';
newRecords.push(acct);
}
var result = sforce.connection.create(newRecords);

if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
}

var newRecords = [];
{
   var opp = new sforce.SObject("Opportunity");
   opp.AccountId =  result[0].id;
   opp.OwnerId = '{!Prospect__c.OwnerId}';
   opp.Name = '{!Prospect__c.Description__c}';
   opp.LeadSource = 'Converted from Prospect';
   opp.StageName = 'Perception Analysis';
   opp.Type = 'New Business';
   opp.Prospect_First_Contact_Made__c = 'True';
   opp.Prospect_Initial_Meeting_Scheduled__c = 'True';
   opp.Prospect_Opportunity_Identified__c = 'True';
   opp.Prospect_Requirements_Documented__c = 'True';
   opp.Qualified_Need_to_Buy_Confirmed__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+30);
   opp.CloseDate = dt;
newRecords.push(opp);
}
sforce.connection.create(newRecords);

var newRecords = [];
{
   var Con1 = new sforce.SObject("Contact");
   Con1.AccountId =  result[0].id;
   Con1.OwnerId = '{!Prospect__c.OwnerId}';
   Con1.FirstName = '{!Prospect__c.First_Name_1__c}';
   Con1.LastName = '{!Prospect__c.Last_Name_1__c}';
   Con1.Title = '{!Prospect__c.Title_1__c}';
   Con1.Email = '{!Prospect__c.Email_1__c}';
   Con1.Phone = '{!Prospect__c.Phone_1__c}';
   Con1.MobilePhone = '{!Prospect__c.Mobile_1__c}';
   Con1.DDI__c = '{!Prospect__c.DDI_1__c}';
   newRecords.push(Con1);
}
sforce.connection.create(newRecords);

if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = '/006?fcf=00Bb0000003n9OG';
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code below:
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var ProspectObj = new sforce.SObject("Prospect__c"); 
   ProspectObj.ID = '{!Prospect__c.Id}';   
   ProspectObj.Converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   ProspectObj.Converted_Date__c = dt;
var result = sforce.connection.update([ProspectObj]);
var status = "{!Prospect__c.Status__c}";
var newRecords = [];
{
   var acct = new sforce.SObject("Account");
   acct.OwnerId = '{!Prospect__c.OwnerId}';
   acct.Name = '{!Prospect__c.Name}';
   acct.Company_Name__c = '{!Prospect__c.Name}';
   acct.Phone = '{!Prospect__c.Phone_1__c }';
   acct.Website = '{!Prospect__c.Website__c}';
   acct.ShippingStreet = '{!Prospect__c.Street__c}';
   acct.ShippingCity = '{!Prospect__c.City__c}';
   acct.ShippingState = '{!Prospect__c.State__c }';
   acct.ShippingPostalCode = '{!Prospect__c.Postcode__c}';
   acct.Type = 'Prospect';
newRecords.push(acct);
}
var result = sforce.connection.create(newRecords);

if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
	return;
}

var newRecords = [];
{
   var opp = new sforce.SObject("Opportunity");
   opp.AccountId =  result[0].id;
   opp.OwnerId = '{!Prospect__c.OwnerId}';
   opp.Name = '{!Prospect__c.Description__c}';
   opp.LeadSource = 'Converted from Prospect';
   opp.StageName = 'Perception Analysis';
   opp.Type = 'New Business';
   opp.Prospect_First_Contact_Made__c = 'True';
   opp.Prospect_Initial_Meeting_Scheduled__c = 'True';
   opp.Prospect_Opportunity_Identified__c = 'True';
   opp.Prospect_Requirements_Documented__c = 'True';
   opp.Qualified_Need_to_Buy_Confirmed__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+30);
   opp.CloseDate = dt;
newRecords.push(opp);
}
var result = sforce.connection.create(newRecords);

if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = '/006?fcf=00Bb0000003n9OG';
}

This line opp.AccountId = '{!Account.Id}'; it is wrong because you have to get the Id from Account's insert result. As per your contacts please share you entire code so we can have a look.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Ronnie Paton 8Ronnie Paton 8
Thanks for getting back to me, with that code in place I get an error saysing 

"illegal return Statment"

Any ideas?

 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Please just remove the return in the code below:
 
if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
	return;
}

Correct:
 
if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Ronnie,

Have you solved your problem?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Ronnie Paton 8Ronnie Paton 8
HI Zuinglio,

Thanks for all your help so far.

Yes removing the "return" solved the issue so can now have the opp and account linked.

The next problem I have is the contacts I keep getting an Account ID value of incorrect type error below is the code

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var ProspectObj = new sforce.SObject("Prospect__c"); 
   ProspectObj.ID = '{!Prospect__c.Id}';   
   ProspectObj.Converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   ProspectObj.Converted_Date__c = dt;
var result = sforce.connection.update([ProspectObj]);
var status = "{!Prospect__c.Status__c}";
var newRecords = [];
{
   var acct = new sforce.SObject("Account");
   acct.OwnerId = '{!Prospect__c.OwnerId}';
   acct.Name = '{!Prospect__c.Name}';
   acct.Company_Name__c = '{!Prospect__c.Name}';
   acct.Phone = '{!Prospect__c.Phone_1__c }';
   acct.Website = '{!Prospect__c.Website__c}';
   acct.ShippingStreet = '{!Prospect__c.Street__c}';
   acct.ShippingCity = '{!Prospect__c.City__c}';
   acct.ShippingState = '{!Prospect__c.State__c }';
   acct.ShippingPostalCode = '{!Prospect__c.Postcode__c}';
   acct.Type = 'Prospect';
newRecords.push(acct);
}
var result = sforce.connection.create(newRecords);

if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
}

var newRecords = [];
{
   var opp = new sforce.SObject("Opportunity");
   opp.AccountId =  result[0].id;
   opp.OwnerId = '{!Prospect__c.OwnerId}';
   opp.Name = '{!Prospect__c.Description__c}';
   opp.LeadSource = 'Converted from Prospect';
   opp.StageName = 'Perception Analysis';
   opp.Type = 'New Business';
   opp.Prospect_First_Contact_Made__c = 'True';
   opp.Prospect_Initial_Meeting_Scheduled__c = 'True';
   opp.Prospect_Opportunity_Identified__c = 'True';
   opp.Prospect_Requirements_Documented__c = 'True';
   opp.Qualified_Need_to_Buy_Confirmed__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+30);
   opp.CloseDate = dt;
newRecords.push(opp);
}
var result = sforce.connection.create(newRecords);

var newRecords = [];
{
   var Con1 = new sforce.SObject("Contact");
   Con1.AccountId =  result[0].id;
   Con1.OwnerId = '{!Prospect__c.OwnerId}';
   Con1.FirstName = '{!Prospect__c.First_Name_1__c}';
   Con1.LastName = '{!Prospect__c.Last_Name_1__c}';
   Con1.Title = '{!Prospect__c.Title_1__c}';
   Con1.Email = '{!Prospect__c.Email_1__c}';
   Con1.Phone = '{!Prospect__c.Phone_1__c}';
   Con1.MobilePhone = '{!Prospect__c.Mobile_1__c}';
   Con1.DDI__c = '{!Prospect__c.DDI_1__c}';
newRecords.push(Con1);
}
var result = sforce.connection.create(newRecords);

if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = '/006?fcf=00Bb0000003n9OG';
}


I will have 5 sets of the above (Con1, Con2, Con3, Con4, Con5) but only Con1 will have info in all the time. I have not added the rest of the Contact mapping as wanted to get the 1st one sorted first.

Once again thanks for all your help it is very appreciated  


 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code below:
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var ProspectObj = new sforce.SObject("Prospect__c"); 
   ProspectObj.ID = '{!Prospect__c.Id}';   
   ProspectObj.Converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   ProspectObj.Converted_Date__c = dt;
var result = sforce.connection.update([ProspectObj]);
var status = "{!Prospect__c.Status__c}";
var newRecords = [];
{
   var acct = new sforce.SObject("Account");
   acct.OwnerId = '{!Prospect__c.OwnerId}';
   acct.Name = '{!Prospect__c.Name}';
   acct.Company_Name__c = '{!Prospect__c.Name}';
   acct.Phone = '{!Prospect__c.Phone_1__c }';
   acct.Website = '{!Prospect__c.Website__c}';
   acct.ShippingStreet = '{!Prospect__c.Street__c}';
   acct.ShippingCity = '{!Prospect__c.City__c}';
   acct.ShippingState = '{!Prospect__c.State__c }';
   acct.ShippingPostalCode = '{!Prospect__c.Postcode__c}';
   acct.Type = 'Prospect';
newRecords.push(acct);
}
var result = sforce.connection.create(newRecords);

if (!result[0].getBoolean("success")) {
    alert(result[0].errors.message);
}

var newRecords = [];
{
   var opp = new sforce.SObject("Opportunity");
   opp.AccountId =  result[0].id;
   opp.OwnerId = '{!Prospect__c.OwnerId}';
   opp.Name = '{!Prospect__c.Description__c}';
   opp.LeadSource = 'Converted from Prospect';
   opp.StageName = 'Perception Analysis';
   opp.Type = 'New Business';
   opp.Prospect_First_Contact_Made__c = 'True';
   opp.Prospect_Initial_Meeting_Scheduled__c = 'True';
   opp.Prospect_Opportunity_Identified__c = 'True';
   opp.Prospect_Requirements_Documented__c = 'True';
   opp.Qualified_Need_to_Buy_Confirmed__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+30);
   opp.CloseDate = dt;
newRecords.push(opp);
}
sforce.connection.create(newRecords);

var newRecords = [];
{
   var Con1 = new sforce.SObject("Contact");
   Con1.AccountId =  result[0].id;
   Con1.OwnerId = '{!Prospect__c.OwnerId}';
   Con1.FirstName = '{!Prospect__c.First_Name_1__c}';
   Con1.LastName = '{!Prospect__c.Last_Name_1__c}';
   Con1.Title = '{!Prospect__c.Title_1__c}';
   Con1.Email = '{!Prospect__c.Email_1__c}';
   Con1.Phone = '{!Prospect__c.Phone_1__c}';
   Con1.MobilePhone = '{!Prospect__c.Mobile_1__c}';
   Con1.DDI__c = '{!Prospect__c.DDI_1__c}';
   newRecords.push(Con1);
}
sforce.connection.create(newRecords);

if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = '/006?fcf=00Bb0000003n9OG';
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
Ronnie Paton 8Ronnie Paton 8
THi,

That worked for me..

Thanks for all your help in writting this code.