Friday 30 October 2015

How to approve or reject the record without login to salesforce?

Go to Setup --> Build --> Workflow & Approvals --> Process Automation Settings --> Check "Enable Email Approval Response" option --> Save.



This option allows, user can approve/reject record without salesforce login.

Enabling email approval response lets users reply to email approval requests by typing APPROVE or REJECT in the first line and adding comments in the second line.

Possible email response reply for approving records,

1. APPROVE
2. APPROVED
3. YES

Possible email response reply for rejecting record,

1. REJECT
2. REJECTED
3. NO


If replying via email, you can also add comments on the second line. Comments will be stored with the approval request in Salesforce CRM.

Reference:

Cannot Modify Active/Once Active Approval Process Definition in Salesforce

Here is way to delete an approval process if you received a below error "Cannot Modify Active/Once Active Approval Process Definition".

       1. Active approval processes can't be deleted.
       2. Before deleting an approval process, make sure it is inactive and that no records have been submitted for approval.
       3. If any records have been submitted, delete them and remove them from the Recycle Bin.


Refer the below useful links,


Thursday 29 October 2015

Code coverage or No test coverage issue in Salesforce

Some people's say, I have wrote test classes but not getting code coverage. In this occasion, try the below steps. It might solve your problem.

Step 1: 

Ensure your test class declared with correct annotation,

1. Test class should start with @isTest annotation.
2. Methods should declared as testMethod or @isTest annotation.


  1. @isTest
  2. private class MyTestClass {
  3.    // Methods for testing
  4.    static testMethod void test1() {
  5.       // Implement test code
  6.    }
  7.    @isTest static void test2() {
  8.       // Implement test code
  9.    }
  10. }

Step 2: 

Go to Setup --> Build --> Develop --> Apex Test Execution -->  Options --> Disable Parallel Apex Testing

Ensure "Disable Parallel Apex Testing" check box is checked.

Note: Try the below solution if you received an error as "UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record"

Step 3: 

Go to Setup --> Build --> Develop --> Apex Test Execution -->  View Test History--> Clear all data

Step 4:
Go to Setup --> Build -->  Develop --> Apex Test Execution -->  Select Test --> Run

Conditional Rendering in Lightning Component using aura:if tag

Here is the basic example for iterating a collection or list of values in lightning component using aura:iteration tag.

Step 1:
Open Developer Console

Step 2:
Click File --> New --> Lightning Component --> Enter name as "ConditionalComponent" and Submit.

Clear the existing code and paste the below one,

  1. <aura:component >
  2.     <aura:attribute name="edit" type="Boolean"/>
  3.     <aura:if isTrue="{!v.edit}">
  4.         <ui:button label="Edit" />
  5.         <aura:set attribute="else">
  6.             <ui:button label="Edit" disabled="true"/>
  7.         </aura:set>
  8.     </aura:if>
  9. </aura:component>


Step 3:
Use existing app or new one and paste the below code.

For "edit" attribute i set attribute value as true in the below code. In this case button will be visible, if you set "edit" attribute as false, Button will will disabled.


  1. <aura:application >
  2.     <c:ConditionalComponent edit="true"/>
  3. </aura:application>

For True condition, set attribute value as below,

  1.     <c:ConditionalComponent edit="true"/>

For False condition, set attribute value as below,

  1.     <c:ConditionalComponent edit="false"/>

How to Encrypt and Decrypt a Value in Salesforce Using Apex Crypto Class

As per the salesforce documentation, the following algorithms are supported. Please read it before use.

The Crypto class provides the following functions to encrypt and decrypt using the AES algorithm:

encrypt()
decrypt()
encryptWithManagedIV()
decryptWithManagedIV()

The following considerations should be noted:

  • The AES128, AES192 and AES256 algorithms are supported
  • A private key can either be generated externally or via the Crypto.generateAESKey(Integer size) method. The length of the private key must match to the specified algorithm.
  • The private key should not be hardcoded in the Apex code. Instead, it should be placed in a protected custom setting.
  • The standard AES algorithm is used with a Cipher Mode of Cipher Block Chaining (CBC) and PKCS#5 padding. Ensure that any applications that you interact with use the same parameters.(Note that PKCS#5 and PKCS#7 are compatible.)
  • The algorithm requires an initialization vector of 16 bytes (128 bits). Use the encryptWithManagedIV() function to have Salesforce generate the IV for you in the first 16 bytes of the cipher text.Third party systems that receive the cipher should extract the IV from the first 16 bits. If third party systems send the IV in the first 16 bytes of the cipher, then use the decryptWithManagedIV() method to decrypt.
  • If you intend to generate your own initialization vector, then use the encrypt() and/or decrypt() methods, in which the IV is sent as a separate argument. Note that the cipher text passed to the decrypt() method should not contain the IV in the first 16 bytes and neither does the encrypt() function place the IV in the first 16 bytes of the generated cipher.

The below example will illustrate how to encrypt and decrypt a data. Use this utility wherever you need. For demo purpose, I have hardcoded private key in code and used AES128 algorithm.


  1. public class CryptoUtil
  2. {
  3.     // This should be stored and referred from custom setting. Don't hard code here. For demo purpose i have hard coded.
  4.     static Blob encryptionKey = Blob.valueOf('8cPkWGCoHv9a3D7K');
  5.  
  6.     public static string encyptData(String decryptedString)
  7.     {
  8.         Blob data = Blob.valueOf(decryptedString);
  9.         Blob encryptedBlobData = Crypto.encryptWithManagedIV('AES128', encryptionKey , data );
  10.         String base64EncryptedString = EncodingUtil.base64Encode(encryptedBlobData);
  11.         return base64EncryptedString;
  12.     }
  13.    
  14.     public static string decryptData(String encryptedString)
  15.     {
  16.         Blob data = EncodingUtil.base64Decode(encryptedString);
  17.         Blob decryptedBlobData = Crypto.decryptWithManagedIV('AES128', encryptionKey , data);
  18.         String decryptedString= decryptedBlobData.toString();
  19.         return decryptedString;
  20.     }
  21.  
  22. }


Run the below code snippet from Developer Console:

  1. String encryptedResult = CryptoUtil.encyptData('Arunkumar');
  2. System.debug('## Encrypted Result----'+encryptedResult);
  3.        
  4. String decryptedResult = CryptoUtil.decryptData(encryptedResult);
  5. System.debug('## Decrypted Result----'+decryptedResult);

You will get an output like below,



Reference:

Monday 26 October 2015

How to use aura iteration in lightning component salesforce

Here is the basic example for iterating a collection or list of values in lightning component using aura:iteration tag.

Step 1:
Open Developer Console

Step 2:
Click File --> New --> Lightning Component --> Enter name as "DisplayNumbers" and Submit.

Clear the existing code and paste the below one,

  1. <aura:component >
  2.     <aura:attribute name="numbers" type="List"/>
  3.     <ui:button press="{!c.getNumbers}" label="Display Numbers" />
  4.     <aura:iteration items="{!v.numbers}" var="num">
  5.         <br/> Number: {!num.value}
  6.     </aura:iteration>
  7. </aura:component>

Step 3:
Double Click on Controller in right hand side of the component. Clear the existing code and paste the below one,

  1. ({
  2.     getNumbers: function(component) {
  3.         var numbers = [];
  4.         for (var i = 0; i < 10; i++) {
  5.             numbers.push({
  6.                 value: i
  7.             });
  8.         }
  9.         component.set("v.numbers", numbers);
  10.     }
  11. })

Create a lighting app and refer the above created lightning component and run it.

Output:


Friday 23 October 2015

How to validate a field value in lighting component

       Validating form is more over important in UI development. Here is the simple example for validating a number field in lightning component app. 

Please take a look on the below link, if you don't know about creating lighting component.

Lightning Component Code:

  1. <aura:component >
  2.     <center>
  3.         <br/>
  4.         <h5> <b> Age Validator </b> </h5> <br/>
  5.         Enter your age: <ui:inputNumber aura:id="inputCmp"/> <br/>
  6.         <ui:button label="Submit" press="{!c.validateAge}"/>
  7.     </center>
  8. </aura:component>

Component Controller Code:

  1. {
  2.     validateAge : function(component) {
  3.         var ageField = component.find("inputCmp");
  4.         var ageValue = ageField.get("v.value");
  5.        
  6.         if(isNaN(ageValue) || ageValue == '')
  7.         {
  8.             ageField.set("v.errors"[{message:"Enter a valid age."}]);
  9.         }
  10.         else if(parseInt(ageValue) > 150)
  11.         {
  12.             ageField.set("v.errors"[{message:"This is not a valid age."}]);
  13.         }
  14.         else if(parseInt(ageValue) < 18)
  15.         {
  16.             ageField.set("v.errors"[{message:"Minimum age to submit this form should be greater or equal 18"}]);
  17.         }
  18.         else
  19.         {
  20.             ageField.set("v.errors"null);
  21.         }
  22.     }
  23. }

Output:


Build Your First Lightning Component App In Salesforce

     Here is the simple example to add two numbers by using lightning component framework. Follow the below steps,

Step 1:

Open Developer Console

Step 2:

Click File --> New --> Lightning Component --> Enter name as "CalculateTotal" and Submit.

  1. <aura:component >
  2.     <center>
  3.         <br/>
  4.         <br/>
  5.         <h2> <b> Simple Calculation </b> </h2>
  6.         <br/>
  7.         <ui:inputNumber aura:id="inputOne" label="First Value" /> <br/>
  8.         <ui:inputNumber aura:id="inputTwo" label="Second Value"/> <br/>
  9.         <ui:button label="Calculate" press="{!c.calculate}"/> <br/> <br/>
  10.         Total Amount is: <ui:outputNumber aura:id="totalValue" value="{!v.totalValue}"/>
  11.     </center>
  12. </aura:component>

Step 3:

Double Click on Controller in right hand side of the component. Clear the existing code and paste the below one,

  1. ({
  2.      calculate : function(component)
  3.       {
  4.         var v1 = parseInt(component.find("inputOne").get("v.value"));
  5.         var v2 = parseInt(component.find("inputTwo").get("v.value"));
  6.         var total = (v1 + v2);
  7.         console.log(total);
  8.         component.find("totalValue").set("v.value", total);
  9.         }
  10. })

Step 4:

Click File --> New --> Lightning Application --> Enter name as "SimpleLightningApp" and Submit. Clear the existing code and paste the below one,

  1. <aura:application >
  2.     <c:CalculateTotal />
  3. </aura:application>

Click on Preview or Updated Preview button in the right hand side of the component.

You can also check How to run or execute a Lightning app in Salesforce.

Short explanation of the code:

1. ui:inputNumber attributes have been declared in the component with respective aura:id of inputOne, inputTwo.

2. ui:Button attribute has been declared with "press" event, this will call c.calculate. Here calculate is the method name of the related controller.

3. ui:outputNumber attribute has been declared and value assigned as {!v.totalValue}. Here "v" denotes view.

4. On the controller side, accessed the two input value by using their respective id's.

5. Added those two values and assigned the result in totalValue attribute. This will displayed in the UI.

Note: If you are using namespace in your org, then you need to use your namespace in the place of "c" in the all above codes.


"c: is the default namespace for Lightning components"

For example,

Without namespace:

    <c:CalculateTotal />

With namespace:

    <MyOrgNameSpace:CalculateTotal />


Feel free to post your doubts as comments...!

Thanks for reading this..!

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...