You need to sign in to do that
Don't have an account?
M Sreekanth
Can any one tell me is this my code is handling excetion.
Can any one tell me is this my code is handling excetion and if transaction happen then how to navigate to page ,if it it is working properly how to know and if any modifactions require let me know
Class:-
=====
/*
* Description : Bulk Account Creation
* Autor :Sreekanth Meesal
* Created Date :16/03/2023
* Last Modified Dtae :
*/
public class Account_Class {
/*
* Description : Bulk Account Creation
* Autor :Sreekanth Meesal */
public string name{set;get;}
public string phone{set;get;}
public string accNo{set;get;}
public string industry{set;get;}
public pageReference insertAcc(){
account a = new account();
a.Name = name;
a.Phone = phone;
a.AccountNumber = accNo;
a.Industry = industry;
list<account> accList = new list<account>();
accList.add(a);
try{
Database.SaveResult[] srList = DataBase.insert(accList,true);
for(Database.SaveResult sr:srList){
if(sr.isSuccess()){
system.debug('Success Id => '+sr.getId());
}else{
for(Database.Error err:sr.getErrors()){
system.debug('Message : fields => '+err.getMessage()+' : '+err.getFields());
}
}
}
}Catch(Exception e){
system.debug('Message : cause '+e.getMessage()+' : '+e.getCause());
}
pageReference p = new pageReference('/'+a.id);
return p;
}
}
Vf Page :-
======
<apex:page controller="Account_Class">
<apex:form >
<apex:pageblock title="Accounts Page">
<apex:pageblockButtons location="Bottom">
<apex:commandButton action="{!insertAcc}" value="Create"/>
</apex:pageblockButtons>
<apex:pageBlockSection columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name"/>
<apex:inputText value="{!name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Phone"/>
<apex:inputText value="{!phone}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account No"/>
<apex:inputText value="{!accNo}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Industry"/>
<apex:inputText value="{!industry}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Class:-
=====
/*
* Description : Bulk Account Creation
* Autor :Sreekanth Meesal
* Created Date :16/03/2023
* Last Modified Dtae :
*/
public class Account_Class {
/*
* Description : Bulk Account Creation
* Autor :Sreekanth Meesal */
public string name{set;get;}
public string phone{set;get;}
public string accNo{set;get;}
public string industry{set;get;}
public pageReference insertAcc(){
account a = new account();
a.Name = name;
a.Phone = phone;
a.AccountNumber = accNo;
a.Industry = industry;
list<account> accList = new list<account>();
accList.add(a);
try{
Database.SaveResult[] srList = DataBase.insert(accList,true);
for(Database.SaveResult sr:srList){
if(sr.isSuccess()){
system.debug('Success Id => '+sr.getId());
}else{
for(Database.Error err:sr.getErrors()){
system.debug('Message : fields => '+err.getMessage()+' : '+err.getFields());
}
}
}
}Catch(Exception e){
system.debug('Message : cause '+e.getMessage()+' : '+e.getCause());
}
pageReference p = new pageReference('/'+a.id);
return p;
}
}
Vf Page :-
======
<apex:page controller="Account_Class">
<apex:form >
<apex:pageblock title="Accounts Page">
<apex:pageblockButtons location="Bottom">
<apex:commandButton action="{!insertAcc}" value="Create"/>
</apex:pageblockButtons>
<apex:pageBlockSection columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name"/>
<apex:inputText value="{!name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Phone"/>
<apex:inputText value="{!phone}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account No"/>
<apex:inputText value="{!accNo}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Industry"/>
<apex:inputText value="{!industry}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
I don't see any issues in the code as you have used all the best practices like try-catch block, DataBase.insert , Database.SaveResult.
The only thing you could consider is checking the value being inserted for the Industry picklist. Currently, you can insert any value in this field. You could make changes such that it only takes values from existing industry picklists.
Related: https://salesforce.stackexchange.com/questions/181459/display-a-pick-list-field-on-a-visual-force-page-using-custom-controller
https://developer.salesforce.com/forums/?id=9060G000000XhJ2QAK
If this information helps, please mark the answer as best. Thank you
All Answers
I don't see any issues in the code as you have used all the best practices like try-catch block, DataBase.insert , Database.SaveResult.
The only thing you could consider is checking the value being inserted for the Industry picklist. Currently, you can insert any value in this field. You could make changes such that it only takes values from existing industry picklists.
Related: https://salesforce.stackexchange.com/questions/181459/display-a-pick-list-field-on-a-visual-force-page-using-custom-controller
https://developer.salesforce.com/forums/?id=9060G000000XhJ2QAK
If this information helps, please mark the answer as best. Thank you
Thank you for replay Swetha