+ Start a Discussion

how to set default value

  i hwant to set default value below is code

but its give error,

"Compile Error: expecting right curly bracket, found '[' at line"


[DefaultValue('Not Available')]
     public String CompanyName{get; set;}


I've never seen that default value syntax before, at am not sure of whether or not it's supported. Have you tried setting the default value in the class constructor instead?

in c# its work. i think in apex also have such procedure to set default value. actually i need to set default value to all such variable in class. so if have solution tell me
Shailesh DeshpandeShailesh Deshpande
For which property are you trying to set the defualt value? Is it CompanyName? If yes, you can set it a default value in the constructor or at the time you declare this variable.

Agreed with Shaliesh,


you can declare like below


public final String str= 'India'; // I have assigned India to str variable

public class Company
public String ETag {get; set;}
public String Id{get; set;}
public Link[] Links{get; set;}
Public String Address{get; set;}
Public AddressParsed AddressParsed{get; set;}
public String City{get; set;}
public String YearEstablished{get; set;}
public string BusinessStatus {get; set;}

// [DefaultValue('Not Available')]
public String CompanyName{get; set;}
/* public String CompanyName

CompanyName = 'Not Available';
return CompanyName;
// if(CompanyName == null)
// {
// CompanyName = 'Not Available';
// }
// else
// {
//CompanyName = CompanyName;
public String FirstName{get; set;}
public String LastName{get; set;}
public Location Location{get; set;}
public String Phone{get; set;}
public String PostalCode{get; set;}
public String Website{get; set;}
public String StateProvince{get; set;}
public c_Contact[] Contacts{get; set;}
public String Neighborhood{get; set;}

public Integer CorporateEmployeesSizeActual{get; set;}
public String CorporateEmployeesSizeRange{get; set;}
//public Integer CorporateSalesVolumeActual{get; set;}
public Decimal CorporateSalesVolumeActual{get; set;}
public String CorporateSalesVolumeRange{get; set;}
public String County{get; set;}

public String CreditRatingScore{get; set;}

public String EIN1{get; set;}
public String EIN2{get; set;}
public String EIN3{get; set;}
public String Fax{get; set;}
public String Gender{get; set;}
public Integer LocationEmployeesSizeActual{get; set;}
public String LocationEmployeesSizeRange{get; set;}
public Naics[] NaicsList{get; set;}
public String PrimaryNaics{get; set;}
public String PrimaryNaicsDescription{get; set;}
public String PrimarySic{get; set;}
public String PrimarySicDescription{get; set;}
public Sic[] SicList{get; set;}
public String Title{get; set;}
public String TollFree{get; set;}
public UriCls[] UriList{get; set;}
public String ZipPlus4{get; set;}
public String BusinessType{get; set;}

//Additional Property for Companies API

public String Franchise{get; set;}
public String LocationSalesVolumeActual{get; set;}
public String LocationSalesVolumeRange{get; set;}
public String TickerSymbol{get; set;}

//Additional Property for Match API
public String RuleName{get; set;}
public Integer Score{get; set;}
public String IsHomeBusiness{get; set;}

public EmailVendor emailVendor {get; set;}

public Company()
this.Links = new List<Link>();
this.CompanyName = 'Not Available';
this.CreditRatingScore = 'Not Available';
this.TickerSymbol = 'Not Available';


above is my code i assign value in constructor but its not work.

i want to dispaly default value if there is null value.
if company doest have any value then its display 'Not Available'.