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
kevin.chileskevin.chiles 

Need help with Custom Handler for Custom object

I am trying to execute this custom handler for my object, and its related child records.  For some reason (probably an obvious one) I cannot seem to get this to work correctly and keep getting errors when I go to save.  Is there any assistance that could be given?

 

public with sharing class CustomSPAPartyMasterHandler {
    public static final String relRegexName = '[<][a-zA-Z0-9\\s="^#!{}_.]*';
    public static final String endChild = '</';
    public static final String endNode = '>';
    public static final String firstMatchedChild = '(.)*?';
    public static final String regexObjField = '[{][!][\\w.]+[}]';
    public static final String emptyString = '';
    
    //Empty Constructor
    public CustomSPAPartyMasterHandler() {
        
    }
    
    //Generate XML for multiple SPA Discounts
    public static String handleSPADiscount() {
        String relName = 'relationName="#SPA_Discount__r:SPA_Discount__c#"';
        String message = '<?xml version="1.0"?><ProcessSPA><SPA><SPA_Discount relationName="#SPA_Discount__r:SPA_Discount__c#"><Name>{!SPA_Discount__r.Name}</Name><Catalog>{!SPA_Discount__r.Catalog__c}</Catalog><AWC Free Freight>{!SPA_Discount__r.AWC_F_Frt__c}</AWC Free Freight><Reason>{!SPA_Discount__r.Reason__c}</Reason><CompetitorName>{!SPA_Discount__r.Competitor_Name__c}</CompetitorName><SalesCommisionSplit>{!SPA_Discount__r.Sales_Commision_Split__c}</SalesCommisionSplit></Optionforsplitchoice>{!SPA_Discount__r.Option_For_SPlit_Choice__c}</Optionforsplitchoice><Shiptorepfirstname>{!SPA_Discount__r.Ship_To_Rep_First_Name__c}</Shiptorepfirstname><Shiptolastname>{!SPA_Discount__r.Ship_To_Rep_Last_Name__c}</Shiptolastname><Discounttype>{!SPA_Discount__r.Discount_Type__c}</Discounttype><Netprice>{!SPA_Discount__r.Net_Price__c}</Netprice><Discountlevel>{!SPA_Discount__r.Discount_Level__c}</Discountlevel><Spiff>{!SPA_Discount__r.Spiff__c}</Spiff><Netexpectedordervalue>{!SPA_Discount__r.Net_Expected_Order_Value__c}</Netexpectedordervalue><Minimumorderquantity>{!SPA_Discount__r.Minimum_Order_Quantity}</Minimumorderquantity><PartNumber>{!SPA_Discount__r.Part_Number__c}</PartNumber></SPAdiscount></ProcessSPA>';
        String SPADiscountXML = getChildXml(message, relName);
        List<SPA_Discount__c> SPADiscountLst = null;
    
        String spQuery= 'Select  sp.Id, ' +
                    ' (Select Name, SPA_Agreement__c,Catalog__c, Reason__c, Competitor_Name__c, Sales_Commision_Split__c, Option_For_Split_Choice__c, Ship_To_Rep_First_Name__c, Ship_To_Rep_Last_Name__c, AWC_F_Frt__c, Part_Number__c,Minimum_Order_Quantity,Net_Expected_Order_Value__c,Spiff__c,Discount_Level__c,Net_Price__c,Discount_Type__c,Ship_To_Rep_Last_Name__c' + 
                    ' From SPA_Discount__r) From SPA__c sp';
        list<SPA__c> lstSPA = database.query(spQuery);
        if(lstSPA != null && lstSPA .size()  > 0 ){
            SPA__c SPA= lstSPA [0];
            SPADiscountLst = lstSPA[0].SPA_Discount__r; 
        }
        if(SPADiscountLst !=null && !SPADiscountLst.isEmpty()) {
            List<String> fields = getFieldsFromXmlPart(SPADiscountXML);
            if(fields != null && !fields.isEmpty()) {
                String childXml;
                String allChildXml = '';
                for(SPA_Discount__c spadiscount: SPADiscountLst){
                    childXml = SPADiscountXML;
                    for(Integer cnt=0;cnt<fields.size();cnt++){
                        String objName = fields[cnt].split('\\.')[0];
                        string fldname = fields[cnt].split('\\.')[1];       
                        childXml = childXml.replace('{!'+fields[cnt]+'}',
                            htmlEncode(String.valueOf(personBillingAddress.get(fldname))));
                    }
                    allChildXml = allChildXml + childXml;
                }
                
                if(allChildXml!='')
                {
                    allChildXml = allChildXml.replace(' ' + relName, emptyString);
                    message = message.replace(SPADiscountXML, allChildXml);
                }
            }
        }
        return message;
    }
    
    //get SPA_Discount from the main XML
    public static String getChildXml(String message, String searchString) {
        Pattern patt;
        Matcher mat;
        string regex = relRegexName+searchString+endNode;
        patt =Pattern.compile(regex);
        system.debug('--- search string>>' + regex);
        mat=patt.matcher(message);
        String elm;
        if(mat.find()){
            elm = mat.group(0);
        }
        System.debug('mat.grp>> '+ elm);
        if(elm!=null && elm!=''){
            elm=elm.replace('>','/>');
            DOM.Document domDoc = new DOM.Document();
            domDoc.load(elm);
            Dom.XmlNode rootXmlNode = domDoc.getRootElement();
            String rootNodeName = rootXmlNode.getName();
            System.debug(rootNodeName);
            String endTag = endChild + rootNodeName + endNode;
            regex = relRegexName+searchString+endNode+firstMatchedChild+endTag;
            System.debug('>>>full regex: '+regex);
            patt =Pattern.compile(regex);
            mat=patt.matcher(message);
            if(mat.find()){
                elm = mat.group(0);
            }
            System.debug('mat.grp>> '+ elm);
        }
        return elm;
    }
    
    //get fields from xml
    public static List<String> getFieldsFromXmlPart(String xml) {
        Pattern patt;
        Matcher mat;
        List<String> fields = new List<String>();
        patt = Pattern.compile(regexObjField);
        System.debug('xml>>>>' + xml);
        if(xml != '' && xml != null) {
            mat = patt.matcher(xml);
            while(mat.find()){
                String fieldName =  mat.group();
                fieldName = fieldName.replace('{!','');
                fieldName = fieldName.replace('}','');
                fields.add(fieldName);
            }
        } 
        return fields;
    }
    
    public static string htmlEncode(String str) {
        if(!StringUtil.isNull(str))
            return str.Replace('&', '&amp;').Replace('<', '&lt;').Replace('>', '&gt;').Replace('\"', '&quot;').Replace('\'', '&apos;');
        else return str;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
KrishHariKrishHari

There is no built-in class named 'StringUtil'. If you are using your own library or open-source libraries like this, then make sure you name it correctly. For e.g. there is a class named 'StringUtils' in the apex-lang library.

 

Regards,

HK.

All Answers

KrishHariKrishHari

Can you specify what error you see when you save it?

 

 

kevin.chileskevin.chiles

it was giving me an unspecified field error.  But now I was able to work past this and am stuck with the error 

 

Error: Compile Error: Variable does not exist: StringUtil at line 108 column 12

 

Here is the latest version of the code I am on.

 

public with sharing class CustomSPAPartyMasterHandler {
    public static final String relRegexName = '[<][a-zA-Z0-9\\s="^#!{}_.]*';
    public static final String endChild = '</';
    public static final String endNode = '>';
    public static final String firstMatchedChild = '(.)*?';
    public static final String regexObjField = '[{][!][\\w.]+[}]';
    public static final String emptyString = '';
    
    //Empty Constructor
    public CustomSPAPartyMasterHandler() {
        
    }
    
    //Generate XML for multiple SPA Discounts
    public static String handleSPADiscount() {
        String relName = 'relationName="#SPA_Discount__r:SPA_Discount__c#"';
        String message = '<?xml version="1.0"?><ProcessSPA><SPA><SPA_Discount relationName="#SPA_Discount__r:SPA_Discount__c#"><Name>{!SPA_Discount__r.Name}</Name><Catalog>{!SPA_Discount__r.Catalog__c}</Catalog><AWC Free Freight>{!SPA_Discount__r.AWC_F_Frt__c}</AWC Free Freight><Reason>{!SPA_Discount__r.Reason__c}</Reason><CompetitorName>{!SPA_Discount__r.Competitor_Name__c}</CompetitorName><SalesCommisionSplit>{!SPA_Discount__r.Sales_Commision_Split__c}</SalesCommisionSplit></Optionforsplitchoice>{!SPA_Discount__r.Option_For_SPlit_Choice__c}</Optionforsplitchoice><Shiptorepfirstname>{!SPA_Discount__r.Ship_To_Rep_First_Name__c}</Shiptorepfirstname><Shiptolastname>{!SPA_Discount__r.Ship_To_Rep_Last_Name__c}</Shiptolastname><Discounttype>{!SPA_Discount__r.Discount_Type__c}</Discounttype><Netprice>{!SPA_Discount__r.Net_Price__c}</Netprice><Discountlevel>{!SPA_Discount__r.Discount_Level__c}</Discountlevel><Spiff>{!SPA_Discount__r.Spiff__c}</Spiff><Netexpectedordervalue>{!SPA_Discount__r.Net_Expected_Order_Value__c}</Netexpectedordervalue><Minimumorderquantity>{!SPA_Discount__r.Minimum_Order_Quantity}</Minimumorderquantity><PartNumber>{!SPA_Discount__r.Part_Number__c}</PartNumber></SPA></ProcessSPA>';
        String SPADiscountXML = getChildXml(message, relName);
        List<SPA_Discount__c> SPADiscountLst = null;
    
        String spQuery= 'Select  SPA.Id, ' +
                    ' (Select Name, SPA_Agreement__c,Catalog__c, Reason__c, Competitor_Name__c, Sales_Commision_Split__c, Option_For_Split_Choice__c, Ship_To_Rep_First_Name__c, Ship_To_Rep_Last_Name__c, AWC_F_Frt__c, Part_Number__c,Minimum_Order_Quantity,Net_Expected_Order_Value__c,Spiff__c,Discount_Level__c,Net_Price__c,Discount_Type__c,Ship_To_Rep_Last_Name__c' + 
                    ' From SPA_Discount__r) From SPA__c SPA';
        list<SPA__c> lstSPA = database.query(spQuery);
        if(lstSPA != null && lstSPA .size()  > 0 ){
            SPA__c SPA= lstSPA [0];
            SPADiscountLst = lstSPA[0].SPA_Discounts__r; 
        }
        if(SPADiscountLst !=null && !SPADiscountLst.isEmpty()) {
            List<String> fields = getFieldsFromXmlPart(SPADiscountXML);
            if(fields != null && !fields.isEmpty()) {
                String childXml;
                String allChildXml = '';
                for(SPA_Discount__c spadiscount: SPADiscountLst){
                    childXml = SPADiscountXML;
                    for(Integer cnt=0;cnt<fields.size();cnt++){
                        String objName = fields[cnt].split('\\.')[0];
                        string fldname = fields[cnt].split('\\.')[1];       
                        childXml = childXml.replace('{!'+fields[cnt]+'}',
                            htmlEncode(String.valueOf(SPADiscount.get(fldname))));
                    }
                    allChildXml = allChildXml + childXml;
                }
                
                if(allChildXml!='')
                {
                    allChildXml = allChildXml.replace(' ' + relName, emptyString);
                    message = message.replace(SPADiscountXML, allChildXml);
                }
            }
        }
        return message;
    }
    
    //get SPA_Discount from the main XML
    public static String getChildXml(String message, String searchString) {
        Pattern patt;
        Matcher mat;
        string regex = relRegexName+searchString+endNode;
        patt =Pattern.compile(regex);
        system.debug('--- search string>>' + regex);
        mat=patt.matcher(message);
        String elm;
        if(mat.find()){
            elm = mat.group(0);
        }
        System.debug('mat.grp>> '+ elm);
        if(elm!=null && elm!=''){
            elm=elm.replace('>','/>');
            DOM.Document domDoc = new DOM.Document();
            domDoc.load(elm);
            Dom.XmlNode rootXmlNode = domDoc.getRootElement();
            String rootNodeName = rootXmlNode.getName();
            System.debug(rootNodeName);
            String endTag = endChild + rootNodeName + endNode;
            regex = relRegexName+searchString+endNode+firstMatchedChild+endTag;
            System.debug('>>>full regex: '+regex);
            patt =Pattern.compile(regex);
            mat=patt.matcher(message);
            if(mat.find()){
                elm = mat.group(0);
            }
            System.debug('mat.grp>> '+ elm);
        }
        return elm;
    }
    
    //get fields from xml
    public static List<String> getFieldsFromXmlPart(String xml) {
        Pattern patt;
        Matcher mat;
        List<String> fields = new List<String>();
        patt = Pattern.compile(regexObjField);
        System.debug('xml>>>>' + xml);
        if(xml != '' && xml != null) {
            mat = patt.matcher(xml);
            while(mat.find()){
                String fieldName =  mat.group();
                fieldName = fieldName.replace('{!','');
                fieldName = fieldName.replace('}','');
                fields.add(fieldName);
            }
        } 
        return fields;
    }
    
    public static string htmlEncode(String str) {
        if(!StringUtil.isNull(str))
            return str.Replace('&', '&amp;').Replace('<', '&lt;').Replace('>', '&gt;').Replace('\"', '&quot;').Replace('\'', '&apos;');
        else return str;
    }
}

 

 

 

KrishHariKrishHari

There is no built-in class named 'StringUtil'. If you are using your own library or open-source libraries like this, then make sure you name it correctly. For e.g. there is a class named 'StringUtils' in the apex-lang library.

 

Regards,

HK.

This was selected as the best answer