• Santanu Roy 18
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 8
    Replies
Hi All,
We have a requirement of masking all email by appending .invalid with all the custom settings emails . I have written this below code but I am unable to convert this to batch class .Can somebody help me to convert it please . I am new to batch apex hence facing the challenges .

public class Post_Copy_CustomSettings_Email {
    //TODO handle encodingType for SPARK AGN_SPARK_USER_Settings custom settings
    String emailUTFEncoding = 'UTF-8';
    public void UpdateCustomSettingsEmails(){
        Map < String, Schema.SObjectType > gd = Schema.getGlobalDescribe();        
        for ( String objectName : gd.keySet() ) {            
            Schema.SObjectType result = gd.get( objectName );            
            if ( result.getDescribe().isCustomSetting() ) {                
                String query = 'SELECT ';
                List <String> listFields = new List <String>();
                Map <String, Schema.SObjectField> objectFields = result.getDescribe().fields.getMap();
                for (String s : objectFields.keySet()) {                    
                    if (String.valueOf( objectFields.get(s).getDescribe().getType()) == 'Email' || String.valueOf(objectFields.get(s).getDescribe().getType()) == 'String'){ 
                         //&& Integer.valueOf(objectFields.get(s).getDescribe().getLength()) > 100)) {                         
                        //System.debug( 'Iterating the email fields and retrieving');
                        query += s + ',';
                        listFields.add(s);                            
                    }
                }
                if (listFields.size() > 0 ) {
                    query = query.removeEnd( ',' );
                    query += ' FROM ' + objectName;
                    List<sObject> listRecords = Database.query( query );
                    //if (listRecords.size() > 0 ) {
                    for (sObject obj : listRecords ) {
                        for (String strField : listFields ) {
                            if (obj.get(strField) != null && PostCopyValidateUtil.validateEmail(String.valueOf(obj.get(strField)))){ //Regex to compare value if email or not
                                obj.put(strField, obj.get(strField) +'.invalid');                                
                            }
                        }
                        try{
                            //update listRecords;
                        }
                        Catch(DmlException exp){
                            System.debug('DML operation has failed');
                        } 
                        
                        
                        
                        //}
                    }
                }
                
                /*BatchJobProcess_CustomMetadata custMetadataJob = new BatchJobProcess_CustomMetadata();
                 Database.executeBatch(custMetadataJob,20);*/
            }
        }
    }
}
Hi please help me some poc's or code snippet if possible. I need to 1st get all the custom setting from the entire org then need to search email field type to all those custom settings ..Find those email field and then update with the new value. Some of the custom settings are having 3 email type fields as toEmail, ccEmail and bccEmail my code should find all those 3 fields across all custom settings and then update with the new email id .Please provide some code snippet if possible Mag.. Thanks for the help...
Hi All I need an Urgent help in updating the all WORKflowAlert ccEMAIL by some automation code. I have found that the bestway to do these using Tooling API. I have written the code for callout and also the Wrapper class . I am able to get all the attributes for the single id I am passing . But while updating the value of ccEmail(NB: making this blank) and trying to send it using PATCH request I am getting service error.I am new to tooling api and integration so any coding help much appreciated . If anybody help me urgently will be really appreciated . Below I am giving the callout class and Wrapper Class so then if any one can take a look and help .. Also suggest if there is any better way to do it ...Pls pls pls............ I searched everywhere in google but not getting any help .Please take a look.

CallOut
==========
    String currentSessionId = UserInfo.getSessionID();
    String domainUrl = URL.getSalesforceBaseUrl().toExternalForm();
    System.debug('!!!!!!domainUrl:!!!!!! ' + currentSessionId);
    String objectId = '01W240000004Q4AEAU';
    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer ' + currentSessionId);
    request.setHeader('Content-Type', 'application/json');
    request.setEndpoint(domainUrl + '/services/data/v47.0/tooling/query/?q=select+id,SenderType,templateId+from+WorkflowAlert+where+Id=\'' + objectId + '\'');
    request.setMethod('GET');
    Http h = new Http();
    HttpResponse response = h.send(request);
    System.debug('Getting the reponse for the currtent workflow id:::' + Json.serializePretty(response.getBody()));
    List < String > workflowEmailAlertIds = new List < String > ();
    String getResponse = response.getBody();
    System.debug('!!!!Response!!!!!' + getResponse);
    JSONParser parser = JSON.createParser(getResponse);
    //Map <String, Object> workflowAllertMap = null;
    WorkflowEmailJSONParser emailAlertDetails = null;
    String ccEmails1 = '';
    while (parser.nextToken() != null) {
        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
            String fieldName = parser.getText();
            parser.nextToken();
            String fieldValue = parser.getText();
            if (fieldName == 'Id') {
                workflowEmailAlertIds.add(fieldValue);
            }
        }
    }
    System.debug('***Size WorkflowEmailAlertIds size:***' +
        workflowEmailAlertIds.size());
    for (String workflowIDs: workflowEmailAlertIds) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + currentSessionId);
        req.setHeader('Content-Type', 'application/json');
        req.setEndPoint(domainUrl + '/services/data/v47.0/tooling/sobjects/WorkflowAlert/' + workflowIDs);
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        System.debug(res.getBody());
        String response = res.getBody();
        //response = response.replace('type', 'type_z');
        //response = response.replace('protected', 'protected_z');
        //response = response.replaceAll(',\r\n', '\r\n');
        try {
            emailAlertDetails = new WorkflowEmailJSONParser();
            emailAlertDetails = (WorkflowEmailJSONParser) System.JSON.deserialize(response, WorkflowEmailJSONParser.class);
            System.debug('******emailAlertDetails :******' + emailAlertDetails);
            if (emailAlertDetails.Metadata != null) {
                if (emailAlertDetails.Metadata.ccEmails.size() > 0) {
                    ccEmails1 = string.join(emailAlertDetails.Metadata.ccEmails, ',');
                    if (ccEmails1.contains('invalid')) {
                        ccEmails1 = 'Santanu.Roy6@cognizant.com';
                    }
                    System.debug('******All ccEmails******' + ccEmails1);
                } else {}
                if (emailAlertDetails.Metadata.recipients != null) {
                    if (emailAlertDetails.Metadata.recipients.size() > 0) {
                        string recString = '';
                        for (WorkflowEmailJSONParser.Recipients rec: emailAlertDetails.Metadata.recipients) {
                            recString = recString + rec.toString();
                        }
                        System.debug('****recipients:****' + recString);
                    } else {}
                } else {}
                if (emailAlertDetails.Metadata.senderAddress != null) {

                } else {}
                if (emailAlertDetails.Metadata.senderType != null) {

                } else {}
                if (emailAlertDetails.Metadata.template != null) {} else {}
            }
            /* For update calls*/
            HttpRequest updateReq = new HttpRequest();
            string sessionId= UserInfo.getSessionID();
            updateReq.setHeader('Authorization', 'Bearer ' + sessionId);
            updateReq.setHeader('Content-Type', 'application/json');
            updateReq.setEndPoint(domainUrl + '/services/data/v47.0/tooling/sobjects/WorkflowAlert/' + workflowIDs);            
            String reqBody = '{"MetaData":' +
                        +'{"description": "AGN Medical Information-ES",' +
                        +'"protected": "false",' +
                        +'"senderAddress": "null",' +
                        +'"senderType": "CurrentUser",' +
                        +'"template": "Workflow_Email_Templates/AGN_MIR_Submitted_ES",' +
                        +'"recipients": "[]",' +
                        +'"urls": "null",' +
                        +'"ccEmails": "[test@email.com]"},'+
                        +'"FullName": "Medical_Inquiry_vod__c.AGN_Medical_Information_ES"}';
            updateReq.setBody(reqBody);
            updateReq.setMethod('PATCH');
            Http h1 = new Http();
            HttpResponse updateRes = h1.send(updateReq);
            System.debug(updateRes.getStatusCode());
            System.debug(updateRes.getBody());


        } catch (CallOutException ex) {
           system.debug('****exception for email alertid-'+ex.getMessage());
        }
    }
    


WrapperClass
============
public class WorkflowEmailJSONParser {
    public string Id;
    public String CcEmails ;
    public String Description ;
    public String DeveloperName ;
    
    public String SenderType ;
    //public String EntityDefinitionId ;
    public Metadata Metadata ;
    public String FullName ;
    public string TemplateId;
    
    public class Metadata {
        public List<String> ccEmails ;
        public String description ;
        public Boolean protected_Z ;
        public List<Recipients> recipients ;
        public String senderAddress ;
        public String senderType ;
        public String template ;
        public Object urls ;
    }
    public class Recipients {
        public String field ;
        public Object recipient ;
        public String type_Z ;
    }
}



 
Hi All,
We have a requirement of masking all email by appending .invalid with all the custom settings emails . I have written this below code but I am unable to convert this to batch class .Can somebody help me to convert it please . I am new to batch apex hence facing the challenges .

public class Post_Copy_CustomSettings_Email {
    //TODO handle encodingType for SPARK AGN_SPARK_USER_Settings custom settings
    String emailUTFEncoding = 'UTF-8';
    public void UpdateCustomSettingsEmails(){
        Map < String, Schema.SObjectType > gd = Schema.getGlobalDescribe();        
        for ( String objectName : gd.keySet() ) {            
            Schema.SObjectType result = gd.get( objectName );            
            if ( result.getDescribe().isCustomSetting() ) {                
                String query = 'SELECT ';
                List <String> listFields = new List <String>();
                Map <String, Schema.SObjectField> objectFields = result.getDescribe().fields.getMap();
                for (String s : objectFields.keySet()) {                    
                    if (String.valueOf( objectFields.get(s).getDescribe().getType()) == 'Email' || String.valueOf(objectFields.get(s).getDescribe().getType()) == 'String'){ 
                         //&& Integer.valueOf(objectFields.get(s).getDescribe().getLength()) > 100)) {                         
                        //System.debug( 'Iterating the email fields and retrieving');
                        query += s + ',';
                        listFields.add(s);                            
                    }
                }
                if (listFields.size() > 0 ) {
                    query = query.removeEnd( ',' );
                    query += ' FROM ' + objectName;
                    List<sObject> listRecords = Database.query( query );
                    //if (listRecords.size() > 0 ) {
                    for (sObject obj : listRecords ) {
                        for (String strField : listFields ) {
                            if (obj.get(strField) != null && PostCopyValidateUtil.validateEmail(String.valueOf(obj.get(strField)))){ //Regex to compare value if email or not
                                obj.put(strField, obj.get(strField) +'.invalid');                                
                            }
                        }
                        try{
                            //update listRecords;
                        }
                        Catch(DmlException exp){
                            System.debug('DML operation has failed');
                        } 
                        
                        
                        
                        //}
                    }
                }
                
                /*BatchJobProcess_CustomMetadata custMetadataJob = new BatchJobProcess_CustomMetadata();
                 Database.executeBatch(custMetadataJob,20);*/
            }
        }
    }
}
Hi please help me some poc's or code snippet if possible. I need to 1st get all the custom setting from the entire org then need to search email field type to all those custom settings ..Find those email field and then update with the new value. Some of the custom settings are having 3 email type fields as toEmail, ccEmail and bccEmail my code should find all those 3 fields across all custom settings and then update with the new email id .Please provide some code snippet if possible Mag.. Thanks for the help...
Hi All I need an Urgent help in updating the all WORKflowAlert ccEMAIL by some automation code. I have found that the bestway to do these using Tooling API. I have written the code for callout and also the Wrapper class . I am able to get all the attributes for the single id I am passing . But while updating the value of ccEmail(NB: making this blank) and trying to send it using PATCH request I am getting service error.I am new to tooling api and integration so any coding help much appreciated . If anybody help me urgently will be really appreciated . Below I am giving the callout class and Wrapper Class so then if any one can take a look and help .. Also suggest if there is any better way to do it ...Pls pls pls............ I searched everywhere in google but not getting any help .Please take a look.

CallOut
==========
    String currentSessionId = UserInfo.getSessionID();
    String domainUrl = URL.getSalesforceBaseUrl().toExternalForm();
    System.debug('!!!!!!domainUrl:!!!!!! ' + currentSessionId);
    String objectId = '01W240000004Q4AEAU';
    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer ' + currentSessionId);
    request.setHeader('Content-Type', 'application/json');
    request.setEndpoint(domainUrl + '/services/data/v47.0/tooling/query/?q=select+id,SenderType,templateId+from+WorkflowAlert+where+Id=\'' + objectId + '\'');
    request.setMethod('GET');
    Http h = new Http();
    HttpResponse response = h.send(request);
    System.debug('Getting the reponse for the currtent workflow id:::' + Json.serializePretty(response.getBody()));
    List < String > workflowEmailAlertIds = new List < String > ();
    String getResponse = response.getBody();
    System.debug('!!!!Response!!!!!' + getResponse);
    JSONParser parser = JSON.createParser(getResponse);
    //Map <String, Object> workflowAllertMap = null;
    WorkflowEmailJSONParser emailAlertDetails = null;
    String ccEmails1 = '';
    while (parser.nextToken() != null) {
        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
            String fieldName = parser.getText();
            parser.nextToken();
            String fieldValue = parser.getText();
            if (fieldName == 'Id') {
                workflowEmailAlertIds.add(fieldValue);
            }
        }
    }
    System.debug('***Size WorkflowEmailAlertIds size:***' +
        workflowEmailAlertIds.size());
    for (String workflowIDs: workflowEmailAlertIds) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + currentSessionId);
        req.setHeader('Content-Type', 'application/json');
        req.setEndPoint(domainUrl + '/services/data/v47.0/tooling/sobjects/WorkflowAlert/' + workflowIDs);
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        System.debug(res.getBody());
        String response = res.getBody();
        //response = response.replace('type', 'type_z');
        //response = response.replace('protected', 'protected_z');
        //response = response.replaceAll(',\r\n', '\r\n');
        try {
            emailAlertDetails = new WorkflowEmailJSONParser();
            emailAlertDetails = (WorkflowEmailJSONParser) System.JSON.deserialize(response, WorkflowEmailJSONParser.class);
            System.debug('******emailAlertDetails :******' + emailAlertDetails);
            if (emailAlertDetails.Metadata != null) {
                if (emailAlertDetails.Metadata.ccEmails.size() > 0) {
                    ccEmails1 = string.join(emailAlertDetails.Metadata.ccEmails, ',');
                    if (ccEmails1.contains('invalid')) {
                        ccEmails1 = 'Santanu.Roy6@cognizant.com';
                    }
                    System.debug('******All ccEmails******' + ccEmails1);
                } else {}
                if (emailAlertDetails.Metadata.recipients != null) {
                    if (emailAlertDetails.Metadata.recipients.size() > 0) {
                        string recString = '';
                        for (WorkflowEmailJSONParser.Recipients rec: emailAlertDetails.Metadata.recipients) {
                            recString = recString + rec.toString();
                        }
                        System.debug('****recipients:****' + recString);
                    } else {}
                } else {}
                if (emailAlertDetails.Metadata.senderAddress != null) {

                } else {}
                if (emailAlertDetails.Metadata.senderType != null) {

                } else {}
                if (emailAlertDetails.Metadata.template != null) {} else {}
            }
            /* For update calls*/
            HttpRequest updateReq = new HttpRequest();
            string sessionId= UserInfo.getSessionID();
            updateReq.setHeader('Authorization', 'Bearer ' + sessionId);
            updateReq.setHeader('Content-Type', 'application/json');
            updateReq.setEndPoint(domainUrl + '/services/data/v47.0/tooling/sobjects/WorkflowAlert/' + workflowIDs);            
            String reqBody = '{"MetaData":' +
                        +'{"description": "AGN Medical Information-ES",' +
                        +'"protected": "false",' +
                        +'"senderAddress": "null",' +
                        +'"senderType": "CurrentUser",' +
                        +'"template": "Workflow_Email_Templates/AGN_MIR_Submitted_ES",' +
                        +'"recipients": "[]",' +
                        +'"urls": "null",' +
                        +'"ccEmails": "[test@email.com]"},'+
                        +'"FullName": "Medical_Inquiry_vod__c.AGN_Medical_Information_ES"}';
            updateReq.setBody(reqBody);
            updateReq.setMethod('PATCH');
            Http h1 = new Http();
            HttpResponse updateRes = h1.send(updateReq);
            System.debug(updateRes.getStatusCode());
            System.debug(updateRes.getBody());


        } catch (CallOutException ex) {
           system.debug('****exception for email alertid-'+ex.getMessage());
        }
    }
    


WrapperClass
============
public class WorkflowEmailJSONParser {
    public string Id;
    public String CcEmails ;
    public String Description ;
    public String DeveloperName ;
    
    public String SenderType ;
    //public String EntityDefinitionId ;
    public Metadata Metadata ;
    public String FullName ;
    public string TemplateId;
    
    public class Metadata {
        public List<String> ccEmails ;
        public String description ;
        public Boolean protected_Z ;
        public List<Recipients> recipients ;
        public String senderAddress ;
        public String senderType ;
        public String template ;
        public Object urls ;
    }
    public class Recipients {
        public String field ;
        public Object recipient ;
        public String type_Z ;
    }
}



 
Hi,
We have one Custom settigns as Tem2deafultFields and it has four fields. After clicking the Manage button, we have three sets as R1, R2 and R5. Each set has some fields in it.

Now i want to get the Tem2deafultFields  custom setting and loop through R1, R2 and R3 to see which one has matching recordtype id as teh opty record type id  and get the fields from that set and display them on VF page.

User-added image

even if anybody can just help in looping of the sets will be helpfull.

Thanks.
  • March 15, 2018
  • Like
  • 0