0

I've setup a JavaScript button on a custom object Prospect__c that creates a new Contact record and updates the current Prospect__c record. Everything works as expected except the redirect URL right at the end. It ends up in "myorg.salesforce.com/Undefined" with a URL no longer exists message on the page. I also have a lookup field that will link to the newly created Contact record, which is also empty at the moment, as I'm not able to get the Id back and use it - either in the URL or the Lookup field. Any ideas? Thanks

{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")}
var Prospect__c = new sforce.SObject("Prospect__c"); 
   Prospect__c.ID = '{!Prospect__c.Id}';   
   Prospect__c.converted__c = 'True';
   dt= new Date();
   dt.setDate(dt.getDate()+0);
   Prospect__c.converted_date__c = dt;
var result = sforce.connection.update([Prospect__c]);


var newRecord = [];
{
   var Con1 = new sforce.SObject("Contact");

   Con1.OwnerId = '{!Prospect__c.OwnerId}';
   Con1.LastName = '{!Prospect__c.contact_name__c}';
   Con1.Locale__c = '{!Prospect__c.locale__c}';
   Con1.Email = '{!Prospect__c.email__c}';
   Con1.Phone = '{!Prospect__c.phone__c}';
   Con1.Description = '{!Prospect__c.description__c}';
   Con1.Billing_Zip_Code__c = '{!Prospect__c.Billing_Zip_Code__c}';
   Con1.Billing_Street__c = '{!Prospect__c.Billing_Street__c}';
   Con1.Billing_Country__c = '{!Prospect__c.Billing_Country__c}';
   Con1.Billing_City__c = '{!Prospect__c.Billing_City__c}';

   newRecord.push(Con1);
}
var result2 = sforce.connection.create(newRecord);
console.log(result2[0]);
if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = "/" + result2.Id;
}

In case you're interested, I also tried this approach

1

You have a very minor mistake in your code. It is result2[0].id not result2.Id

var result2 = sforce.connection.create(newRecord);
console.log(result2[0]);
if (result[0].success == 'false') {
    alert(result[0].errors.message);
} else {
   window.parent.location = "/" + result2[0].id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.