+ Start a Discussion
RobotiRoboti 

How to update a field on a custom object using javascript in a custom button

Hi,

 

We have a custom button on a custom object. We click on this button and the javascript performs the actions based on a field in this custom object. The field is a checkbox field and it has default value as unchecked. Like this - 

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

If field A = 0 (unchecked)

     perform AAA

else

     perform BBB

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

But we want to update the field value (checkbox should become checked) when if statement encounters the value = 0

 

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

If field A = 0 (unchecked)

     update A = 1

     perform AAA

else

     perform BBB

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

How can we achieve this?

Thanks in advance!

 

Best regards

Best Answer chosen by Admin (Salesforce Developers) 
Anil SavaliyaAnil Savaliya

Hi ,

 

Please use ajax developer toolkit guide.

http://www.salesforce.com/us/developer/docs/ajax/apex_ajax.pdf

 

See in Page 16,and follow.I hope ,It solve your problem.

All Answers

Ashish_SFDCAshish_SFDC

Hi, 

 

So if the Field is unchecked you have to perform BBB and also Check the Same Check Box, is it what you are looking for?

 

If that is the case then at the end of perform BBB you have to add another statement update field value = true. 

 

Regards,

Ashish

 

 

Anil SavaliyaAnil Savaliya

Hi ,

 

Please use ajax developer toolkit guide.

http://www.salesforce.com/us/developer/docs/ajax/apex_ajax.pdf

 

See in Page 16,and follow.I hope ,It solve your problem.

This was selected as the best answer
RobotiRoboti
Hi Ashish,

No, if field is unchecked, we have to perform AAA and also check the field.
If the field is checked, we have to perform BBB.
I hope this clarifies it.

Thanks!
Ashish_SFDCAshish_SFDC

Hi Roboti,

 

You have to use a Visualforce page and an APEX controller extention that has this logic in it. 

 

See below for some similar sample. 

 

http://salesforce.stackexchange.com/questions/17923/rerender-when-calling-a-method-from-an-inputcheckbox

 

Regards,

Ashish

RobotiRoboti
Hi Ashish,

Thanks for your reply! We do not have resources to use and maintain VF and APEX part. So we are looking for a solution which can be achieved without these.

Regards
RobotiRoboti

Hi, We are using following in the custom button on a custom object Sales__c.

 

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}&an={!Sales__c.OpportunityAccountName__c}%';

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

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

When we click on the button, it always shows the alert, no matter if checkbox is checked or unchecked. Also, it does not update the checkbox from unchecked to checked.

It is always coming to "else" part.

 

Please help. Thanks!

 

Regards,

RobotiRoboti

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

Ashish_SFDCAshish_SFDC

Hi Roboti, 

 

Salesforce processes the record upon DML action, i believe the code is processing the If Part - Coming out of loop and validating the condition again and going to the else part. 

 

Which means it has a sequence which is looping itself to the else part everytime. 

 

I would suggest you to use another Check box field to be checked, update new field = 1. 

 

Regards,

Ashish

 

 

RobotiRoboti

Hi Ashish,

 

We have created one more checkbox (OppUp2__c)on the object. And here is our Javascript code for Custom button.

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

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.OppUp2__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...
}

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

But it is also not working.

We appreciate your quick help on this!

 

Thanks and regards

Ashish_SFDCAshish_SFDC

Hi Roboti, 

 

Looks like Windows.location is breaking the execution thread.  

 

Use window.location.replace("http://example.com");

 

Regards,

Ashish

RobotiRoboti

Hi Ashish,

 

The problem is solved. We put the alert after each line of code and checked where it was going wrong. The code was not updating the record in the system. When we used the id we got it working. The line which did the trick, is in bold. 

 

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}

var currurl = window.location.href;
var oppUp ="{!Sales__c.OppUp__c}";

if(oppUp == 1){
alert('Already created.');
}
else
{

window.location = 'http://xxx.yyy.com/Sales?OpenAgent&id={!Sales__c.OpportunityId__c}&oo={!Sales__c.OpportunityOwner__c}%;
var stc = new sforce.SObject("Sales__c");
stc.id="{!Sales__c.Id}";
stc.OppUp__c=1;
result = sforce.connection.update([stc]);
}

 

Thanks for the help!

Regards

Ashish_SFDCAshish_SFDC

Hi Roboti, 

 

Thats Awesome!!!, 

 

Thanks for letting me know and updating on the board as it might help someone in need. 

 

Regards,
Ashish

 

RobotiRoboti

Hi Ashish,

 

We had missed the line containing the ID from AJAX toolkit guide. When we put that line, we got the code working.

Thanks to all who provided solutions here.

 

Regards

JaanuJaanu
How to set the check box field to value "1" (true) inside the if clause pls ? I am having big trouble with this...