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
NaishadhNaishadh 

Pass sobject from javascript to component

Hi,

 

I want to pass opportunity sobject from javascript method to component class. I am able to pass string value but  not able to pass object.

 

Please help. 

 

 

vcassvcass
Can you post your code  ?
NaishadhNaishadh

Page code :-

<apex:page controller="testController">
            <apex:includeScript id="connection" value="/soap/ajax/15.0/connection.js"/>
            <apex:form >
            <!-- Define the JavaScript function sayHello-->
            <apex:actionFunction name="sayHello" action="{!sayHello}"
            rerender="out" />

            <apex:outputPanel id="out">
            <apex:outputText value="Hello "/>
            <apex:inputText value="{!username}"/>
            <apex:actionStatus startText="requesting..." id="myStatus">
            <apex:facet name="stop">{!username}</apex:facet>
            </apex:actionStatus>
            </apex:outputPanel>
            <p>
            <apex:outputText value="Clicked? {!state}" />
            <apex:inputText value="{!Opportunity}" id="showstate"/>
            </p>
            </apex:form>           
            <!-- Call the sayHello JavaScript function using a script element-->
            <script>
                //window.setTimeout(sayHello,2000);
                function testJS() {
                    var oppr = new sforce.SObject("Opportunity");
                    oppr.set("Id","123456789");
                    alert(oppr);
                   
                    methodOneInJavascript('test',oppr);

                }
            </script>
           
            
           
            <!-- Add the onclick event listener to a panel.
            When clicked, the panel triggers
            the methodOneInJavascript actionFunction with a param -->
            <apex:outputPanel onclick="testJS()" styleClass="btn">
            Click Me
            </apex:outputPanel>
            <apex:form >
            <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate" >
                <apex:param name="firstParam" assignTo="{!state}" value="" />
                <apex:param name="secondParam" assignTo="{!data}" value="" />
  
            </apex:actionFunction>
            </apex:form>
            </apex:page>

 

Component Code

 

public class testController{
           
            SObject opp;
           
            List<String> data;
           
            public void setOpportunity(SObject s) {
                System.debug(s);
                opp = s;
            }
           
            public String year {get;set;}
           
            public void setData(String[] data) {
                this.data = data;
            }
           
            public String[] getData() {
                return data;
            }
           
            public SObject getOpportunity() {
                return opp;
            }
           
            String uname;
           
            public String getUsername() {
            return uname;
            }
           
            public void setUsername(String s) {
                uname = s;
            }
           
            public PageReference sayHello() {
            uname = UserInfo.getName();
            return null;
            }
           
            public void setState(String n) {
            state = n;
            }
           
            public String getState() {
            return state;
            }
           
            public PageReference methodOne() {
                System.debug('hi inside method one');
                System.debug(getState());
                System.debug(getOpportunity());
                System.debug(getData());
                return null;
            }
           
            private String state = 'no';
           
           
 }

vcassvcass

Thanks. I don't think this is possible to pass SObjects. Would a solution be to create the opp via

 

 

var oppr = new sforce.SObject("Opportunity");
oppr.set("stage","Won");
oppr.set("name","test");

..other required fields

var result = sforce.connection.create([oppr]);

 

 

 then pass the Opp Id to your controller?

Sorry I can't be of more help.

 

TehNrdTehNrd
Why are you doing this with Javascript? I would be alot simpler to have a button that calls a method in your controller to create an opp.
Message Edited by TehNrd on 02-20-2009 03:28 PM
NaishadhNaishadh

I have alreay existing s-control for opportunity merge. It is just like duplicate lead or contact merge page. In my opportunity merge s-control I have to fetch user selected field value. Now I want to replace it with visual force page.

 

I hope u will get my point. If you have any other idea for the same please let me know.

Message Edited by Naishadh on 02-20-2009 10:40 PM