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
TuckoTucko 

Custom button and a formula

Hello, 

I have a question, I am making a custom button which need to generate data from certain formula and after that put the data in given field, the formula was the easy pars, and as I am new to salesforce, I can't get a way to put the data into the given field, I am having this formula code:

{!PayedSum__c.Payed_Sum__c} = {!Account.Compensation_Sum__c} - {!Account.Payed_Compensation_Sum__c} AND
{!PayedSum__c.Payed_Date__c}  = {!TODAY()}

As you can see from the code I need to put the data in sObject PayedSum__c field Payed_Sum__c, can someone help me how to put the result from this formula into the Payed_Sum__c field ?

Thank you in advance,
Bozidar
Best Answer chosen by Tucko
Rahul SharmaRahul Sharma
Could you paste your code, if you have tried something different from below:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");			

a.Id = "{!Account.Id}";

						
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Compensation_Sum__c = '{!TEXT(Account.Compensation_Sum__c)}';
a.Payed_Compensation_Sum__c = "{!Account.Payed_Compensation_Sum__c}";
a.Payed_Compensation_Sum__c ='{!TEXT(Account.Payed_Compensation_Sum__c)}';
b.Payed_Sum__c = "{!PayedSum__c.Payed_Sum__c}";
b.Payed_Sum__c = '{!TEXT(PayedSum__c.Payed_Sum__c)}';

// assigning the Account Id to reference field!
b.Account__c = "{!Account.Id}";

b.Payed_Sum__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum__c;
var result = sforce.connection.create([b]);


if (result[0].getBoolean("success")) { 
    window.location = result[0].id; 
} else { 
    alert("Record creation failed: " + result); 
}

 

All Answers

Rahul SharmaRahul Sharma
Do you want to update the value in PayedSum's Payed Sum field on click of custom button?
Your question is confusing, could you elaborate on what you wanted to do.
TuckoTucko
Yes Rahul, on click of my custom button I need this formula to evaluate its value and update PayedSum's Payed Sum field
Amit  TrivediAmit Trivedi
Hi,
Create custom button on detail page calculate all in apex controller and call that apex controller on button click and put data in given field in same controller.Button should be ajax type.It is one of the way.

Alternative,
Create formula field do calculation in that field using  formula

{!PayedSum__c.Payed_Sum__c} = {!Account.Compensation_Sum__c} - {!Account.Payed_Compensation_Sum__c} AND {!PayedSum__c.Payed_Date__c} = {!TODAY()}
and update Payed_Sum__c field using workflow field update or trigger

Thanks,
Amit Trivedi
Rahul SharmaRahul Sharma
Yes, a Formula field is most suitable for this requirement.

But if need a ajax example, refer below:
Example:

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		
//	Required field to perform DML and Query operation in Javascript					
/*	Start creating object	*/
var a = new sforce.SObject("Account");				
//	Initialize Account Object
a.Id = "{!Account.Id}";							//	Giving Id of Current Account to the object
a.Phone = 123456;							//	Updating Any field (This can Be dynamic), we can specify look-up field's here.
/*	Stop creating object	*/

sforce.connection.update([a]);				
//	Update Records in the array to database
location.reload(true);							//	Reloading the page

 
TuckoTucko
OK, I somehow make an apex controller, but when I try to run this script: 
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}



sforce.apex.execute("Button","genSum",{}}");

I get this error: 
User-added image

Can you tell me what is my mistake ?

Thank you
Rahul SharmaRahul Sharma
It can be done in javascript itself using ajax toolkit (https://resources.docs.salesforce.com/sfdc/pdf/apex_ajax.pdf" target="_blank), using apex for so small task would be overkill.
Have a look at the example which was provided earlier.
TuckoTucko
Okay Rahul, I took your advice and came up with this piece of code, the problem now is that button is working, but I can't see the results. Can you help me with this one too?
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");
var c = new Date();			

a.Id = "{!Account.Id}";							
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Payed_Compensation_Sum2__c = "{!Account.Payed_Compensatiion_Sum2__c}";
b.Payed_Sum2__c = "{!PayedSum__c.Payed_Sum2__c}";
b.Payed_Date__c = "{!PayedSum__c.Payed_Date__c}";
b.Payed_Sum2__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum2__c;
b.Payed_Date__c = {!TODAY()};


sforce.connection.update([b]);				

location.reload(true);

 
Rahul SharmaRahul Sharma
Whats hapenning when you hit the button, page refreshes with updated values in any of the fields?
Elaborate on whats not working.
TuckoTucko
And with this code: 
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var connection = sforce.connection;
var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");
var c = new Date();			

a.Id = "{!Account.Id}";							
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Payed_Compensation_Sum2__c = "{!Account.Payed_Compensatiion_Sum2__c}";
b.Payed_Sum2__c = "{!PayedSum__c.Payed_Sum2__c}";

b.Name = "{!PayedSum__c.Name}";
b.Payed_Sum2__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum2__c;


result = sforce.connection.create([b]);				
alert(result);

location.reload(true);

I get this error, even though all fields included in equation are Currency:

User-added image
TuckoTucko
Rahul, page reloads and I don't see any changes.
Rahul SharmaRahul Sharma
It is a javascript error(occurs when data type of the field and value assignment mismatches), you should assign a number value to number field. But you are enclosing the number fields in quotes by which its data type changes to String:
b.Payed_Sum2__c = {!PayedSum__c.Payed_Sum2__c};
Try to update single field from the button, and once it updates proceed by adding updates for other required fields. It would be easier for debuging.
 
TuckoTucko
I need help with how to create a new record, the equation works and in pop-up I get the result I need, but I can not update field with that result, here is the code:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");			

a.Id = "{!Account.Id}";

						
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Compensation_Sum__c = '{!TEXT(Account.Compensation_Sum__c)}';
a.Payed_Compensation_Sum__c = "{!Account.Payed_Compensation_Sum__c}";
a.Payed_Compensation_Sum__c ='{!TEXT(Account.Payed_Compensation_Sum__c)}';
b.Payed_Sum__c = "{!PayedSum__c.Payed_Sum__c}";
b.Payed_Sum__c = '{!TEXT(PayedSum__c.Payed_Sum__c)}';


b.Payed_Sum__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum__c;
var result = b.Payed_Sum__c;

sforce.connection.create([b]);
alert(result);
location.reload(true);

Can someone point me how I suppose to create new record of type PayedSum__c and write the result in field Payed_Sum__c. 
Thanks
Rahul SharmaRahul Sharma
I think It is creating the record in Line#20, you just need to navigate to new record on creation.
Currently it is just refreshing the Account page.

Try the code below:
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");			

a.Id = "{!Account.Id}";

						
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Compensation_Sum__c = '{!TEXT(Account.Compensation_Sum__c)}';
a.Payed_Compensation_Sum__c = "{!Account.Payed_Compensation_Sum__c}";
a.Payed_Compensation_Sum__c ='{!TEXT(Account.Payed_Compensation_Sum__c)}';
b.Payed_Sum__c = "{!PayedSum__c.Payed_Sum__c}";
b.Payed_Sum__c = '{!TEXT(PayedSum__c.Payed_Sum__c)}';


b.Payed_Sum__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum__c;
var result = sforce.connection.create([b]);


if (result[0].getBoolean("success")) { 
    window.location = result[0].id; 
} else { 
    alert("Record creation failed: " + result); 
}

 
TuckoTucko
I tried the given code and getting next error:

User-added image
Rahul SharmaRahul Sharma
You need to populate all the required fields while creation!
Rahul SharmaRahul Sharma
Could you paste your code, if you have tried something different from below:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}		

var a = new sforce.SObject("Account");	
var b = new sforce.SObject("PayedSum__c");			

a.Id = "{!Account.Id}";

						
a.Compensation_Sum__c = "{!Account.Compensation_Sum__c}";
a.Compensation_Sum__c = '{!TEXT(Account.Compensation_Sum__c)}';
a.Payed_Compensation_Sum__c = "{!Account.Payed_Compensation_Sum__c}";
a.Payed_Compensation_Sum__c ='{!TEXT(Account.Payed_Compensation_Sum__c)}';
b.Payed_Sum__c = "{!PayedSum__c.Payed_Sum__c}";
b.Payed_Sum__c = '{!TEXT(PayedSum__c.Payed_Sum__c)}';

// assigning the Account Id to reference field!
b.Account__c = "{!Account.Id}";

b.Payed_Sum__c = a.Compensation_Sum__c - a.Payed_Compensation_Sum__c;
var result = sforce.connection.create([b]);


if (result[0].getBoolean("success")) { 
    window.location = result[0].id; 
} else { 
    alert("Record creation failed: " + result); 
}

 
This was selected as the best answer
TuckoTucko
Finally its worked, thank you very much.
Rahul SharmaRahul Sharma
Great, Glad to be of any help. Keep learning!