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
BillPowell__cBillPowell__c 

Javascript Button Not Updating Record

I have a lookup field on my Opportunity object that looks up to a custom object "Projects" (so we can see all of the opportunities associated with a project from different contractors). The # of opportunities are won/lost dynamically (not always 1 won, etc).  

Therefore, I want to create a list button on the Opportunity related list on projects so my users can just check off which ones have "won" and click a "Won" button to set the stage. The button works without error, but doesnt change the stage. Here is the code I have, why is this not working? Because of the relationship? 
 
{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js")}
var o = new sforce.SObject("Opportunity");
o.id = "{!Opportunity.Id}";
o.StageName = "McGrory Won";
result = sforce.connection.update([o]);
window.location.reload();

 
Amit Chaudhary 8Amit Chaudhary 8
I tested same code on detail page layout which is working fine in my developer org. Look like issue is coming because you are using same code in List Button?

Please check below post for same issue
1) https://success.salesforce.com/answers?id=90630000000hiHJAAY
2) http://focusonforce.com/configuration/salesforce-list-buttons-javascript-example/


Try code somthing like this
{!REQUIRESCRIPT('/soap/ajax/35.0/connection.js')}

selectedOpportunityIds = {!GETRECORDIDS($ObjectType.Opportunity)};
OppForUpdate = [];
if (selectedOpportunityIds[0] == null) {
alert("You must select at least one record");
}
else
{
for (var i = 0; i < selectedOpportunityIds.length; i++)
{
var Opportunity = new sforce.SObject("Opportunity");
Opportunity.Id = selectedOpportunityIds[i];
Opportunity.StageName = "McGrory Won";
OppForUpdate.push(Opportunity);
}
}
alert(OppForUpdate);
varsaveResult = sforce.connection.update(OppForUpdate);
location.reload(true);

Let us know if this will help you
BillPowell__cBillPowell__c
No, threw an error about the Stagename.
Amit Chaudhary 8Amit Chaudhary 8
I tested the same code in my org which is working fine. I just added the button under tha account detail page in opportunity related list.

Can you please share the screen shot of your error and let me know where you added above button with screen shot
 
BillPowell__cBillPowell__c
I think the issue is that my related list is a lookup object, not in a master detail
Amit Chaudhary 8Amit Chaudhary 8
What error you are getting in Stagename ?
BillPowell__cBillPowell__c
{type:'Opportunity', Id:'0063A000014kzlj', StageName:'McGrory Won', }
BillPowell__cBillPowell__c
Amit, thanks for your help. I thin i'm going to can the idea of the javascript button. The more I look into it, the more work it will wind up making since I cant transition it to Lightning anyway. Thanks for your help