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
deepu.sandeepdeepu.sandeep 

Contact insert

Hi

 

        I created one  vf page and there are some fields on it along with dat fields i have firstname,lastname,email fields and

i need to insert with these fields in to contact  object and remaining fields to custom object.

 

How can i do dat can any one suggest me. 

Navatar_DbSupNavatar_DbSup

Hi,


You can creat a simple VF page. Try the below code as reference(made changes accordingly):


//////////////////// Controller //////////////////////////////
Public class ContactInsert
{
Public string lstName{get;set;}
Public string email{get;set;}
Public string OtherFieldName{get;set;}
Public void save()
{
Contact con=new contact(lastname= lstName,email=email);
Insert con;
Customobject__c cus=new Customobject__c(name= OtherFieldName);
Insert cus;
}
}
////////////////// VF page //////////////////////////////////
<apex:page>
<apex:form>
LastName:<apex:inputtext value=”{!lastname}” /><br/>
Email:<apex:inputtext value=”{!email}” /><br/>
OtherFieldName:<apex:inputtext value=”{! OtherFieldName }” /><br/>
<apex:commanbutton value=”Save” action=”{!save}” />
</apex:form>
</apex:page>

deepu.sandeepdeepu.sandeep

k i am able to insert contact but in custom object i have lookup field contact how can i get that lookup field value as the inserted contac value;

SaravanaBharathi-SBSaravanaBharathi-SB

Contact con = new Contact(lastname = lstName,email = email);

insert con

CustomObject__c cus = new CustomObject__c(name=OtherFieldName,LookUpField = con.Id);

insert cus;

deepu.sandeepdeepu.sandeep

k SB Thanks its solved but i have one more problem.

i have login credentials saved in object and i need to check while logging-in the user is a already reistered user or a new user. if he is a new u ser i need to send him to register user and if he is already registered user then it have to ask for password change page.

How could i do dat can you suggest me.

 

SaravanaBharathi-SBSaravanaBharathi-SB

Could you please elaborate your scenario.

At what time user should be redirect to register user and at what time he should be redirecting to password change page.

 

For clarity,

Logging in user is already registered, he should redirect to create record in another custom object where you want to store the credentials.. Is this the  case?.. If not... Elaborate your scenario..

deepu.sandeepdeepu.sandeep

My Requirement is if the user is visiting my site first time he will register anf after clicking an autegenerated password and username will send to his mail & his fname,lname,email will insert into contact obj. now he logs in with those usrname & pwd for first time it need to ask for password change page bcoz his pwd is random generated, after pwd change he needs to redirect to application form page. now my prob is how i need to check dat the user is logging in fo  1st time nd redirect to pwd change page (or) if already he changed his pwd  to redirect to appl'n page.

 

i am sending the code wat ever i have wrote if possible do some changes to dis code revert to me.

 

-------------vf page-------------------------

<apex:page controller="logincntrlr" sidebar="false">
<apex:pageMessages />
<apex:form id="theForm"><br /><br /><br />
<apex:pageBlock >
<apex:pageBlockButtons location="bottom">

<table cellpadding="5" align="center" >
<tr><td><b>Already Registered User</b> Sign-in </td>
<td><b>New User</b> Sign-up</td>
</tr>
<tr>
<td>
<table>
<div id="myDiv" style="color:#FF0000;"></div>
<tr>
<td><b><apex:outputLabel for="User Name">User Name:</apex:outputLabel></b></td>
<td><apex:inputField value="{!l.Name}" id="username"/></td>
</tr>

<tr>
<td><b><apex:outputLabel for="Password">Password:</apex:outputLabel></b></td>
<td><b><apex:inputsecret value="{!l.password__c}" id="password"/></b></td>
</tr>
<tr>
<apex:inputHidden value="{!autopassword}" id="randomfield"/>
</tr>
<td></td>
<tr><td>
<apex:commandButton value="Sign-in" action="{!save}" onclick="return validateFields('{!$Component.username}','{!$Component.password}');" id="loginButton" /></td></tr>

</table>

</td>
<td>
<table cellspacing="0">
<tr>
<td><b><apex:outputLabel for="First Name">First Name</apex:outputLabel></b></td>
<td><b><apex:inputtext value="{!firstname}"/></b></td>
</tr>
<tr>
<td><b><apex:outputLabel for="Last Name">Last Name</apex:outputLabel></b></td>
<td><b><apex:inputtext value="{!lastname}" /></b></td>
</tr>
<tr>
<td><b><apex:outputLabel for="EMail">E-Mail:</apex:outputLabel></b></td>
<td><b><apex:inputtext value="{!email}" id="email"/></b></td>
</tr>

<tr><td><apex:commandButton value="Sign-up" action="{!sendEmail}"/></td></tr>
</table>
</td>
</tr>

</table>

<html>
<head>
<script>
function validateFields(username,password)
{
var usrName=document.getElementById(username).value;
var password=document.getElementById(password).value;

var ni1 = document.getElementById('myDiv');
ni1.innerHTML = '';

if(usrName=='' || password=='')
{
ni1.innerHTML = 'Please fill Username and Password';
return false;
}
else
{
return true;
}

}

</script>
</head>
</html>
<script>
if({!emailsentflag})

alert("Login Details has been mail to your Registered mail Address Check ur mail");
</script>


</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

------------------------controller-------------------------------

 

public with sharing class logincntrlr {

public contact con;
public String autopassword{get;set;}
public String firstname{get;set;}
public String lastname{get;set;}
public String email{get;set;}

public static String getautopassword(Integer len)
{
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
System.debug(key);
return key.substring(0,len);
}

public logincntrlr(){
l= new Logins__c();
}

public boolean emailsentflag{get;set;}

public PageReference sendEmail()
{


String autopassword =logincntrlr.getautopassword(6);
l.autopassword__c=autopassword;
l.First_Name__c=firstname;
l.Last_Name__c=lastname;
l.Email__c=email;


Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

String [] toAddresses = new String[] {l.Email__c};
email.setToAddresses(toAddresses );
email.setSubject('Username & password');
emailsentflag = true;
email.setHtmlBody('Username: ' + l.Email__c +'<br/> password:'+ l.autopassword__c+
'<br/>To log-in<br/> <a href=https://c.ap1.visual.force.com/apex/loginpage><br/>click here.</a>');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

insert l;
Contact con=new contact(lastname= l.Last_Name__c,firstname=l.First_Name__c,email=l.Email__c);
Insert con;
l= new Logins__c(Contact__c=con.id);

return null;
}

public Logins__c l{get;set;}
public PageReference save()
{

insert l;
pagereference p = new pagereference('/apex/endeavor2012regform2');
p.setredirect(true);
return p;
}


}

 

 

How could i resolve it  suggest me

Thanks in Advance.