+ Start a Discussion
DeepVDeepV 

Insert/Save CustomObject and account fields using visualforce page?

Hi all,

For external users, we will be sending a link in the email to fill survey and when they click submit.

1) A record should be created/saved into salesforce customOBject(Survey__c)
2)This Survey object has a lookup field which is Account, this should also be saved.

How do I achieve this? Please can you post the code.



 
Best Answer chosen by DeepV
Raj VakatiRaj Vakati

Passing the id in the url as ..apex/myAudit?id=<Account record id pass here . >

  public PageReference save() {
        //Add your custom logic to update specific fields here 
        Survey__c aq = new Survey__c();
        aq.Name = txtSurvey;
aq.account__c =apexpages.currentpage().getparameters().get('id');
        insert aq;
        return null;
    }
 

All Answers

Raj VakatiRaj Vakati
Hi MVReddy,

When you are creating a Survey Page, pass the account into the URL parameters from the page. 
You can get the account id from the URL parameters and assign it while you are saving it. 

Thanks ,
Raj

 
DeepVDeepV
Hi Rajamohan, thanks for the reply. I am able to save the new record but account lookup on survey object is always null/blank

Below is my apex class:

public class myAudit {

    public String account { get; set; }
    public String txtAuditQ { get; set; }
    public PageReference save() {
        //Add your custom logic to update specific fields here 
        Survey__c aq = new Survey__c();
        aq.Name = txtSurvey;
        insert aq;
        return null;
    }
  }
}

My VisualForce page:

<apex:page controller="myAudit">
    <apex:form >
        <apex:pageblock >
            <apex:inputField  value="{!account.name}"/>
            <apex:inputText id="Survey" value="{!txtSurvey}"/>
              <apex:commandButton action="{!save}" value="save"/>
        </apex:pageblock>
    </apex:form>
</apex:page>
Raj VakatiRaj Vakati
public class myAudit {

    public String account { get; set; }
    public String txtAuditQ { get; set; }
    public PageReference save() {
        //Add your custom logic to update specific fields here 
        Survey__c aq = new Survey__c();
        aq.Name = txtSurvey;
aq.account__c =account  ;
        insert aq;
        return null;
    }
  }
}


<apex:page controller="myAudit">
    <apex:form >
        <apex:pageblock >
            <apex:inputText value="{!account}"/>
            <apex:inputText id="Survey" value="{!txtSurvey}"/>
              <apex:commandButton action="{!save}" value="save"/>
        </apex:pageblock>
    </apex:form>
</apex:page>

 
Raj VakatiRaj Vakati
Here is the code 

public class myAudit {

    public String account { get; set; }
    public String txtSurvey{ get; set; }
    public PageReference save() {
        //Add your custom logic to update specific fields here 
        Survey__c aq = new Survey__c();
        aq.Name = txtSurvey;
aq.account__c =account  ;
        insert aq;
        return null;
    }
  }
}


<apex:page controller="myAudit">
    <apex:form >
        <apex:pageblock >
            <apex:inputText value="{!account}"/>
            <apex:inputText id="Survey" value="{!txtSurvey}"/>
              <apex:commandButton action="{!save}" value="save"/>
        </apex:pageblock>
    </apex:form>
</apex:page>
DeepVDeepV
I'm getting the below error:

System.StringException: Invalid id: new account survey
Error is in expression '{!save}' in component <apex:commandButton> in page myaudit: Class.myAudit.save: line 12, column 1


Passing the id in the url as ..apex/myAudit?id=06636000004OblF
Raj VakatiRaj Vakati

Passing the id in the url as ..apex/myAudit?id=<Account record id pass here . >

  public PageReference save() {
        //Add your custom logic to update specific fields here 
        Survey__c aq = new Survey__c();
        aq.Name = txtSurvey;
aq.account__c =apexpages.currentpage().getparameters().get('id');
        insert aq;
        return null;
    }
 
This was selected as the best answer
DeepVDeepV
line 12 is aq.account__c = account;
Please help!
DeepVDeepV
User-added image

Thanks for your help RajaMohan, but I'm unable to see the account name as highlighted above
DeepVDeepV
Now I can see the account name as well, Thanks a Lot Rajamohan!