2

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 custom Object(Survey__c)

  2. This Survey object has a lookup field which is Account, this should also be saved.

How do I achieve this?

Below is my code:

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>

Controller

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;
    }
  }
}
3
  • Welcome to SFSE! Please take a moment to visit the help center, scroll through the tour, and read How to Ask. What have you tried so far? Where are you stuck? Right now it does not appear that you have made a good faith effort to research or solve this problem before posting here.
    – Adrian Larson
    Commented Apr 4, 2017 at 17:07
  • Please edit your post to be more clear about why what you have so far does not meet your needs.
    – Adrian Larson
    Commented Apr 4, 2017 at 17:19
  • I'm able to save the custom object but not able to populate the account lookup in the customobject
    – mvr
    Commented Apr 4, 2017 at 17:31

1 Answer 1

0

According to your code aq.account__c = account; You are assigning Account which is string to the lookup field. Thats why it is not giving you AccountId after saving.

Small changes are suggested.

Declare Account as object instead of String

public Account accountObj { get; set; }

In the visualforce, for <apex:inputField> use Account.Id, which will give you Account Lookup.

<apex:inputField  value="{!accountObj.Id}"/>

In the Controller make this change aq.account__c = accountObj.Id;

Controller

public class myAudit {

    public Account accountObj { 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 = accountObj.Id;
        insert aq;
        return null;
    }
  }
}

Finally, since you are performing DML so use try-catch block as a matter of best practices.

You must log in to answer this question.

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