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
Dale WorthingtonDale Worthington 

Using JS popup to fill a field and save a record

Hi All

Please excuse my noviceness here. I have found some blocks of JS which im trying to use for my solution. 
The goal of this is to have a button that the user can click which will prompt them with a popup to provide text, upon providing the text the user will click OK, the text will be placed into 'Reason__c' and the other fields will be updated with the predefined values. 
The record should then save.

So far i have.
 
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}

var reason = prompt("Enter reason", "");
if (reason != null) {
    var opportunity = new sforce.SObject("Opportunity");
    opportunity.id = "{!Opportunity.Id}";
    opportunity.StageName = "Closed Lost";
    opportunity.Loss_Reason_Select1__c = "Reason6";
    opportunity.Reason__c = reason;

var result = sforce.connection.update([opportunity]);

if (result[0].getBoolean("success"))
{
    // Refresh window
    window.location.reload();
}
else
{
    alert("Error saving opportunity");
}

Any help or pointers would be grealy appreicated. 

Thanks in advance
Dale 
Best Answer chosen by Dale Worthington
Pankaj_GanwaniPankaj_Ganwani
Hi Dale,

Just add one more '}' at the end. This will fix your issue.
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}

var reason = prompt("Enter reason", "");
if (reason != null) {
    var opportunity = new sforce.SObject("Opportunity");
    opportunity.id = "{!Opportunity.Id}";
    opportunity.StageName = "Closed Lost";
    opportunity.Loss_Reason_Select1__c = "Reason6";
    opportunity.Reason__c = reason;

var result = sforce.connection.update([opportunity]);

if (result[0].getBoolean("success"))
{
    // Refresh window
    window.location.reload();
}
else
{
    alert("Error saving opportunity");
}
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Dale,

Just add one more '}' at the end. This will fix your issue.
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}

var reason = prompt("Enter reason", "");
if (reason != null) {
    var opportunity = new sforce.SObject("Opportunity");
    opportunity.id = "{!Opportunity.Id}";
    opportunity.StageName = "Closed Lost";
    opportunity.Loss_Reason_Select1__c = "Reason6";
    opportunity.Reason__c = reason;

var result = sforce.connection.update([opportunity]);

if (result[0].getBoolean("success"))
{
    // Refresh window
    window.location.reload();
}
else
{
    alert("Error saving opportunity");
}
}

 
This was selected as the best answer
Dale WorthingtonDale Worthington
Pankaj_Ganwani,

I cannot believe it. What a clown.

Cheers
Dale