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
KavitaKavita 

Need help to write S control that imports Oppty Product informtion into Opportunity Object

Can someone help me solve this.
 
I need the amount field on the opportunity object (which is a standard non-editable field) to represent a different calculated value.
 
I've created 2 custom fields and a button
(A) "Amount2" in the opportunity object.
(B) "Total price 2" in the opportunity product object (a custom calculation) 
(C) "Update Amount 2" button in the opportunity to upload the sum of (B) into (A)
 
I've been playing with scontrols for a week now trying to figure out how to populate amount 2 with the sum of Totalprice 2 but it doesn't work!!!!!!! Can someone please help?
MadhuMadhu
hi ,
 
wt exactly u want,u want to populate the data to the page layout.can u pls send u r code
so that i can help u.
 
Regards,
madhu
sfdcfoxsfdcfox
Code:
{!RequireScript("/soap/ajax/9.0/connection.js")}
// We're going to just do blanket error coverage.
try
{ 
// Create an SObject for the opportunity we are on.
Opportunity = new sforce.SObject("Opportunity")
// Populate the ID field for the update call.
Opportunity["id"] = "{!Opportunity.Id}"
// Query the Total Amount 2 field from the line items.
Products = sforce.connection.query("select Total_Amount_2__c from OpportunityLineItem where OpportunityId = '{!Opportunity.Id}'")
// Create a QueryResultIterator to total the Total Amount 2 field.
ProductIterator = new sforce.QueryResultIterator(Products)
// Zero the Amount 2 field in our record update object.
Opportunity["Amount_2__c"] = 0
// While there are more line items to add together...
while(ProductIterator.hasNext())
{ // ... get the next line item ...
  Product = ProductIterator.next()
  // ... and add it to the opportunity's Amount 2 field.
  Opportunity["Amount_2__c"] += Product.getFloat("Total_Amount_2__c")
}
// Update the opportunity's Amount 2 field.
sforce.connection.update([Opportunity])
// Refresh the page.
} catch(e)
{ // Show the soap error, javascript error, or the error object if none of the above.
  alert("There was an error processing your request: "+e.faultCode?e.faultCode:e.message?e.message:e)
}
// Refresh the page's contents.
window.top.location.href = window.top.location.href

Note, I designed this to be a button/link, not an S-Control. To use it, go to Setup | App Setup | Customize | Opportunities | Buttons and Links. Choose the "Execute JavaScript" behavior when using this code. This will not be an automatic update; the user will need to click the button or link to cause the code to execute. You could make it an automatic page script, but those are annoying as they slow down the browser and are easy to accidently create infinite loops. This way, the user expects a delay while the browser calculates the new total and updates the opportunity (this normally takes 1-2 seconds, depending on the number of line items).

~ sfdcfox ~

Message Edited by sfdcfox on 08-06-2007 03:24 PM

Message Edited by sfdcfox on 08-06-2007 05:03 PM

KavitaKavita
Hi. I walked through all the steps and when I try to execute the script by clicking on the button and I get the following pop-up error;
 
A problem with the OnClick Javascript for this button or link was encountered: Expected 'y'
 
Please advise. Thanks in advance
 
 
KavitaKavita
oops . I meant expected ')' instead of 'y' in my earlier post.
sfdcfoxsfdcfox
I modified the script in the message above... it seems that for some unfathomable reason, it converted my "?" symbols to "-" in the previous post, so the error alert wasn't working. Feel free to try it again.

~ sfdcfox ~
KavitaKavita

I feel so needy, wish I could solve some of this myself...

Now the pop up message says "undefined"

 

sfdcfoxsfdcfox
Yep. I had another typo in my code. QueryResultsIterator was supposed to be QueryResultIterator. This time, I tested my code before posting it. It works. I hope this helps you out!

~ sfdcfox ~

Edit: I should mention that I do better programming AFTER my first cup of coffee.

Message Edited by sfdcfox on 08-06-2007 05:06 PM

KavitaKavita
I think functioning after coffee goes for both of us:) I copied and pasted the revised code but it still gives me an "unknown" error popup.  I think it might be user error this time since you had your coffee and tested it:)
 
I will go get an espresso and start this from scratch.
 
Thanks so much for your assistance.
KavitaKavita
SFDC Fox- you rock!!! Thanks