Tag Archives: sObject

sObjects That Don’t Support DML Operations

Biswajeet   August 6, 2018   0 Comments

Following standard objects don’t support DML operations, but it supports SOQL query.

  • AccountTerritoryAssignmentRule
  • AccountTerritoryAssignmentRuleItem
  • ApexComponent
  • ApexPage
  • BusinessHours
  • BusinessProcess
  • CategoryNode
  • CurrencyType
  • DatedConversionRate
  • NetworkMember (allows update only)
  • ProcessInstance
  • Profile
  • RecordType
  • SelfServiceUser
  • StaticResource
  • Territory2
  • UserAccountTeamMember
  • UserTerritory
  • WebLink

Convert Salesforce sObject Record To JSON

Biswajeet   December 29, 2016   0 Comments

Apex Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class ConvertsObjectToJSON
{
    //Return the JSON string from record Id
    public static string getJsonFromSObject(Id recordId)
    {
        String jsonData = '';
        try{
            if(String.isNotBlank(recordId))
            {
                String sObjectFields = '';
                 
                //Get sObject Name
                String objName = recordId.getSObjectType().getDescribe().getName();
                 
                //Getting the fields information
                Map<String, Schema.sObjectField> sObjectFieldMap = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();
                 
                //Map key is the field API name and value is the field data type.
                Map<String, String> fieldMap = new Map<String, String>();
                 
                for(Schema.SObjectField sfield: sObjectFieldMap.Values()){
                    Schema.DescribeFieldResult fieldDesc = sfield.getDescribe();
                    fieldMap.put(fieldDesc.getName(), fieldDesc.getType().name());
                }
                 
                //Create query with all fields
                for(String field: fieldMap.keySet()){
                    sObjectFields += field+',';
                }
                sObjectFields = sObjectFields.removeEnd(',');
                 
                //Dynamic SOQL Query with all fields
                String soqlQuery = 'SELECT '+ sObjectFields +' FROM '+objName+' WHERE Id =: recordId';
                 
                //Execute the SOQL query
                sObject sObj = Database.Query(soqlQuery);
                 
                //Create JSON
                JSONGenerator gen = JSON.createGenerator(true);
                gen.writeStartArray();
                gen.writeStartObject();
                gen.writeFieldName('attributes');
                gen.writeStartObject();
                gen.writeStringField('type', objName);
                gen.writeEndObject();
                gen.writeFieldName('fields');
                gen.writeStartObject();
                 
                for(String field: fieldMap.keySet()){
                    if(sObj.get(field) != null){
                        gen.writeStringField(field, String.ValueOf(sObj.get(field)));
                    }
                    else{
                        gen.writeStringField(field, '');
                    }
                }
                 
                gen.writeEndObject();
                gen.writeEndObject();
                gen.writeEndArray();
                //Getting the JSON String Data
                jsonData = gen.getAsString();
            }
        }
        catch(Exception ex){
             
        }
        return jsonData;
    }
}

Invoke the method:

1
2
String jsonData = ConvertsObjectToJSON.getJsonFromSObject('001B000000pA7sV');
system.debug('jsonData-' + jsonData);

Get All Required Fields of sObject Using Apex in Salesforce

Biswajeet   April 29, 2015   0 Comments

Sample Code:

Here in below code I’m getting all required fields of “Account” object.

1
2
3
4
5
6
7
8
9
10
11
12
13
Map<String, Schema.SObjectType> mapObj  = Schema.getGlobalDescribe();
Schema.SObjectType sObjType = mapObj.get('Account');
Schema.DescribeSObjectResult objDescribe = sObjType.getDescribe();
Map<String,Schema.SObjectField> mapFields = objDescribe.fields.getMap();
List<String> requiredFieldList = new List<String>();
 
for(String obj : mapFields.keyset()) {
    Schema.DescribeFieldResult describeField = mapFields.get(obj).getDescribe();
    if (describeField.isCreateable()  && !describeField.isNillable() && !describeField.isDefaultedOnCreate()) {
        requiredFieldList.add(obj);
        System.debug(obj);
    }
}