+ Start a Discussion
NishanNishan 

Custom button and APIs

Hi everybody,

                     I am new to force.com. So I dont know much about it. Here is what I want to achieve.

 

 I have an object 'Inventory' with a custom read-only field 'Stock' and a custom button 'Add'. When 'Add' is pressed a number is input from the keyboard via prompt. Then 'Stock' should be updated to 'Stock + the input number'.

 

                 Here is my code

 

{!requireScript("/soap/ajax/26.0/connection.js")}

var url = parent.location.href;

var input=prompt("Enter the number");

var customObj = sforce.connection.query("SELECT Id FROM Inventory__c WHERE
Id='{!Inventory__c.Id}'");

customobj.Stock__c=customobj.Stock__c+input;
update customobj;

parent.location.href = url;

 

 

But it is not working. Can someone help me with this?

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

I tried below code and it works for me.

 

{!requireScript("/soap/ajax/26.0/connection.js")}

var url = parent.location.href;

var input=prompt("Enter the number");
var query = "SELECT Id, Stock__c FROM Inventory__c WHERE Id='{!Inventory__c.Id}'";
var customObj = sforce.connection.query(query);
var records = customObj.getArray("records");

var inv = new sforce.SObject("Inventory__c");
inv.Id = "{!Inventory__c.Id}";
inv.Stock__c = parseFloat(records[0].Stock__c) + parseFloat(input);
result = sforce.connection.update([inv]);
parent.location.href = url;

 

Hope this will be usefull to you.

All Answers

Dhaval PanchalDhaval Panchal

I tried below code and it works for me.

 

{!requireScript("/soap/ajax/26.0/connection.js")}

var url = parent.location.href;

var input=prompt("Enter the number");
var query = "SELECT Id, Stock__c FROM Inventory__c WHERE Id='{!Inventory__c.Id}'";
var customObj = sforce.connection.query(query);
var records = customObj.getArray("records");

var inv = new sforce.SObject("Inventory__c");
inv.Id = "{!Inventory__c.Id}";
inv.Stock__c = parseFloat(records[0].Stock__c) + parseFloat(input);
result = sforce.connection.update([inv]);
parent.location.href = url;

 

Hope this will be usefull to you.

This was selected as the best answer
NishanNishan

Hi Dhaval,

                  Thank you vey much for the code. I works fine for me too. If it doesnt take too much of your time, could you please explain to me about the changes you made in my code.

 

         In the line,

                      

                        var records = customObj.getArray("records");

 

            what does the "records" parameter return to the getArray function?

 

 

Also what is the error in the way I updated Inventory__c ?

 

 

Dhaval PanchalDhaval Panchal

Hi,

 

Consider your code,

 

var customObj = sforce.connection.query("SELECT Id FROM Inventory__c WHERE
Id='{!Inventory__c.Id}'");

customobj.Stock__c=customobj.Stock__c+input;
update customobj;

 

here you have used variable customObj, upto this everything is correct. But now you try to access fields using costomobj but you need to extract array from costomobj.

 

see below my code.

 

var records = customObj.getArray("records");

var inv = new sforce.SObject("Inventory__c");
inv.Id = "{!Inventory__c.Id}";
inv.Stock__c = parseFloat(records[0].Stock__c) + parseFloat(input);
result = sforce.connection.update([inv]);

once you get array, you can access 0th element to process (here only one record is fetched).

 

now you have tried to call DML function "Update" directly, but we can not make call it directly as we are using sfdc connection, so we have to use sforce.connection.update. Its apply to all DML operations.

 

One more thing you are trying to add input value to strock, but you need to parse before adding them.

 

I hope this will help you. Feel free to ask question.

Dhaval PanchalDhaval Panchal
Hi Nishan,

Even I don't know why we have to use "records". I found this on web and using that every time and works for me fine.
NishanNishan

Thanks,

              Just one more thing. What does the parameter "records" return to the getArray function?

Dhaval PanchalDhaval Panchal
It returns sObject[].