node.js + ALM + REST API = Awesome

Everyday you see people (or yourself) doing some mundane task and you wonder how can you help them automate that process. So this article highlights something I did a few months back to help a friend who used to spend considerable amount of time fetching data from ALM (Earlier know as QC) formatting it and putting in some other database.

I always wanted to use node.js in something useful, so I decided to set up a server to get data from ALM server and push it to other database. Until this point I had no idea if ALM had exposed any API’s. Luckily they had opened up access to ALM 11.00 and higher version via their REST API. I wanted everything on server side and there was no UI involved.

So I was able to come up with a small script which authenticates the user into ALM and then get all the defect related to a release. I have provided the comments where ever necessary in the below code. I am still a noob in node.js so please excuse me if I have made any mistake


var https = require('https'),
	fs = require('fs'),
	config = JSON.parse(fs.readFileSync('config.json'));//this refers to a file where I have all my config like host, userName, password Etc

//this is added to avoid the TLS error. Uncomment if you get a TLS error while authenticating.
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';

//set the correct options for the call.
var options = {
	host : config.host, 
	path : "/qcbin/authentication-point/authenticate",
	method: "GET",
	headers : {'Authorization': 'Basic '+new Buffer(config.alm_userName + ':' + config.alm_password).toString('base64')}
	};
	//authenticating the user into ALM
	ALMConnect(options, 'header','', function(status, data){
		if(status){
			//get the LWSSO_Cookie from the header. This is the session cookie which will be used in all callouts to ALM.
			if(data.headers["set-cookie"] != undefined ) {
				extractDefects(data.headers["set-cookie"]);
			}else{
				console.log('Dagnabbit!! ERROR:  Unable to login, check your username/password/serverURL.');
			}
		}else{
			console.log('Dagnabbit!! ERROR:  ' + JSON.stringify(data));
		}
	});

//Function to extract the defects for analysis.
function extractDefects(LWSSO_Cookie){
	var queryParam = "{";
	//add Release
	queryParam += "detected-in-rel["+config.release+"];";
	//add all your request parameters here. Its a little complicated initially, but you will get a hang of it. 
	// Make sure to use encodeURIComponents() for all the values in the query parameters.
	queryParam+="}";
	//get all the fields that you want to query. Lesser the fields smaller the XML returned, faster is the call.
	var fields = config.defectFieldMapping.fieldArray.join(',');
	var opt = {
		host: config.host,
		path: "/qcbin/rest/domains/"+config.domain+"/projects/"+config.project+"/defects?query="+queryParam+"&fields="+fields+"&page-size=max",
		method:"GET",
		headers: {"Cookie":LWSSO_Cookie}
	};

	ALMConnect(opt, 'data','',function(status,data){
		if(status){
                        //write the defects to an XML file in local drive.
			fs.writeFileSync('newDefect.xml',data);
			//once you get the defectXML you can parse it into JSON and push it other databases like SFDC etc..		
		}else{
			console.log('Dagnabbit!! ERROR:  ' + JSON.stringify(data));
		}
	});
}

function ALMConnect(opt, responseType,requestBody, callback){

	var request = https.request(opt, function(res){
		res.setEncoding('utf8');
		var XMLoutput='';
		res.on('data',function(chunk){
			XMLoutput+=chunk;
		});
		res.on('end',function(){
			if(responseType=='data'){
				callback(true,XMLoutput);
			}else {
				callback(true, res);
			}
		});
	});
	request.on('error',function(e){
		callback(false,e);
	});
	if(opt.method=='POST' || opt.method == 'PUT'){
		request.write(requestBody);
	}
	request.end();
}


Showing a VF in jQuery modal dialog on standard page button click

Showing a VF page as a popup on click of a custom button is fairly easy. But the problem arises when we have to refresh the parent when the user has completed the operation on the VF pop-up page because of the same-origin policy. The browser doesn’t allow any communication across domain/ protocol. But as a workaround we can use the postMessage() to communicate between parent and pop-up window even if they are on different domain, but again IE8 doesn’t support this.

jQuery Modal Dialog to the rescue. In this demo we will place a custom button on the account detail page, clicking on which will pop-up a modal-vf page which will allow the user to update the name of the account.

I will be using 4 components to demo this :

1) A javascript file, stored in Static resource
2) An images zip file in static Resource. Click here to download the images zip file and store it in the static resource by the name images
3) A Custom button
4) An apex class (extension)
5) A VF page (pop-up)

Lets start with the javascript file. Store this in the static resource by the name mainjs

	/*@description: This script will insert a modal div in the standard page.
					the modal will contain a VF page. This file should be located in static resource.*/
	var j$ = jQuery.noConflict();
	var currentUrl = window.location.href;
	var hostIndex = currentUrl.indexOf(window.location.host+'/')+(window.location.host+'/').length;
	var accountId = currentUrl.substring(hostIndex,hostIndex+15); 
	j$(function(){
		/*Insert the jQuery style sheets in the Head.*/
		/*Insert the Modal dialog along with the VF as an iframe inside the div.*/
		j$("head").after(
			j$("<link>",{rel:"stylesheet",
						href:"https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"}));
		j$("body").after(
			j$("<div>",{id:"modalDiv",
					    style:"display:none;"
			   }).append(
				j$("<iframe>",{id:"vfFrame",
							 src:"/apex/popuppage?id="+accountId,
							 height:200,
							 width:550,
							 frameBorder:0})
			   ));
		/*Initialize the Dialog window.*/
		j$("#modalDiv").dialog({
			autoOpen: false,
			height: 210,
			width: 605,
			modal:true
		});
	});

Apex Class :

public class PopUpExtension{
    public Account account{get;set;}

    public PopUpExtension(ApexPages.StandardController stdController){
        this.account = (Account)stdCOntroller.getRecord();
    }
    
    public void saveAccount() {
        try{
            update Account;
        }catch(exception e) {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Occured while updating the account '+e.getMessage());
            ApexPages.addMessage(myMsg);
        }
    }
}

VF page, name it as popuppage:

<apex:page showHeader="false" sidebar="false" standardController="Account" extensions="PopUpExtension">
    <apex:form>
        <apex:pageBlock title="{!Account.Name}" id="pageBlock">
            Update the Account Name:<br/>
            <b>Account Name</b><apex:inputField value="{!Account.Name}"/>
            <apex:commandButton action="{!saveAccount}" value="Save" onComplete="closeAndRefresh();" status="actionStatus" reRender="pageBlock"/>
            <apex:actionStatus id="actionStatus">
                <apex:facet name="start"> 
                    <img src="/img/loading.gif"/>
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlock>
    </apex:form>
    <script>
        function closeAndRefresh(){
            console.log('clicked on the button');
            window.top.location = '/{!$CurrentPage.parameters.id}';
        }
    </script>
</apex:page>

Finally create a button on the Account and call it jQuery modal. Select
‘Detail Page Button’ as the Display Type ,
‘Execute Javascript’ as the Behavior and
‘onClick JavaScript’ as content source.

Add the below code in the button:

{!REQUIRESCRIPT("https://code.jquery.com/jquery-1.10.2.js")} 
{!REQUIRESCRIPT("https://code.jquery.com/ui/1.10.4/jquery-ui.js")} 
{!REQUIRESCRIPT("/resource/mainjs")}

j$("#modalDiv").dialog('option','title','{!Account.Name}').dialog('open');

For more info on the Dialog check out the doc here

Hope this helps!

Generate a PDF and Attach it to record from a trigger in Salesforce — RESTful Webservice

Attaching the PDF using RESTful services.

I have posted an article on the step-by-step instruction on doing the same the SOAP way.

Here you go:
Step 1: create a REST resource class and in the post method add the logic to generate the PDF
Step 2: Create a class which has future Method (with callout=true). This future method will call the REST Resource
Step 3: Finally create a trigger that will call this future method.

In this scenario I will create a account and a pdf will be attached to it after the insert.

First lets create the VF page to generate the pdf, a simple one. Lets call it, pdfRenderPage


<apex:page standardController="Account" renderAs="pdf">

Hey, the Account name is {!account.Name}

</apex:page>

Create the Rest Resource which will insert the pdf attachement.

/********
Copyright (c) <2014> <junglee Force(jungleeforce@gmail.com)>
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**********/
@RestResource(urlMapping='/addPDFtoRecord/*')
global with sharing class AddPDFtoRecordREST{
  
  @HttpPost
    global static void doPost(list<String> accountIdList) {
       list<attachment> insertAttachment = new list<attachment>();
        for(String accId: accountIdList){
            //create a pageReference instance of the VF page.
            pageReference pdf = Page.pdfRenderPage;
            //pass the Account Id parameter to the class.
            pdf.getParameters().put('id',accId);
            Attachment attach = new Attachment();
            Blob body;
            if(!test.isRunningTest()){
                body = pdf.getContent();
            }else{
                body=blob.valueOf('TestString');
            }
            attach.Body = body;
            attach.Name = 'pdfName'+accId+'.pdf';
            attach.IsPrivate = false;
            attach.ParentId = accId;//This is the record to which the pdf will be attached
            insertAttachment.add(attach);
         }
         //insert the list
         insert insertAttachment;
    }
}

Next create a class with the future method:

global class AccountTriggerController{

    @Future(callout=true)
    public static void addPDFAttach(string sessionId, list<id> accountIdList){
       HttpRequest req = new HttpRequest();
       req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/addPDFtoRecord/');
       req.setMethod('POST');
       req.setBody('{"accountIdList":'+JSON.serialize(accountIdList)+'}');
       req.setHeader('Authorization', 'Bearer '+ sessionId);
       req.setHeader('Content-Type', 'application/json');
       Http http = new Http();
       if(!test.isRunningTest()){
           HTTPResponse res = http.send(req);
       }
    }
}

FInally create the trigger:

trigger accountAfterInsert on Account (after insert) {

     list<id> accId = new list<id>();
     for(id acc: trigger.newMap.keySet()){
         accId.add(acc);
      }
		//You would need to send the session id to the future method as the you cannot use the userInfo.getSessionID() method in the future method because it is asynchronous and doesn't run in the user context.
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), accId);

}

All of this would work only if you add the host in my case it’s https://ap1.salesforce.com, add it to the remote site setting. So go to Setup-> Admin Setup -> security Controls -> Remote Site Settings. Click on new, give it a name(RESTTest), add the Remote Site URL(just copy paste the url from the address bar), it will only take the host and truncate the rest when you save it.

Test Class:

@isTest
public class AddPDFtoRecordTest{
    @isTest static void addPDFtoRecordTestMethod() { 
        account a = new account();
        a.name= 'test PDF';
        insert a;
        
        List<String> accountIdList = new List<String>();
        accountIdList.add(a.Id);
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
    
        req.requestURI = 'https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/addPDFtoRecord/';  
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueOf('{"accountIdList":'+JSON.serialize(accountIdList)+'}');
        req.addHeader('Content-Type', 'application/json');
        RestContext.request = req;
        RestContext.response = res;
    
        AddPDFtoRecordREST.doPost(accountIdList);              
    }
}

You can find the code on gitHub

Hiding selected Side bar custom components(HTML)

There are times in the project where we have to have some UI changes done on the Standard pages. For example, you may have to change the label of the ‘open Activities’ related list on a particular object’s standard page. To achieve this functionality we usually write a simple javascript / jquery script in the side bar component (of type HTML) and then that will do the work. So there are scenarios where multiple team might have implemented such functionality and this results in a lot of sidebar components showing up on the UI which will not make sense to the End Users

In the below code one have to just enter the name of the such side bar components and then that side bar component will be automatically hidden.

//Referencing the jQuery library from the static resource
<script src="/resource/jQuerylib"></script>
<script>
	
var j$ = jQuery.noConflict();
//Add All the name of the components that you want to hide.
var hideComponentArray = ['component1','component2','HideSideBarComponents'];
j$(function(){
   j$("h2.brandPrimaryFgr").each(function(){
      if(j$.inArray(j$(this).text(), hideComponentArray) != -1){
         //Hiding the entire div.
         j$(this).parent("div.sidebarModuleHeader").parent("div.htmlAreaComponentModule").hide();
      }
    });
});
</script>

Storing Javascript object in local storage

I was working on a chrome extension where I wanted to store javascript objects in localstorage and retrieve it later. But I was unable to do so with just setting the javascript objects in localstorage. I had to first stringify it and then parse it.


//create a javascript object array.
 var team = [{lead:'Monica',projectManager:'Ross',intern:'Chandler'},
	     {lead:'Joey',projectManager:'Rachel',intern:'Phoebe'}];

//Store it in the localstorage and name it localStr.
localStorage.setItem('localStr',JSON.stringify(team));

//Retrieve the data and parse it back to JSON.
var retrieveData = JSON.parse(localStorage.getItem('localStr'));

//Write the data to the console.
console.log('and the lead of team 1 is '+ retrieveData[0].lead);

Uploaded another extension on Chrome : Salesforce code Coverage extractor

I have taken this extension out now.. Need to implement some changes. . Will be back up soon

This extension will help admin / developers to extract the overall code coverage from the developer console. All they have to do is hover over the Overall code coverage section and then click on the [Extract Data] link and then you have the csv downloaded. 

You can also extract SOQL and SOSL results in the same manner from the developer console.

You can get the extension here:

https://chrome.google.com/webstore/detail/salesforce-code-coverage/lmndofengngdnomiikmhmhfddhgahnio

Uploaded my first salesforce extension on Chrome store

I have finally managed to upload my chrome extension which will help developers to search a string in their code.

You can download it from here:

https://chrome.google.com/webstore/detail/salesforce-advanced-code/lnkgcmpjkkkeffambkllliefdpjdklmi

A similar extension is also available on Mozilla as well. You can download it from here.

https://addons.mozilla.org/en-US/firefox/addon/sforcecodesearch/

Extracting list of all the objects (Standard + Custom) in Salesforce

Adding another category in blog today : “Just for kicks!!”, which is the reason for this post.

Today as I was whiling away hours, a thought struck my mind ‘Why not list out all the objects(which is quite simple) and their related objects(this was challenging) as well in a VF page’?

The reason listing the child object for each object was challenging is because salesforce(governer limits) only allows 100 childrelationship describes per apex invocation, as we already have more 100 objects in our org, doing child describes for each objects will result in more than 100 child relationship describe per invocation. As a workaround: I added a apex:actionFunction which will be fired every 2.5 seconds triggering a method in the class, so each time it is triggered it is considered as one invocation.

allObjectList

So here’s the code (save the class 1st and then the Page):

Apex Class:

/********
Copyright (c) <2013> <junglee Force(jungleeforce@gmail.com)>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**********/

public class DisplayObjectDetail{
    
    public list<objectClass> standardObjList{get;set;}
    public list<objectClass> customObjList{get;set;}   
    public FINAL integer numberOfObject;
    public set<string> objectSet{get;set;}
    //This is a variable to check if we're done with the Iteration.
    public string isIterationDone{get;set;}
    
    //Constructor.
    public displayObjectDetail(){
        
        //initializing the lists.

       customObjList  = new list<objectClass> ();
       standardObjList = new list<objectClass> ();
       objectSet= new set<string>();
       list<schema.sObjectType> allObjects = Schema.getGlobalDescribe().Values();
        //get number of objects available for iteration.
       numberOfObject = allObjects.size();
       isIterationDone = 'No';
       
   }
   
    public void ObjectList(){
        // check if we have all the objects added in the list
        if((customObjList.size() + standardObjList.size())<numberOfObject){
            integer i=0;
            for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
                //We will iterate over this list only 99 times as there is governer limit of 100 childrelationship describes per invocations.
                if(i<99){
                    //adding the object name to the set, so that we do not add duplicates to the list.
                    if(!objectSet.contains(objTyp.getDescribe().getName())){
                        i++;
                        objectSet.add(objTyp.getDescribe().getName());
                        //check if its a custom or standard object.
                        if(objTyp.getDescribe().isCustom()){
                            customObjList.add(new objectClass(objTyp.getDescribe().getLabel(), objTyp.getDescribe().getName(), objTyp.getDescribe().getKeyPrefix(), objTyp.getDescribe().getChildRelationships(), objTyp.getDescribe().isCustomSetting(), objTyp.getDescribe().getRecordTypeInfos()));
                        }else{
                            standardObjList.add(new objectClass(objTyp.getDescribe().getLabel(), objTyp.getDescribe().getName(), objTyp.getDescribe().getKeyPrefix(), objTyp.getDescribe().getChildRelationships(), objTyp.getDescribe().isCustomSetting(), objTyp.getDescribe().getRecordTypeInfos()));
                        }
                    }
                }else{
                    break;
                }
            }
        }else{
            isIterationDone = 'Yes';
        }
    }
   
   //This is a wrapper class. 
   public class objectClass{
        public string label{get;set;}
        public string apiName{get;set;}
        public string keyPrefix{get;set;}
        public boolean isCustomSet{get;set;}
        public list<Schema.ChildRelationship> childObjects;
        public list<string> childObjectArray{get;set;}
        public list<schema.RecordTypeInfo> recordTypes;
        public list<string> recordTypeArray{get;set;}
        public objectClass(string lab, string api, string prefix, list<Schema.ChildRelationship> childObj, boolean isCustomSetting, list<schema.RecordTypeInfo> recordTypeList){
            this.label = lab;
            this.apiName = api;
            this.KeyPrefix = prefix;
            this.isCustomSet = isCustomSetting;
            this.childObjectArray = new list<string>();
            this.recordTypeArray = new list<string>();
            if(childObj!=null){
                for(schema.childRelationShip ch : childObj){
                    this.childObjectArray.add(ch.getChildSObject().getDescribe().getLabel()+' ('+ch.getChildSObject().getDescribe().getName()+')');
                }
            }
            if(recordTypeList!= null){
                for(schema.RecordTypeInfo rt: recordTypeList){
                    this.recordTypeArray.add(rt.getName());
                }
            }
        }
    }
}

VF page:

<!--
Copyright (c) <2013> <junglee Force(jungleeforce@gmail.com)>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<apex:page controller="DisplayObjectDetail" readOnly="true">
    <script type="text/javascript" src="/resource/jQuery"></script>
    <script type="text/javascript">
    
        var j$ = jQuery.noConflict();
        var timeInter =setInterval(function(){iterateObjectMethod()},2500);

        function iterateObjectMethod(){
            getObjectList();
            var iterationDone = j$("#isIterationDoneDiv").text();
            if(iterationDone  == 'Yes'){
                j$("#isIterationDoneDiv").hide();
                clearInterval(timeInter);
            }
        }
    </script>   

    <apex:actionStatus id="myStatus" startText="Fetching the Data..." stopText=""/><br/><br/>
    <apex:form >
        <apex:actionFunction action="{!ObjectList}" status="myStatus" name="getObjectList" reRender="thePageBlock"/>
    </apex:form>
    <h1>Here's the List of Standard and Custom Objects:</h1>
    <apex:pageBlock id="thePageBlock">
        <b>Standard Objects:</b>
        <apex:pageBlockTable value="{!standardObjList}" var="std">
            <apex:column headerValue="Name">{!std.label}</apex:column>
            <apex:column headerValue="API Name">{!std.apiName}</apex:column>
            <apex:column headerValue="Key Prefix">{!std.keyPrefix}</apex:column>
            <apex:column headerValue="ChildObjects">
                <apex:repeat value="{!std.childObjectArray}" var="ch">
                    {!ch}<br/>
                </apex:repeat>
            </apex:column>
            <apex:column headerValue="Record Types">
                <apex:repeat value="{!std.recordTypeArray}" var="rt">
                    {!rt}<br/>
                </apex:repeat>
            </apex:column>
        </apex:pageBlockTable>
        <br/><br/><br/>
        <b>Custom Objects:</b>
        <apex:pageBlockTable value="{!customObjList}" var="cust">
            <apex:column headerValue="Name">{!cust.label}</apex:column>
            <apex:column headerValue="API Name">{!cust.apiName}</apex:column>
            <apex:column headerValue="Key Prefix">{!cust.keyPrefix}</apex:column>
            <apex:column headerValue="Custom Setting">{!cust.isCustomSet}</apex:column>
            <apex:column headerValue="ChildObjects">
                <apex:repeat value="{!cust.childObjectArray}" var="ch">
                    {!ch}<br/>
                </apex:repeat>
            </apex:column>
            <apex:column headerValue="Record Types">
                <apex:repeat value="{!cust.recordTypeArray}" var="rt">
                    {!rt}<br/>
                </apex:repeat>
            </apex:column>
        </apex:pageBlockTable>
        <div id="isIterationDoneDiv">{!isIterationDone}</div>
     </apex:pageBlock>
</apex:page>

If you think there’s a better way of doing this, please do share.

In the coming days I will post about doing the same thing The pure javascript way.

Passing sObjects to future methods in Salesforce

So how to pass sObject to future methods, even when we know that the future methods do not accept the sobjects. So how do we do it?
We will use the JSON methods to first serialize the list of account which will be serialized as string. This string is passed to the future method and then in the future method we will de-serialize the string back to list of account and insert the accounts.

Here’s the code;

This is the main class, we will call it GenerateJson

	public class GenerateJson{
    //Constructor
    public GenerateJson(){
        // I have hardcoded the name and the account number in Kannada!! 
        account acc1 = new account(name = 'ಬೆಂಗಳೂರು', AccountNumber= '೧೦೨೯೩೭');
        account acc2 = new account(name ='ಹುಬ್ಬಳ್ಳಿ', AccountNumber= '೬೮೬೮೮೮೦');
        
        //Initialize a list of account
        list<account> accList = new list<account>();
        accList.add(acc1);
        accList.add(acc2);
        
        //Using the JSON.serializePretty we will serialize the list of account.
        string jsonString = JSON.serializePretty(accList);

        //Pass the serialized list to the future method.
        DeserializeJsonRecords.insertAccount(jsonString);
    }
}       

We will create another class with a future method. We will name the class DeserializeJsonRecords

public class DeserializeJsonRecords{
    
    @future
    public static void insertAccount(string accountString){
        //Deserialize the list and cast it to account list.
        list<account> insertAccList =  (list<account>)JSON.deserializeStrict(accountString, list<account>.class);
		//Insert the accounts
        insert insertAccList;
    }
}

To check if the code works just run the below line in the developer console

GenerateJson genJson = new GenerateJson();

One of the catch here is if the size of the list is very huge then there are chances that the heap size limit will be reached. Also I am not sure about how the the rich text area fields are deserialized, will need to test that.

Hope it helps!

Generate a PDF and Attach it to record from a trigger in Salesforce — SOAP webservice

The reason you stumbled upon this post is because you tried using the .getContentAsPdf() method in a trigger and you got an error message stating you cannot use it in a trigger. So you tried to search and figure out as to how you could do it.

I had faced a similar issue when a task was given to me attach a pdf(which would be automatically created) when a record is created. Inititally I thought its quite simple and accepted it. But when I actually started coding, I became aware of the issues with this requirement.

Well, I googled for a while and came across a post (unfortunately, I could not find it now) which had the solution to this problem, but the guy had not posted the code to demonstrate. So I thought I could share the code in a post as this could help someone.

Here you go:
Step 1: create a class and include a webservice method that will generate the pdf and attach it to a record
Step 2: Generate the WSDL from this class.
Step 3: Generate a class from the WSDL you just created. (step 2 and 3 are a little wierd, but believe me it works)
Step 4: Create a class which has future Method (with callout=true). This future method will call the stub which you just created from the WSDL.
Step 5: Finally create a trigger that will call this future method.

If you find any better way of doing this please do let me know.

In this scenario I will create a account and a pdf will be attached to it after the insert.

First lets create the VF page to generate the pdf, a simple one. Lets call it, pdfRenderPage


<apex:page standardController="Account" renderAs="pdf">

Hey, the Account name is {!account.Name}

</apex:page>

The webservice class, After you save this class, you can see a generate WSDL button. click on it and save the WSDL file

global class AddPdfToRecord{

    webservice static void addPDF(list<id> accountIds){
		//Instantiate a list of attachment object
        list<attachment> insertAttachment = new list<attachment>();
        for(Id accId: accountIds){
			//create a pageReference instance of the VF page.
            pageReference pdf = Page.pdfRenderPage;
			//pass the Account Id parameter to the class.
            pdf.getParameters().put('id',accId);
            Attachment attach = new Attachment();
            Blob body;
            if(!test.isRunningTest()){
                body = pdf.getContent();
            }else{
                body=blob.valueOf('TestString');
            }
            attach.Body = body;
            attach.Name = 'pdfName'+accId+'.pdf';
            attach.IsPrivate = false;
            attach.ParentId = accId;//This is the record to which the pdf will be attached
            insertAttachment.add(attach);
         }
		 //insert the list
         insert insertAttachment;
    }
}

Now, consume the WSDL that you just generate. Go to Setup->App Setup-> Develop-> Apex Classes and click on Generate from WSDL. Load the WSDL file and name the class as ‘addPDFStub’.

//Generated by wsdl2apex

public class addPDFStub {
    public class LogInfo {
        public String category;
        public String level;
        private String[] category_type_info = new String[]{'category','http://soap.sforce.com/schemas/class/AddPdfToRecord','LogCategory','1','1','false'};
        private String[] level_type_info = new String[]{'level','http://soap.sforce.com/schemas/class/AddPdfToRecord','LogCategoryLevel','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'category','level'};
    }
    public class AllowFieldTruncationHeader_element {
        public Boolean allowFieldTruncation;
        private String[] allowFieldTruncation_type_info = new String[]{'allowFieldTruncation','http://www.w3.org/2001/XMLSchema','boolean','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'allowFieldTruncation'};
    }
    public class DebuggingHeader_element {
        public addPDFStub.LogInfo[] categories;
        public String debugLevel;
        private String[] categories_type_info = new String[]{'categories','http://soap.sforce.com/schemas/class/AddPdfToRecord','LogInfo','0','-1','false'};
        private String[] debugLevel_type_info = new String[]{'debugLevel','http://soap.sforce.com/schemas/class/AddPdfToRecord','LogType','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    }
    public class CallOptions_element {
        public String client;
        private String[] client_type_info = new String[]{'client','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'client'};
    }
    public class SessionHeader_element {
        public String sessionId;
        private String[] sessionId_type_info = new String[]{'sessionId','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    }
    public class addPDFResponse_element {
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{};
    }
    public class DebuggingInfo_element {
        public String debugLog;
        private String[] debugLog_type_info = new String[]{'debugLog','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'debugLog'};
    }
    public class addPDF_element {
        public String[] accountIds;
        private String[] accountIds_type_info = new String[]{'accountIds','http://soap.sforce.com/schemas/class/AddPdfToRecord','ID','0','-1','true'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord','true','false'};
        private String[] field_order_type_info = new String[]{'accountIds'};
    }
    public class AddPdfToRecord {
        public String endpoint_x = 'https://'+URL.getSalesforceBaseUrl().getHost()+'/services/Soap/class/AddPdfToRecord';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        public addPDFStub.CallOptions_element CallOptions;
        public addPDFStub.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
        public addPDFStub.SessionHeader_element SessionHeader;
        public addPDFStub.DebuggingHeader_element DebuggingHeader;
        public addPDFStub.DebuggingInfo_element DebuggingInfo;
        private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/schemas/class/AddPdfToRecord';
        private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=http://soap.sforce.com/schemas/class/AddPdfToRecord';
        private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/schemas/class/AddPdfToRecord';
        private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/schemas/class/AddPdfToRecord';
        private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/schemas/class/AddPdfToRecord';
        private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/schemas/class/AddPdfToRecord', 'addPDFStub'};
        public void addPDF(String[] accountIds) {
            addPDFStub.addPDF_element request_x = new addPDFStub.addPDF_element();
            addPDFStub.addPDFResponse_element response_x;
            request_x.accountIds = accountIds;
            Map<String, addPDFStub.addPDFResponse_element> response_map_x = new Map<String, addPDFStub.addPDFResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://soap.sforce.com/schemas/class/AddPdfToRecord',
              'addPDF',
              'http://soap.sforce.com/schemas/class/AddPdfToRecord',
              'addPDFResponse',
              'addPDFStub.addPDFResponse_element'}
            );
            response_x = response_map_x.get('response_x');
        }
    }
}

Next create a class with the future method:

global class AccountTriggerController{

    @Future(callout=true)
    public static void addPDFAttach(string sessionId, list<id> accountId){
        addPDFStub.SessionHeader_element sessionIdElement= new addPDFStub.SessionHeader_element();
        sessionIdElement.sessionId = sessionId;

        addPDFStub.AddPdfToRecord stub= new addPDFStub.AddPdfToRecord();
        stub.SessionHeader= sessionIdElement;
        stub.addPDF(accountId);
    }
}

FInally create the trigger:

trigger accountAfterInsert on Account (after insert) {

     list<id> accId = new list<id>();
     for(id acc: trigger.newMap.keySet()){
         accId.add(acc);
      }
		//You would need to send the session id to the future method as the you cannot use the userInfo.getSessionID() method in the future method because it is asynchronous and doesn't run in the user context.
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), accId);

}

All of this would work only if you add the host in my case it’s https://ap1.salesforce.com, add it to the remote site setting. So go to Setup-> Admin Setup -> security Controls -> Remote Site Settings. Click on new, give it a name(webserviceTest), add the Remote Site URL(just copy paste the url from the address bar), it will only take the host and truncate the rest when you save it.

Hope it helps!!

UPDATE

I have updated the post with the test Classes. Now all the classes have more than 90% coverage!
ALso I have made some changes in the AddPdfToRecord class to accommodate the Test class changes
Below test classes needs to be added to get the coverage

First add the web service mock implementation and then the actual test class.

@isTest
global class WebServiceMockImpl implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
           String[] accountIdArray = new String[]{};
       addPDFStub.addPDFResponse_element respElement = new addPDFStub.addPDFResponse_element();
       addPDFStub.AllowFieldTruncationHeader_element class1= new addPDFStub.AllowFieldTruncationHeader_element();
       addPDFStub.DebuggingHeader_element class2= new addPDFStub.DebuggingHeader_element();
       addPDFStub.CallOptions_element class3= new addPDFStub.CallOptions_element();
       addPDFStub.DebuggingInfo_element  class4= new addPDFStub.DebuggingInfo_element ();
       addPDFStub.SessionHeader_element class5= new addPDFStub.SessionHeader_element();
       response.put('response_x', respElement ); 
   }
}
@isTest
public class AddPDFtoRecordTest{
    @isTest static void addPDFtoRecordTestMethod() { 
        Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        account a = new account();
        a.name= 'test PDF';
        insert a;
        
        AddPdfToRecord.addPDF(new list<Id>{a.Id});    
    
    }
}

UPDATE
YOu can achieve the same by using the APEX Rest Resource. Check out the post here

You can find the code on gitHub