+ Start a Discussion
RobotiRoboti 

How to update a checkbox field on a Custom object through a Custom button

Hi,

 

Can anyone correct the code? We are having two issues here.

1. The code is not checking the "IF" condition here. It is directly going to "ELSE" part.

2. We are not able to update a checkbox field from unchecked to checked.

 

---------------------------------------------------------------------------------------------------------------------------------------------------

var currurl = window.location.href;
var stc = new sforce.SObject("Sales__c");

if(stc.OppUpdate__c = 0){
window.location = 'http://xxx.yyy.com/Sales?OpenAgent&id={!Sales__c.OpportunityId__c}&oo={!Sales__c.OpportunityOwner__c...

stc.OppUpdate__c = 1;
result = sforce.connection.update([stc]);
}else{
alert('Already created. You should not create it again');
}

---------------------------------------------------------------------------------------------------------------------------------------------------

We are not very experienced in Salesforce and we have never used APEX, Javascript etc. So it is the tough one here.

We will really appreciate the help.

 

Thanks!

 

Regards

Deepa.B.AnkaliDeepa.B.Ankali
@Roboti,

Try using this : if(stc.OppUpdate__c == 0) instead of if(stc.OppUpdate__c = 0)
RobotiRoboti

Hi Deepa,

 

We have corrected it with the solution provided by you. But it is also not working.

 

---------------------------------------------------------------------------------------------------------------------------------------------------

var currurl = window.location.href;
var stc = new sforce.SObject("Sales__c");

if(stc.OppUpdate__c == 0){
window.location = 'http://xxx.yyy.com/Sales?OpenAgent&id={!Sales__c.OpportunityId__c}&oo={!Sales__c.OpportunityOwner__c...

stc.OppUpdate__c = 1;
result = sforce.connection.update([stc]);
}else{
alert('Already created. You should not create it again');
}

---------------------------------------------------------------------------------------------------------------------------------------------------

Thanks!

Regards

RobotiRoboti

We have modified the code as following.

 

---------------------------------------------------------------------------------------------------------------------------------------------------

var currurl = window.location.href;
var stc = new sforce.SObject("Sales__c");

if(stc.OppUpdate__c == 1)

{
alert('Already created. You should not create it again');
}

else

{
stc.OppUpdate__c = 1;
result = sforce.connection.update([stc]);

window.location = 'http://xxx.yyy.com/Sales?OpenAgent&id={!Sales__c.OpportunityId__c}&oo={!Sales__c.OpportunityOwner__c...
}

---------------------------------------------------------------------------------------------------------------------------------------------------

Now, it is directly going to the URL of the external system. No matter if the stc.OppUpdate__c is checked (1) or unchecked (0). Also, if it is unchecked (0), the code is not updating it with checked (1) value.

 

Please help!

Thanks and regards