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
RiverRiver 

code sample requested (probably javascript) to create a calculated field

We need to create a calculated field where (custom_field_A ) - (custom_field_B) = (custom_field_C).  Further, custom_field_A is on a different tab than custom_field_B.  Can anyone out there supply me with some sample code and/or suggestions on how to go about accomplishing this?  Any help is much appreciated.

Thanks

DevAngelDevAngel

Hi River,

Here is a sample you can use.  It is an sforce control used on an opportunity.  It takes the sessionid, opportunity id, server url and opportunity detail link as merge fields to provide context and navigation.  The line items of the opportunity are queried by the scontrol returning the TotalPrice for each line item.  These prices are summed and the result is placed in a custom field on the opportunity showing the total price for all line items.  Once the opportunity is updated, the scontrol redisplays the opportunity page with the newly calculated value.  This is not a usefull calculation, but should demonstrate how to do a simple calculation referencing related objects.

For the link, right click and use save target as to save the page to your system.  You can then view the source using notepad or some other editor.  Don't try to open the page in the browser, it won't work due to the merge fields embedded in the javascript.

You can create a new scontrol and paste the source of this page into the content of the scontrol.  You will need to create a new currency field on the opportunity call Actual_Total for this to work.  Also, there is no error checking to see if any line items exist.

Cheers

Message Edited by DevAngel on 12-27-2004 12:15 PM

RiverRiver

Hi Dave,

Thanks so much for the quick response!  I've got a few related questions and hope you won't mind giving some guidance. 

Background:
My company has identified 2 things (so far) that would require custom development - 1) calculated fields and 2) having fields from one tab display on another.  Based on this, they are planning on sending me to the API class in January.  My "programming background" is that I spent about 7 months going through an intensive java course and studying for the certification (didn't pass   as well as some very basic web programming and scripting skills.  I've been brushing up on java and xml for the purposes of the upcoming API class.

Questions:
After reviewing your code, it seems to me that for the calculated field, (our most important concern), I would be better off putting off the sforce API class and instead getting a handle on ActiveX/javascript/xml - would you agree?  I'm looking for the most expedient way to get up to speed and get the job done instead of slogging my way through jsp books and taking a week long class which may only barely touch upon what I'm actually trying to accomplish.  Do I need to get Microsoft Visual Studio .NET in order to dig in and get started?  Any and all words of wisdom would be greatly appreciated. 

Thanks again,

Jen (River)

DevAngelDevAngel

Hi River,

The programming tools to use are dictated by the business requirements and capabilities of the API.  SControls can be implemented using the method I provided, but are not limited to that.  Javascript will likely be part of any web link or scontrol.  For the calculated fields problem, you can solve this using either web links or scontrols.

To implement the calculated field using a web link you would need to have an http service running and available within your firewall and the solution would be implemented in java, .net, php or any appropriate web technology.  At the end of the process you a bit of javascript would be required to get back to the original lead detail page.

Underlying the various possible implementation scenarios, a basic understanding of the sforce API is also required.  The api looks and behaves the same way regardless of the tools used to exercise it.  The logical place to start is to become familiar with the capabilities and technology of the API and then to examine the business requirements.  Between these to pieces of information and available skills and infrastructure, you can then come to a decision about the implementation of the solution.

Cheers

Katie_TKatie_T

Dave,

I have used the code that you attached in your message to create my first sforce control, and it worked beautifully.  Now I am trying to do something similar, but instead of calculating a total, I want to set a field to the value of one of 3 other fields within the Opportunity (which ever one is not null).  So, if field 1 is not null, set it to that, else if field 2 is not null, set it to that, else set it to field 3.

I have not been able to write a query that does this.  Can you offer any assistance?  I appreciate any help you can give.

Katie

DevAngelDevAngel

Hi Katie_T,

The (in the context of the sample) would be:

queryCommand += "<queryString>Select Id, Field1, Field2, Field3 from OpportunityLineItem Where OpportunityId = '" + oppId + "'</queryString>";

Then, when you process the results:

var newVal = "default";

var objNodeList = xmlDoc.documentElement.selectNodes("//sf:Field1");
if (objNodeList != null) newVal = objNodeList.item(0).text;

else

 
for (var i=0;i<objNodeList.length;i++) {
 tot = tot + parseFloat(objNodeList.item(i).text);
}

DevAngelDevAngel

Hi Katie_T,

The (in the context of the sample) would be:

queryCommand += "Select Id, Field1, Field2, Field3 from OpportunityLineItem Where OpportunityId = '" + oppId + "'";

Then, when you process the results:

var newVal = "default";

var objNodeList = xmlDoc.documentElement.selectNodes("//sf:Field1");
if (objNodeList != null) newVal = objNodeList.item(0).text;

else {

     objNodeList = xmlDoc.documentElement.selectNodes("//sf:Field2");

     if (objNodeList != null) newVal = objNodeList.item(0).text;

     else {

          objNodeList = xmlDoc.documentElement.selectNodes("//sf:Field3");

          if (objNodeList != null) newVal = objNodeList.item(0).text;

     }

}


Then you can modify the update portion to set the field you want to newVal.

Cheers

Message Edited by DevAngel on 01-05-2005 03:37 PM

Katie_TKatie_T

Thanks for the quick response!  I understand what you have given me, but I have a question about OpportunityID.  I see how this would be used when selecting from Opportunity Line Items or something related to an Opportunity. 

But, to select fields within the Opportunity itself, I can't figure out how this would work.  I don't see OpportunityID in the WSDL within Opportunity, only for objects that are associated to an Opportunity.  There is an AccountID within Opportunity, but if I use that in the query to select Opportunity fields, how will it know which Opportunity to select from if there is more than one?

e.g.

queryCommand += "Select Id, Field1, Field2, Field3 from Opportunity Where AccountId = '" + acctId + "'";

Once again - I really appreciate your help.  Your postings are the only thing that has helped me to understand any of this!

DevAngelDevAngel

Hi Katie,

Each object has an Id field.  This is the primary key for an object.  If you have the opportunity id, say from a merge field you could ask for

"Select Id, Name, {otherOpportinity fields...} From opportunity Where Id = '994093j'"

You can combine other crieteria into the where clause.  In the case where you are using account id, you should recieve 0 to n opportunities depending on how many are associated with that account as referenced by the account id.

If you need to narrow down the results further, you need to determine which criteria to use to obtain the record you want and include that in the where clause:

where AccountId = '00438943534' and IsClosed = false

 

If this is not the answer to your question, please rephrase it.

Cheers

Katie_TKatie_T

Thank you!! This immediately solved my problem, and it works exactly how I want it.

Katie

JoyceJoyce

 

Message Edited by Joyce on 03-09-2005 08:20 AM

JoyceJoyce

It is not possible to trigger this to occur automatically.  From the work we've done, , the Scontrol needs to open up in a frame for the user.  We need to take the functionality out of the Salesforce for this to happen.

You remarked

"Once the opportunity is updated, the scontrol redisplays the opportunity page with the newly calculated value"

Message Edited by Joyce on 03-09-2005 08:18 AM

The_FoxThe_Fox
Hi Joyce,

You can't you have to click on a web link

By the way, you can make it "transparent for the users" by having your code in the scontrols open in the salesforce.co frame and then update the window.parent.parent=returl

Voila

Cheers
JoyceJoyce

THanks Fox,

Can you give me more detail on having the "scontrols open in the salesforce.co frame"

 

Message Edited by Joyce on 03-09-2005 08:19 AM

The_FoxThe_Fox
Hi joyce

When you create the web link rletaed to your scontrols you choose
Open in Salesforce.com window with sidebar

and somewhere in your sforce controls and in javascript you have to get something like
[var retUrl = "https://emea.salesforce.com/" + accId; ] (accID is a variable I set before) here I am returning to an account and then if you do not need user interaction at the end of you javascript you put the following line

[window.parent.parent.location.href = retUrl; ] and that's it.

I hope this help. I you need more info do not hesitate to contact me on my email
chumbert[nospam]@[nospam]odyssey-group.com
ddoellingddoelling

I have been able to write a couple of sforce controls using your code.  I am working on a utility in javascript that will insert a new opportunity using selected fields from an old opportunity.  After a successful insert I would like to get to the id in the result and build the URL so the new opportunity can be displayed to the user.  I looked at the sample create response message in the API 5.0 doc and tried the following but it didn't work. What is the correct method of doing this?  Thanks

var objNodeList = xmlDoc.documentElement.selectNodes("//sf:id");

Here is the code so far that handles the response.

xmlDoc.loadXML(msxml.responseText);

xmlDoc.setProperty("SelectionNamespaces", "xmlns:sf='urn:sobject.enterprise.soap.sforce.com'");

var status = msxml.status if (status == 200) {  

document.write("Opportunity successfully cloned.");   

//window.parent.parent.location.href="https://na1.salesforce.com/" +  newoppid;

}

else {  

document.write("Error condition = ")  

document.write(status);  

document.write(" Please send a print of this screen to the Salesforce Administrator ");  

document.write(msxml.responseText); }

 
James McGillJames McGill
I know this thread is old, but the s-control or code does not seem to be attacehd.  Can you please send to me at jmcgill@quantapoint.com.
MCunninghamMCunningham

Hello Dave

I was trawling the blogs in search of some example code to summarise currency values in multiple related records - and your response seems to ring all the bells. Would you mind sharing this example code with me too?

Also I have another I hope you might have ideas on..as follows;

My client has asked for validation in Account records to ensure that if a checkbox (called "Open Mandate") has been checked that at least 1 attachment or perhaps a specifically named attachment exists.   Any thoughts on how to achieve this?

Many thanks in advance

Michael.