+ Start a Discussion
sampath pallisampath palli 

Problem in performing Dynamic Dml operations

Hi
I have a requirement that i need to perform dynamic dml operations and when i have custom object registration and vf pages like login and registration And registration object with fields name,password,email.When i entered name dynamically in vf page if i entered name is existed in object it returns null if i entered name is doesn't exist in object then we need to insert the data and go to login page. 
Please check my code:
ApexClass
public class Registrationapex_Rum {
public String rumname{set;get;}
public String password{set;get;}
public String email{set;get;}
public list<Registartion__c> reg{set;get;}
public void search(){
reg=[select Rum_Name__c,Password__c,E_Mail__c from Registartion__c where Rum_Name__c=:rumname];
for(Registartion__c rl:reg){
if(rl.Rum_Name__c==rumname){
clear(); 
}else{
create();
}
}
}
public PageReference create(){
Registartion__c r=new Registartion__c();
r.Rum_Name__c=rumname;
r.Password__c=password;
r.E_Mail__c=email;
insert r;
PageReference p=new PageReference('/apex/loginpage_Rum');
return p;
}
public void clear(){
rumname=null;
password=null;
email=null;
}
}
VF Page:
<apex:page controller="Registrationapex_Rum">
<apex:form >
<apex:pageBlock title="Registration" id="pb1">
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Rum Name"/>
<apex:inputText value="{!rumname}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Password"/>
<apex:inputText value="{!password}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="E-mail"/>
<apex:inputText value="{!email}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton value="Submit" action="{!search}"/>
<apex:commandButton value="Clear" action="{!clear}" reRender="pb1"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Thanks In Advance
Sampath
Best Answer chosen by sampath palli
ajay rawat 14ajay rawat 14
Hi Sampat 
               Try below code

public class Registrationapex_Rum {
public String rumname{set;get;}
public String password{set;get;}
public String email{set;get;}
public list<Registartion__c> reg{set;get;}

public PageReference  search(){
reg=[select Rum_Name__c,Password__c,E_Mail__c from Registartion__c where Rum_Name__c=:rumname];
if(reg.size() == 0)
return create();
else
clear(); 
return null;
}
public PageReference create(){
Registartion__c r=new Registartion__c();
r.Rum_Name__c=rumname;
r.Password__c=password;
r.E_Mail__c=email;
insert r;
PageReference p=new PageReference('/apex/loginpage_Rum');
return p;
}
public void clear(){
rumname=null;
password=null;
email=null;
}
}


Thanks

All Answers

ajay rawat 14ajay rawat 14
Hi Sampat 
               Try below code

public class Registrationapex_Rum {
public String rumname{set;get;}
public String password{set;get;}
public String email{set;get;}
public list<Registartion__c> reg{set;get;}

public PageReference  search(){
reg=[select Rum_Name__c,Password__c,E_Mail__c from Registartion__c where Rum_Name__c=:rumname];
if(reg.size() == 0)
return create();
else
clear(); 
return null;
}
public PageReference create(){
Registartion__c r=new Registartion__c();
r.Rum_Name__c=rumname;
r.Password__c=password;
r.E_Mail__c=email;
insert r;
PageReference p=new PageReference('/apex/loginpage_Rum');
return p;
}
public void clear(){
rumname=null;
password=null;
email=null;
}
}


Thanks
This was selected as the best answer
sampath pallisampath palli
thanks ajay