1

I have a JavaScript button that is meant to set SAPReleased__c = "True" whenever it is pushed. Now this should only work when Approved__c is True. This is my current code which isn't working.. Any thoughts?

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
//Query the record from the DB.
var result = sforce.connection.query("Select Name,Approved__c,SAPReleased__c,Sample_Fullfillment_Channel__c from Sampling__c where Id=\'{!Sampling__c.Id}\'");
var records = result.getArray("records");
//Since we are dealing with one record per button we can just take the 1st val from 
//the records array.
var Sampling = records[0];
if(Sampling.Approved__c){
  Sampling.SAPReleased__c = 'TRUE'; 
  Sampling.Sample_Fullfillment_Channel__c = 'SAP'; 
}
else{
  alert('Sample must be approved before it can be booked.');
}

result = sforce.connection.update([Sampling]);
window.location.reload();

So update to my code I changed it from "True" to 1, in which it appears that it is getting inside of my Loop, now I just need to figure out how to set SAPReleased__c == True. It isn't getting updated how I have it now.

It is running but it isn't setting my check box to "True" Is this right?

Sampling.SAPReleased__c = true; 
  • Are you trying to create new object and then update the newly created record or trying to update an existing record? – Rao Apr 11 '14 at 16:51
  • I'm trying to update the existing – EricSSH Apr 11 '14 at 16:54
1

There are 2 ways to do this, if you do not want to deal with Javascript's case sensitivity you can go down Visual force page route Create button and set content source as Visual force.

Page:

<apex:page standardcontroller="Sampling__c" extensions="SAP_Controller" action="{!set_sap_val}">

</apex:page>

Controller:

public class SAP_Controller{
public Account acct;

public SAP_Controller(ApexPages.StandardController controller) {
        this.acct = (Account)stdController.getRecord();

}

public PageReference set_sap_val{
   if(acct.Approved__c == "Approved"){
      //do whatever.
   }
}

}

Try this :

    {!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
    //Query the record from the DB.
    var result = sforce.connection.query("SELECT Name,Approved__c,Sample_Fullfillment_Channel__c,Sample_Fullfillment_Channel__c from Sampling__c where Id=\'{!Sampling__c.Id}\'");
    var records = result.getArray("records");
   //Since we are dealing with one record per button we can just take the 1st val from 
   //the records array.
    var sampling = records[0];
    if(sampling.Approved__c){
      //for boolean(checkbox) use true without ''. anything inside '' is a string.
      sampling.SAPReleased__c = true; 
      sampling.Sample_Fullfillment_Channel__c = 'SAP'; 
    }
    else{
      alert('Sample must be approved before it can be booked.');
    }

result = sforce.connection.update([sampling]);
window.location.reload();
  • Sorry I should of said Approved__c and SAPReleased__c is a check box, I will give this a try though – EricSSH Apr 11 '14 at 17:22
  • 1
    @EricSSH I just modified his code to change if(sampling.Approved__c == "Approved"){ to if(sampling.Approved__c){ but I don't know if it's come through yet – Nathan Williams Apr 11 '14 at 17:23
  • 2
    @NathanWilliams I was updating the answer :/, I updated the edits on my answer :). – Rao Apr 11 '14 at 17:25
  • So it's running, but I don't think it isn't setting SAPReleased__c, is this right? Sampling.SAPReleased__c = 'TRUE'; It is a checkbox, I have tried 1, and '1' – EricSSH Apr 11 '14 at 17:30
  • @Rao it comes back as as error "True is not defined" I tried true, also doesn't work. – EricSSH Apr 11 '14 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.