Wednesday, August 26, 2020

Parse XML Using Apex in Salesforce

 Sample XML

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://xxx.xxx-xx.org/xx/ns/xx">
    <edmx:DataServices>
        <Schema Namespace="Person.Entities" xmlns="http://xx.xx-xxorg/xx/ns/xx">
            <EntityType Name="PersonInfo">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" />
                <Property Name="Name"/>
                <Property Name="FirmType"/>
                <Property Name="WebSite"/>
                <Property Name="Description"/>
                <Property Name="Address" />
                <Property Name="Address1" />
                <Property Name="HQCity" />
                <Property Name="State"/>
                <Property Name="Zip"/>
                <Property Name="CountryCode" />
                <Property Name="Phone"/>
                <Property Name="NetPatientRev"/>
                <Property Name="NetworkId"/>
                <Property Name="NetworkParentId"/>
                <Property Name="NetworkName"/>
            </EntityType>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>
Sample Code:
StaticResource res = [SELECT Id, Body From StaticResource WHERE Name = 'SmapleXML'];

String strBody = res.Body.toString();
Dom.Document doc = new Dom.Document();
doc.load(strBody);

DOM.XmlNode rootNode = doc.getRootElement();
list<String> lstNames = new list<String>();
list<DOM.xmlNode> lst = rootNode.getChildElements();

for(DOM.XmlNode re : lst) {
    if(re.getName() == 'DataServices') {
        for(DOM.XmlNode re1 : re.getChildElements()) {
            if(re1.getName() == 'Schema') {
                for(DOM.XmlNode re2 : re1.getChildElements()) {
                    if(re2.getName() == 'EntityType') {
                        if(re2.getAttribute('Name', '') == 'PersonInfo') {
                            for(DOM.XmlNode re3 : re2.getChildElements()) {
                                if(re3.getName() == 'Property') {
                                    lstNames.add(re3.getAttribute('Name', ''));
                                }
                            }
                        }
                    }
                }	
            }
        }
    }
}

System.debug('lstNames ==> '+lstNames);
Result:

Monday, August 24, 2020

How to deploy process builder using Apache ANT Tool in Salesforce?

 Package.xml

<?xml version="1.0" encoding="UTF-8"?>  
  
<Package xmlns="http://soap.sforce.com/2006/04/metadata">  
    <types>    
        <members>API_Name-2</members>   
        <name>Flow</name>    
    </types>  
    <version>46.0</version>  
</Package>  
This will retrieve a version 2 of the "API_Name" process.

Wednesday, July 15, 2020

Calculate Days and hours and minutes from two dates using apex in Salesforce


Sample Code
DateTime startDate = DateTime.now();
DateTime endDate = DateTime.now().addDays(2).addHours(5);
map<String, Integer > diffMap = new Map<String, Integer>();
Long startDateTime = startDate.getTime();
Long endDateTime = endDate.getTime();
Decimal sdiffMilliSecs = Decimal.valueOf(endDateTime - startDateTime);
Decimal sdDays = sdiffMilliSecs/1000/60/60/24;
Integer siDays = Integer.valueOf(math.floor(sdDays));
Decimal sremainderDays = sdDays- siDays;

Decimal sdHours = sremainderDays * 24;
Integer siHours = Integer.valueOf(math.floor(sdHours));
Decimal sremainderHours = sdHours - siHours;

Decimal sdMinutes = sremainderHours * 60;
Integer siMinutes = Integer.valueOf(math.floor(sdMinutes));
Decimal sremainderMinutes = sdMinutes - siMinutes;
diffMap.put('Days', siDays);
diffMap.put('Hours', siHours);
diffMap.put('Minutes', siMinutes);

System.debug('diffMap ==> '+diffMap);
Output