5

1st part

I have a long-ish question and I will break it into two parts.

First, how to pass parameter to VF page via Javascript?

I want to click a button and it will take me to VF page and give it parameter account ID of record from which button was clicked.

I'm using javascript because I'm unable to see any VF pages while creating button, for some reason. If you can help me in that regard ,it'd be awesome too.

Thanks

2nd part

How to edit/update/fill field on record with some value from a Visualforce page? (Part 2 of 2)

Hi, this is 2nd part of my question.

My VF page will give out a Contact name, how do I put that into a field on an Account record (that called the VF page)?

The user selects a radio button and clicks on "Accept" and the page should change to the Account record with new information in a field (lets call it "contact name temp" for now).

How do I do this?

Thanks.

Sorry if this sounds too basic but I'm unable to "integrate" or "put" these tricks together.

3
  • 1
    As far as i understood, All the Contacts Names with Checkbox should be displayed in VF Page. And whenever you clicks that radio button and click on accept, It should be updated in Account Field. Is that Correct?. If what i said is correct then Some more info needs for the second part. What is the Field of Account that you want to update? What is the Contact Field that needs to be display in Account.?
    – Subhash
    Commented Jun 30, 2016 at 10:07
  • Subhash, the 1st part is working perfectly, thanks to you, and it is exactly like you wrote in this comment. I want to update custom field name PRIMARYCONTACT__c (the actual one) with the name of the Contact radio button that is picked before selecting Accept button. Only contact name. for eg, I select name (*)Anil and click Accpet, the field PRIMARYCONTACT__c should display "Anil". You've really helped me learn a lot here!
    – fraglord
    Commented Jun 30, 2016 at 10:12
  • Sorry for the Delay, Check my updated answer.
    – Subhash
    Commented Jun 30, 2016 at 12:34

1 Answer 1

5

For 1st Part


1st Method:

Create a Button with Onclick Javascript and add the below code in it.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

window.open("/apex/YOUR_VF_PAGE_NAME?id={!Account.Id}", "_self", "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=no, width=650, height=650");

2nd Method:

You can put a direct VF Page by choosing the URL Option in the Custom Button Edit Page and place the below url

/apex/YOUR_VF_PAGE_NAME?id={!Account.Id}

For 2nd Part

VF Page:

<apex:page sidebar="false" standardController="Account" extensions="aaaController">
    <apex:form >
        <apex:pageBlock id="block3">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!ProcessSelected}" value="Save the Contact in Account Field"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockTable value="{!wrapContactList}" var="waccl">

                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox />
                        </apex:facet>
                        <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId">
                            <apex:actionSupport action="{!CheckBoxSelect}" event="onchange" reRender="block2,block3">
                                <apex:param name="ConId" value="{!waccl.cont.Id}" assignTo="{!ConId}"/>
                            </apex:actionSupport>
                        </apex:inputCheckbox>
                    </apex:column>

                    <apex:column value="{!waccl.cont.LastName}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class Controller:

public class aaaController {
    public Id ConId{get;set;}
    public list<wrapContact> wrapContactList {get;set;}
    public list<Contact> selectedContacts{get;set;}
    public Id AccId{get;set;}
    public aaaController (ApexPages.StandardController controller){
        wrapContactList =new list<wrapContact>();
        AccId = ApexPages.currentPage().getParameters().get('Id');
        for(Contact c:[Select Id, LastName from Contact where AccountId =: AccId]){
            wrapContactlist.add(new wrapContact(false,c));
        }
    }
    public void CheckBoxSelect(){
        selectedContacts=new list<Contact>();
        System.debug('ConId=====>'+ConId);
        for(wrapContact wc : wrapContactList){
            if(ConId == wc.cont.Id){
                selectedContacts.add(wc.cont);
            }
            else{
                wc.isSelected = false;
            }
        }
        System.debug('wrapContactlist====>'+wrapContactList);
    }
    public PageReference ProcessSelected(){
        selectedContacts=new list<Contact>();
        for(wrapContact wrapobj:wrapContactlist){
            if(wrapobj.isSelected==true){
                selectedContacts.add(wrapobj.cont);
            }
        }
        Account acc = new Account(id=AccId);
            acc.Description = selectedContacts.get(0).LastName;
        update acc;
        PageReference pr = new PageReference('/'+AccId);
        return pr;
    }
    public class wrapContact{
        public Contact cont{get;set;}
        public boolean isSelected{get;set;}
        public wrapContact(boolean isselect, Contact a){
            cont=a;
            isselect = isSelected;
        }
    }
}

NOTE: I have given the Description Field to be updated in Account with Contact Name in it. Please Change the things according your scenario...

Hope this might helps...

3
  • 1
    It will open the VF Page of yours with the AccountID as parameter in it. You have to use that parameter using the Apex Controller
    – Subhash
    Commented Jun 30, 2016 at 9:53
  • EDIT : what is use of "var w" here? Do I have to use it in VF page/controller to get ID?
    – fraglord
    Commented Jun 30, 2016 at 9:53
  • 2
    You do not really need to load an Ajax Toolkit for this. It is enough to add an parameter to the page URL... And yes, you do not need to use an extra variable w. It is enough to call the window.open(...) method or just redirect the user to the page using location.href. Commented Jun 30, 2016 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .