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
prashant1985prashant1985 

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;}

jbroquistjbroquist

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?

prashant1985prashant1985
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.
Vinit_KumarVinit_Kumar

Agreed with Shaliesh,

 

you can declare like below

 

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

prashant1985prashant1985
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
{

get
{
CompanyName = 'Not Available';
return CompanyName;
}
set
{
// 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'.