Wednesday 30 November 2016

How to determine the string type (Numeric, AlphaNumeric, etc.) in Salesforce

Introduction

Salesforce provides new string methods to determine what type of characters contains in a particular string by using some of the below methods.

Methods to Determine Character Types

Alpha

  1. String str = 'AlphaOnly';
  2. System.assertEquals(true, str.isAlpha());

The above string method will return true, if the string contain only characters.

isAlphaSpace

  1. String str = 'AlphaOnly Space';
  2. System.assertEquals(true, str.isAlphaSpace());

The above string method will return true, if the string contain alphabet and white spaces.

isAlphaNumeric

  1. String str = 'AlphaNumeric123';
  2. System.assertEquals(true, str.isAlphaNumeric());

The above string method will return true, if the string contain alphabet, numbers.

isAlphanumericSpace

  1. String str = 'Alpha Numeric123';
  2. System.assertEquals(true, str.isAlphanumericSpace());

The above string method will return true, if the string contain alphabet, numbers and white spaces.

isNumeric

  1. String str = '432';
  2. System.assertEquals(true, str.isNumeric());

The above string method will return true, if the string contain numbers only.

isNumericSpace

  1. String str = '432 ';
  2. System.assertEquals(true, str.isNumericSpace());

The above string method will return true, if the string contain numbers with spaces.

isWhitespace

  1. String str = ' ';
  2. System.assertEquals(true, str.isWhitespace());

The above string method will return true, if the string contain only white spaces.

Dynamically Assigning Fields in Salesforce With Example

Introduction
        Salesforce provides sObject methods and all of these methods are instance methods such as addError(), get(), etc. This article regarding the real time usage of put() method, It can be reduce code and make the code efficient instead of hard coding Field or Object Names. Recently we had a scenario like Field names should be assigned dynamically.

Consider the below two objects,
1. Opportunity (Standard Object) 
2. OpportunityStatusHistory (Custom Object)

Problem
        OpportunityStatusHistory object having the fields and that are named like Prospecting__c, Qualification__c, etc. The field names are similar to the StageNames in Opportunity object.

        Whenever the Opportunity record created or updated every time with the particular stage means we need to count the stage history details in “OpportunityStatusHistory” object. For example if I create Opportunity with Stage “Prospecting” means, OpportunityStatusHistory object record need to be created and the corresponding field Prospecting__c value will be set as 1.

        If I changed above opportunity Stage to “Qualification” means, the corresponding OpportunityStatusHistory object field Qualification__c value to be set as 1. If I went to backward stage i.e. ‘Prospecting” means the related Prospecting__c field in OpportunityStatusHistory record will get updated as 2.

  1. for(Opportunity currentOpp : Trigger.New)
  2. {
  3.    OpportunityStatusHistory__c statusHistory = new OpportunityStatusHistory__c();
  4.    if(currentOpp.StageName == 'Prospecting'){
  5.         statusHistory.Prospecting__c = '1;
  6.   }
  7.   else if(currentOpp.StageName == 'Qualification'){
  8.       statusHistory.Qualification__c = '1;
  9.     }
  10. }

This above code each and every time the field value is checked and assigned to the related OpportunityStatusHistory object. Suppose if you have 50 – 100 stages means the code goes complex. 

Solution
To overcome this issue, we can utilize the Dynamically getting field and assigning.

  1. Map<String, String> opportunityStatusFieldMap = new Map<String, String>();
  2. Map<String, Schema.SObjectField> opportunityStatusDescribe = OpportunityStatusHistory__c.sObjectType.getDescribe().fields.getMap();
  3. for(Schema.SObjectField currentField : loanStatusFieldsDescribe.Values())
  4. {
  5.   Schema.DescribeFieldResult fieldResult = currentField.getDescribe();  
  6. opportunityStatusFieldMap.put(fieldResult.getLabel(), fieldResult.getName());
  7. }    
  8. for(Opportunity currentOpp : Trigger.New)
  9. {
  10.     OpportunityStatusHistory__c statusHistory = new OpportunityStatusHistory__c();
  11.     statusHistory.put(loanStatusFieldsMap.get(currentLoan.Stage__c)1); // This put method used to dynamically assigning field values
  12. }

Conclusion
By using Dynamic DML we can easily get the field names dynamically at run time and insert them into the database using DML. This approach is efficient and easy to use than checking each and every time and assigning to the corresponding fields.

Escape and unescape String methods in Salesforce

Introduction
Salesforce provides lot of string methods similar to Java. But most of us familiar with the following string methods such as contains, replace, split, length, etc. This article is regarding the usage of escapeHtml and unescapeHtml String methods.

Escape String Method
This method escapes the characters in a String using HTML 4.0 entities. Consider the below example,

  1. String s = '"Salesforcekings.blogspot.in <BR>"';
  2. System.debug('Before Escape:'+s);
  3. String escapeString = s.escapeHtml4();
  4. System.debug('After Escape:'+escapeString);
             

The above string “MST Solutions <BR>” is formatted as escaped characters, the changes made in the string is below,
is formatted as &quot;
< is formatted as &lt;
> is formatted as &gt;

Unescape String Method

This method unescapes the characters in a String using HTML 4.0 entities. Consider the below example,

  1. String escapedString = '&quot;Salesforcekings.blogspot.in &lt;BR&gt;&quot;';
  2. System.debug('Escaped String:  '+escapedString);
  3. String unescapeString = escapedString.unescapeHtml4();
  4. System.debug('Unescaped String:  '+unescapeString);

 
The above escaped string &quot;Salesforcekings.blogspot.in &lt;BR&gt;&quot; is reverted to the original format i.e. "Salesforcekings.blogspot.in <BR>"

Usage
Consider you have a formula field with return type string in any one of the object and building values to this field using html tags such as <BR>. If you access this field value in Query Editor or Apex class you will get the escaped String values.
If you want this field value what exactly shows in the User interface means you will need to unescape this escaped string value before you need to use. If you do not formatted this means you will only get the escaped characters.

Conclusion
            By using this string methods we can escape and unescape the string based on the above example, it will be useful when you need to format the string.


Reference: New String Methods

Salesforce to NetSuite Integration

What is NetSuite OpenAir?

NetSuite OpenAir is the world's No. 1 Professional Services Automation (PSA) solution. From resource management and project management to time and expense tracking, project accounting, advanced billing and invoicing, the NetSuite OpenAir supports the entire professional services delivery lifecycle with a powerful Software-as-a-Services (SaaS) suite.

How does the NetSuite OpenAir XML work?

     NetSuite OpenAir provides OpenAir XML API as a layer for exchanging the NetSuite OpenAir data between the main site and peripheral programs.

     OpenAir XML API is based on industry standard components: HTTPS (Secure Hypertext
Transfer Protocol) and XML (Extensible Markup Language).

How to authenticate NetSuite Credential via Postman Extension using Google Chrome?

1.       Enter the Request URL.
For NetSuite Sandbox: https://sandbox.openair.com/api.pl
For NetSuite Production: https://www.openair.com/api.pl
2.       Add the XML Payload to Authenticate.
3.       Set the request method as “POST” and Send the Request.

Request payload:

                The below XML Payload are used to authenticate with NetSuite Open Air. The following attributes values are mandatory for authentication:

1. Namespace
2. Key
3. Company
4. User
5. Password

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <request API_ver="1.0" client="test app" client_ver="1.1" namespace="Namespace" key="API Key">
  3. <Auth>
  4. <Login>
  5. <company>Company Name</company>
  6. <user>User Name</user>
  7. <password>Password</password>
  8. </Login>
  9. </Auth>
  10. </request>

Response:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <response>
  3.     <Auth status = "0"></Auth >
  4. </response>




Note: <Auth status = “0”/> indicates the authentication is successful. In case of authentication failure, you will see an error code such as 401, 505, and etc.

How to read data from NetSuite?
                The below example shows how to retrieve the “Project” object record from NetSuite. The basic syntax to read the data as follows:

  1. <Read type="Project" method="all" limit="1"/>

Type - It denotes the NetSuite object/table Name.
Method - It denotes the search option. You can also read records based on filter/criteria, such as equal to, not equal to, and etc in the place of method attribute.
Limit - Number of records to Return.

Request payload:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <request API_ver="1.0" client="test app" client_ver="1.1" namespace="Namespace" key="API Key">
  3.        <Auth>
  4.     <Login>
  5.         <company>Company Name</company>
  6.         <user>User Name</user>
  7.         <password>Password</password>
  8.     </Login>
  9.       </Auth>
  10. <Read type="Project" method="all" limit="1"/>
  11. </request>

Response:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <response>
  3.  <Auth status = "0"></Auth >
  4.  <Read status = "0">
  5.       <Project>
  6.      <name>Standard Activities</name>
  7.      <only_owner_can_edit>1</only_owner_can_edit>
  8.      <id>4</id>
  9.      <customer_name>Test Company</customer_name>
  10.      </Project>
  11. </Read>
  12. </response>

How to write data to NetSuite?

The below example shows how to add/create “Project” object record to NetSuite. The basic syntax to create a record as follows:

  1. <Add type="Project" enable_custom="1">
  2. <Project>
  3.    <name>Test Project</name>
  4.    <customer_name>Demo Account</customer_name>
  5. </Project>
  6. </Add>

Add the above payload after the end of </Auth> tag. It is similar to the read request example.

Type - It denotes the NetSuite object/table Name.
enable_custom - It denotes the custom field to be included while creating record. Custom fields will be in the the form of <my_custom_field__c> </my_custom_field__c>. Similar to Salesforce custom field naming convention with “__c”.

How to modify/update data to NetSuite?

The below example shows how to modify/update the “Project” object record to NetSuite. The basic syntax to modify a record as follows:

  1. <Modify type="Project" enable_custom="1">
  2. <Project>
  3.     <id>192</id>
  4.     <name>Test Project Updated</name>
  5. </Project>
  6. </Modify>

ID field is required in order to update the record.

How to delete data from NetSuite?

The below example shows how to delete the “Project” object record to NetSuite. The basic syntax to delete a record as follows:

  1. <Delete type="Project" enable_custom="1">
  2. <Project>
  3.      <id>192</id>
  4. </Project>
  5. </Delete>

Id field is required in order to delete the record.

NetSuite Project View:

Here is the “Project” view of the NetSuite Open Air.

Integrating NetSuite with Salesforce:

Step 1: Create a HttpRequest class.
Step 2: Ensure the request method is set as POST.
Step 3: Build the above example payload structure, by using standard Reading and Writing XML Using the DOM method. Set the payload in the Place of setBody method of HttpRequest.
Step 4: Make a callout from where you need, such as in Visualforce page, Batch class, Button, and etc.
Step 5: Response will be in the form of XML. So, get the response of HttpRequest and process it according to your requirements.

Summary:

The NetSuite Open Air’s Professional Service Automation tool helps improve and optimize service organizations and also provides more visibility and control for project-based businesses.

References:
1.       NetSuite Open Air Guide.

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...