salesforce中的常用的基本變量和集合的常用方法

小瘋最近做的一個項目使用的salesforce,小瘋作爲一個新上手的初學者,在使用中學習salesforce的Apex。其中Apex的語法和Java的很類似。

常用的基本變量:Integer、String、Decimal、Double、Long、Boolean、ID、Object

常用的集合:List、Set 、Map

sObject定義:salesforce中的標準對象或者自定義對象在Apex中使用時被稱作“sObject"。sObject對象的一個實例相當於salesforce中的一條記錄。

在工作中使用上述變量或者集合時,作爲新手對於其中的一些封裝的方法還不熟悉,對於工作不是很方便,所以在這裏記錄一下。一是爲了加深記憶二是方便使用的時候可以提供一個參考。

一、Integer

public String format( )  // Integer轉化爲String
public static Integer valueOf(String stringToInteger)  // String轉化爲Integer

二、Long
public String format( )  // Long轉化爲String
public Integer intValue( )  // 轉化爲Integer
public static Long valueOf(String stringToLong)  // String轉化爲Long

三、Decimal

public Decimal abs()  //返回小數點的絕對值
public Decimal divide(Decimal divisor, Integer scale)  // 除數爲divisor 返回結果爲scale位數
public Double doubleValue() // 轉double
public String format() //  轉string
public Integer intValue()  //轉Integr
public Long longValue()  //轉Long
public Integer precision()  //返回 值對應的數字個數  如 3.2   返回爲2   即3和2
public Long round() //轉換Long 四捨五入
public Integer scale() //返回小數點後的位數個數
public Decimal setScale(Integer scale) //設置小數點後的位數個數
public Decimal stripTrailingZeros() // 去除0以後的小數
Decimal.valueOf(Object objectToDecimal)// 將Double  Long String 等轉換成Decimal
Decimal.toPlainString()// 轉化爲科學計數法的String值

四、Boolean

public static Boolean valueOf(String stringToBoolean)

五、Double

public static Double valueOf(String stringToDouble)
public Long round() // 四捨五入返回Long
public Integer intValue() //轉 Integer
public String format()// 轉String
public Long longValue()//轉Long

六、ID

ID類型可以用任何一個符合規則的18位字符表示,如果你設置ID字符爲15位,則將字符自動擴展成18位。不符合規則的ID字符在運行時則運行時異常。

public static ID valueOf(String toID)   //將toId轉換成ID
public Boolean equals(String id)   //判斷兩個ID是否相同

七、String

//返回簡化字符串,maxWidth>自身長度?自身:maxWidth長度的字符串加上省略號,省略號佔3個字符
public String abbreviate(Integer maxWidth)  
//注意:maxWidth如果小於4,則拋出Runtime Exception
public String abbreviate(Integer maxWidth,Integer offset) //返回簡化字符串,maxWidth爲需要簡化長度,offset爲字符串簡化起點
//如果max太小,則拋出Runtime Exception
public String capitalize()     //返回當前字符串,其中第一個字母改爲標題(大寫)。
public String center(Integer size)  //返回指定大小字符串使原字符串位於中間位置(空格填充左右),如果size<字符串長度,則不起作用
public String center(Integer size,String paddingString)  //返回指定大小字符串使原字符串處於中間位置。參數1:字符串顯示長度;參數2:填充的字符樣式
public Integer charAt(Integer index)   //返回對應值得ASC碼值
public Integer codePointAt(Integer index)   //返回指定位置的值對應的Unicode編碼
public Integer codePointBefore(Integer index)   //返回指定位置的值前一個對應的Unicode編碼
public Integer codePointCount(Integer beginIndex,Integer endIndex)  //返回起始位置到截至位置字符串的Unicode編碼值
public Integer compareTo(String secondString)  //基於Unicode比較兩個字符串大小,如果小於比較值返回負整數,大於返回正整數,等於返回0
public Boolean contains(String substring) //判斷是否包含某個字符串,包含返回true,不包含返回false    
public Boolean containsAny(String inputString)   //判斷是否包含inputString任意一個字符,包含返回true,不包含返回false
public Boolean containsIgnoreCase(String inputString)    //判斷是否包含inputString(不區分大小寫),包含返回true,不包含返回false
public Boolean containsNone(String inputString)   //判斷是否不包含inputString,不包含返回true,包含返回false
public Boolean containsOnly(String inputString)  //當前字符串從指定序列只包括inputString返回true,否則返回false
public Boolean containsWhitespace()  //判斷字符串是否包含空格,包含返回true,不包含返回false
public Integer countMatches(String substring)  //判斷子字符串在字符串中出現的次數
public String deleteWhitespace()  移除字符串中所有的空格  //返回兩個字符串之間不同,如果anotherString爲空字符串,則返回空字符串,如果anotherString爲null,則拋異常     比較結果以anotherString爲基準,從第一個字符比較,不相同則返回anotherString與源字符串不同之處
public Boolean endsWith(String substring)  //判斷字符串是否已substring截止,如果是返回true,否則返回false
public Boolean endsWithIgnoreCase(String substring)  //判斷字符串是否已substring截止(不區分大小寫),如果是返回true,否則返回false
public Boolean equals(Object anotherString)  //判斷字符串是否和其他字符串相同
public Boolean equalsIgnoreCase(String anotherString)  //判斷字符串是否和其他字符串相同(不區分大小寫)
public static String format(String stringToFormat, List<String> formattingArguments)  //將轉換的參數替換成相同方式的字符串中
public static String fromCharArray(List<Integer> charArray)   //將char類型數組轉換成String類型
public List<Integer> getChars()   //返回字符串的字符列表
public static String getCommonPrefix(List<String> strings)  //獲取列表共有前綴
public Integer hashCode()   //返回字符串的哈希值
public Integer indexOf(String substring)  //返回substring第一次在字符串中出現的位置,如果不存在返回-1
public Integer indexOf(String substring,Integer index)  //返回substring第一次在字符串中出現的位置,起始查詢字符串的位置爲index處
public Integer indexOfAny(String substring)  //substring任意一個字符第一次在字符串中出現的位置
public Integer indexOfAnyBut(String substring)  //返回substring任意一個字符不被包含的第一個位置,無則返回-1
public Integer indexOfChar(int char)  //返回字符串中char字符最先出現的位置
public Integer indexOfDifference(String compareTo)  //返回兩個字符串第一個不同位置的座標
public Integer indexOfIgnoreCase(String substring)  //返回substring在字符串中第一個出現的位置(不考慮大小寫)
public Boolean isAllLowerCase()  //字符串是否均爲小寫,如果是返回true,否則返回false
public Boolean isAllUpperCase()  //字符串是否均爲大寫,如果是返回true,否則返回false
public Boolean isAlpha()   //如果當前所有字符均爲Unicode編碼,則返回true,否則返回false
public Boolean isAlphanumeric()  //如果當前所有字符均爲Unicode編碼或者Number類型編碼,則返回true,否則返回false
public Boolean isAlphanumericSpace()  //如果當前所有字符均爲Unicode編碼或者Number類型或者空格,則返回true,否則返回false
public Boolean isAlphaSpace()  //如果當前所有字符均爲Unicode或者空格,則返回true,否則返回false
public Boolean isAsciiPrintable()  //如果當前所有字符均爲可打印的Asc碼,則返回true,否則返回false
public Boolean isNumeric()  //如果當前字符串只包含Unicode的位數,則返回true,否則返回false
public Boolean isWhitespace()  //如果當前字符只包括空字符或者空,則返回true,否則返回false
public static String join(Object iterableObj, String separator)   //通過separator連接對象,通用於數組,列表等
public Boolean lastIndexOf(String substring)   //substring在字符串中最後出現的位置,不存在則返回-1
public Boolean lastIndexOfIgnoreCase(String substring)  //substring在字符串中最後出現的位置(忽略大小寫),不存在則返回-1
public String left(Integer index)    //獲取從零開始到index處的字符串
public String leftPad(Integer length)   //返回當前字符串填充的空間左邊和指定的長度
public Integer length()    //返回字符串長度
public String mid(Integer startIndex,Integer length);  //返回新的字符串,第一個參數爲起始位,第二個字符爲字符的長度//類似於substring
public String normalizeSpace()    //前後刪除空白字符
public String remove(String substring)   //移除所有特定的子字符串,並返回新字符串
public String removeIgnorecase(String substring)   //移除所有特定的子字符串(忽略大小寫),並返回新字符串
public String removeEnd(String substring)   //當且僅當子字符串在後面移除子字符串
public String removeStatrt(String substring)   //當且僅當子字符串在後面移除子字符串
public String repeat(Integer numberOfTimes)   //重複字符串numberOfTimes次數
public String repeat(String separator, Integer numberOfTimes)   //重複字符串numberOfTimes次,通過separator作爲分隔符
public String replace(String target, String replacement)   //將字符串中的target轉換成replacement
public String replaceAll(String target,String replacement)
public String reverse()  //字符串倒序排列
public String right(Integer length)   //從右查詢返回length的個數的字符串
public String[] split(String regExp)   //通過regExp作爲分隔符將字符串分割成數組
public String[] split(String regExp, Integer limit)   //通過regExp作爲分隔符將字符串分割成數組,limit顯示數組個數
public Boolean startsWith(string substring)   //判斷字符串是否以substring開頭,如果是返回true,不是返回false
public String substring(Integer length)    //截取字符串固定長度
public String toLowerCase()   //將字符串轉換成小寫
public String toUpperCase()   //將字符串轉換成大寫
public String trim()   //去字符串左右空格
public String uncapitalize()    //將字符串第一個轉換成小寫
public static String valueOf(Object objectToConvert)   //將Object類型轉換成String類型,其中Object類型包括 Date,DateTime,Decimal,Double,Integer,Long,Object

八、Datetime

Datetime nowDatetime = Datetime.now();
Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0);   //初始化
Datetime datetime2 = Datetime.parse(datetimeString);
Datetime datetime3 = Datetime.valueOf(datetimeString);
String datetimeString = '2016-3-1 PM14:38';
datetime1.format('yyyy-MM-dd HH:mm:ss'));   //格式化
年月日時分秒操作
datetime1 = datetime1.addDays(1);
datetime1 = datetime1.addMonths(1);
datetime1 = datetime1.addYears(1);
datetime1 = datetime1.addHours(1);
datetime1 = datetime1.addMinutes(1);
datetime1 = datetime1.addSeconds(1);
Date date1 = datetime1.date();
Date dateGmt = datetime1.dateGmt();
//對應時間獲取
Integer year = datetime1.year();
Integer yearGmt = datetime1.yearGmt();
Integer month = datetime1.month();
Integer monthGmt = datetime1.monthGmt();
Integer day = datetime1.day();
Integer dayGmt = datetime1.dayGmt();
Integer dayOfYear = datetime1.dayOfYear();
Integer dayOfYearGmt = datetime1.dayOfYearGmt();
Integer hour = datetime1.hour();
Integer hourGmt = datetime1.hourGmt();
Integer minute = datetime1.minute();
Integer minuteGmt = datetime1.minuteGmt();
Integer second = datetime1.second();
Integer secondGmt = datetime1.secondGmt();

九、date

Date date2 = Date.today();
Date date3 = Date.newInstance(2016,3,1);
String dateString = '2016-3-1';
Date date4 = Date.parse(dateString);
Date date5 = Date.valueOf(dateString);
date3.format() //格式化
System.debug('通過newInstance實例化:' + date3.format());
System.debug('通過parse實例化:' + date4.format());
System.debug('通過valueOf實例化:' + date5.format());
Integer daysBetween = date3.daysBetween(date4);    //  相差天數 date4-date3
date4和date5是否相同日期   date4.isSameDay(date5)
date3和date4相差月數  date3.monthsBetween(date4)
date3.toStartOfMonth().format()); //調用toStartOfMonth執行值    //返回本月第一天
public Date toStartOfWeek()   //返回本月第一個週日,如果本月1日非週日,則返回上月最晚的週日

十、List

List代表一類的有序數據列表。數據序號從0開始。與JAVA不同的是:List是一個類,並且不存在ArrayList等子類。即實例化
eg:List list1 = new List();
List可以通過自身構造函數實例化,也可以通過數組進行實例化。

List<String> lists = new String[]{'1','3'};
List<String> list1 = new String[] {'5','4'};
lists.set(0,'a');
lists.add(0,'b');
lists.add('2');
lists.addAll(list1);
//lists.sort();
for(String item : lists) {
}
Iterator<String> iterator = lists.iterator();
while(iterator.hasNext()) {
   String item = iterator.next();
}
if(lists.size() > 0) {
   Integer listSize = lists.size();
   lists.remove(listSize-1);
}
List<String> cloneList = lists.clone();
for(Integer i=0;i<cloneList.size();i++) {
   System.debug('cloneListItem : ' + cloneList.get(i));
}
lists.clear();
if(lists.size() > 0) {
   for(String item : lists) {
       System.debug('set item : ' + item);
   }
} else {
   System.debug('lists has already clear');
}

十一、set

Set代表一類數據的無序列表。與JAVA不同的是:Set是一個類,不存在HashSet等子類。即實例化
eg:Set<String> set1 = new Set<String>();

方法:

Set<String> set1 = new Set<String>();
set1.add('1');
set1.add('2');
Set<String> set2 = set1.clone();
Boolean isEquals = set1.equals(set2);
Boolean isContains = set1.contains('1');
Integer setSize = set1.size();
Iterator<String> iterator2 = set1.iterator();
while(iterator2.hasNext()) {
      System.debug('set item:' + iterator2.next());
}
Boolean isEmpty = set1.isEmpty();
//set1.remove('1');
List<String> anotherList = new String[] {'1','3'};
//public Boolean retainAll(List<Object> list)
//譯:set值爲list中和set重複的內容,如果沒有重複的內容,則set爲空
set1.retainAll(anotherList);
Iterator<String> iterator3 = set1.iterator();
while(iterator3.hasNext()) {
    System.debug('set item via retainAll:' + iterator3.next());//1,List與Set公共部分
}

十二、Map<key, value>

Map代表着鍵值對,與JAVA用法類似,區別爲Map是一個類,不是接口,不存在HashMap<K,V>等子類。

Map<String,Object> map1 = new Map<String,Object>();
map1.put('key1','value1');
map1.put('key2','value2');
map1.put('key3','value3');
Boolean isContainsKey = map1.containsKey('key1');
Map<String,Object> map2 = map1.clone();
Boolean isMapEquals = map1.equals(map2);
Set<String> keySet = map1.keySet();
String value1 = (String)map1.get('key1');
List<String> valuesList = (List<String>)map1.values();
Integer mapSize = map1.size();

小瘋在這裏只記載了常用的方法,並沒有把方法全部列出來,在這裏希望可以幫到瀏覽這篇的初學者,也希望有大拿可以指導小弟salesforce方面的知識!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章