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
Sean NapierSean Napier 

Button on Custom Object to edit field on Opportunity

Hi all,

I'm a bit new to this so I apologize if this is not even possible.

I created a Button on a custom Object I have with the intention of having a checkbox on the related Opportunity updated when the button is clicked. This is what I have:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

// identify the record

var o = new sforce.SObject("Opportunity");

o.id = "{!Medical_Consult__c.Patient__c}"; // Medical_Consult__c is my custom Object name Patient__c is the Opportunity lookup

// make the field change

o.Consult_Document_Fully_Completed__c = true; //This is the field on the Opportunity

// save the change

sforce.connection.update([o]);

//refresh the page

window.location.reload();

I'm not getting any errors when I click this button but the field is not updated on the Opportunity.

Any help would be greatly appreciated.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Hi Sean,
try like this,
{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
var __sfdcSessionId = '{!GETSESSIONID()}';
var result='';
try {
var strQuery="Select Id from Opportunity where Patient__c= '" +'{!Medical_Consult__c.Id}'+"'";
var newRecords = [];
var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
if (records.length >= 1) {
for(var i=0; i < records.length ; i ++) {
var ob = new sforce.SObject("Opportunity");
ob.Id = records[i].Id;
ob.Consult_Document_Fully_Completed__c = true;
result += sforce.connection.update([ob]);
}
alert(result);
location.reload(true);
}
}catch(er) {
alert(er);
}

Let us know if it helps,

Thanks,
Balayesu
Sean NapierSean Napier
Thank you very much for helping on this. I updated to what you posted above and get the following error (image)User-added image
Sean NapierSean Napier
Thank you again. It looks like it is just about there but is trying to use the Opportunity name not the IDUser-added image

I thought maybe I could create a custom field to hold the Opp ID after getting the above so I updated it to:
var strQuery="Select Id,Consult_Document_Fully_Completed__c from Opportunity where Id= '" +'{!Medical_Consult__c.OppID__c }'+"'";

After doing that I do not recieve the above error but nothing happens when I click the button.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try your query like this,
var strQuery="Select Id,Consult_Document_Fully_Completed__c from Opportunity where Name= '" +'{!Medical_Consult__c.Patient__c}'+"'";

Let us know if it helps.
Sean NapierSean Napier
You are a rockstar! Thank you so much! I now have the below and it works like a champ:

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
var __sfdcSessionId = '{!GETSESSIONID()}';
var result='';
try {
var strQuery="Select Id,Consult_Document_Fully_Completed__c from Opportunity where Name= '" +'{!Medical_Consult__c.Patient__c}'+"'";
var newRecords = [];
var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
if (records.length >= 1) {
for(var i=0; i < records.length ; i ++) {
var ob = new sforce.SObject("Opportunity");
ob.Id = records[i].Id;
ob.Consult_Document_Fully_Completed__c = true;
result += sforce.connection.update([ob]);
}
alert("You have Successfully Locked the Consult");
location.reload(true);
}
}catch(er) {
alert(er);
}

Have an awesome day!
Andray GaustAndray Gaust
Can we keep record of Chronic Diseases (https://www.sf-healing.com/) with that type of API of medical?