Tuesday, June 9, 2020

Salesforce Package Upload Automation

Uploading a package is big and tedious process if you have several components, even opening package upload page may take more than 10-15 minutes.

What if requirement is to build the org and get package daily. Which can take more than 4 hours to get the package out.

I have a solution to above problem 😃

PackageUploadRequest is the key for us to get success on this.

When user clicks on upload button on package creation page, PackageUploadRequest record gets created in background.

Salesforce has given access to this object using Tooling API, we can insert record to it, which will upload the package.

Fields 
  1. Description - Description of this version, what it contains
  2. Errors - will store errors, if upload failed 
  3. IsReleaseVersion - beta or released
  4. MajorVersion - like 2002 (i.e. 2020 2nd Package, in Salesforce terms Summer 20, Spring 20 etc.) 
  5. MetadataPackageId - Package id 
  6. MetadataPackageVersionId - Version Id, this id will be used in package url, to install it 
  7. MinorVersion - 2002.1 (First version for Summer 20)
  8. Password- Password if needed for installing this version
  9. PostInstallUrl - URL of the post-installation instructions
  10. ReleaseNotesUrl - URL of the package release notes
  11. Status -  Success or Failure, after upload
  12. VersionName - Name of version (Summer 20, Spring 20 etc)
CreatedDate (Field Exists but not mentioned in the document, Raised the doc bug for it)


Different Scenarios :- 

Upload new package with version name and number


packageUploadRequestObj.setMetadataPackageId(packageId);
packageUploadRequestObj.setVersionName(packageVersionDescription);
packageUploadRequestObj.setDescription(packageDescription);
packageUploadRequestObj.setMajorVersion(packageMajorVersionNumber);
packageUploadRequestObj.setMinorVersion(packageMinorVersionNumber);
packageUploadRequestObj.setPostInstallUrl(packagePostInstallUrl);
packageUploadRequestObj.setReleaseNotesUrl(packageReleaseNotesUrl);
packageUploadRequestObj.setIsReleaseVersion(isReleaseVersion);
packageUploadRequestObj.setPassword(packagePassword);

Upload Subsequent number package i.e. 2002.2 when 1 was previously uploaded.

Query Previous Successful Upload (MetadataPackageVersion) 

While uploading release package -

connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion where ReleaseState = 'Released' AND PatchVersion=0 order by MajorVersion desc,MinorVersion desc  limit 1 ");


While uploading beta package -

connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion  order by MajorVersion desc,MinorVersion desc  limit 1 ");

Add 1 to minor version and use the same while inserting record in PackageUploadRequest. 

So minor version would be 2 and so on. 

This way we don't need to mention number and name everyday while uploading, it will automatically take the name and number from previous record  and insert the record

Upload Patch Package 

Here we just copy the same query with a minor modification PatchVersion>0 

connection.query("SELECT Id,Name,MajorVersion,PatchVersion,MinorVersion FROM MetaDataPackageVersion where ReleaseState = 'Released' AND PatchVersion>0 order by MajorVersion desc,MinorVersion desc  limit 1 ");

Copy all fields as it is and insert the record in patch org.


Hope this helps, please fill free to comment in case of questions.

Friday, April 10, 2020

Open Lightning Component in New Tab in Lightning Console

Let's Start with List View Button:-

If  component needs to be opened from list view, then it has to be custom list view button, on click on this button component will open in new tab as shown in below diagram.




Suppose my component name is Create Case with Task, create new lightning tab for it.




Make sure to implement  "force:appHostable,lightning:isUrlAddressable, 
flexipage:availableForAllPageTypes, force:lightningQuickAction" in component, so that it will be visible while creating tabs and quickaction.

Create List view button on object as shown below, 



Suppose my lightning tab name is test1, then my tab url would be /lightning/n/namespaceprefix__test1(tab name).

You are all set to add this button in object list view.

Note :- There is still a know issue on this, Custom List button in lightning console leaves blank workspace tab when navigating away from the List View
https://success.salesforce.com/issues_view?id=a1p3A0000003eyZQAQ


But if user don't select check box "Display Checkboxes (for Multi-Record Selection)" while creating list view button, this issue will not appear. 






Wednesday, March 25, 2020

Run - "Time Based Workflow" or "Process Builder" within 60 Minutes or 1 Hour of Record Creation

We mostly get a requirement, to update the field after few minutes of creation of record or send an email after 1 minute of status update of record. 

Salesforce gives only 2 options while creating time based workflow i.e. hours or days. Means if we need that action should be executed within 5 or 10 minutes, it is not possible.




To serve above purpose, we can create a date time formula field "Created Date - 50 Minutes", value = CreatedDate-(50/1440). 

Suppose record was created on 26 March 2020 9:00 AM (03/26/2020 09:00), this formula field will have value 26 March 2020 8:10 AM (03/26/2020 08:10).





Now you can use this field in workflow like below :-





Activate workflow, which will work after 10 minutes from record creation. You can use the same field in process builder as well.

Note : - Time based workflow runs in batch as per salesforce queue, record will be queued after 10 minutes, execution may vary as per salesforce queue execution.