A blog about Salesforce CRM Configuration and development

Saturday, 23 June 2018

Generate DataMatrix code in Salesforce

The Data Matrix is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern, also known as a matrix. The information to be encoded can be text or numeric data.

To generate DataMatrix code in Salesforce for object records fields, I am going to use this website (https://barcode.tec-it.com/) which provided all kinds of barcode generation url.

Below are steps to generate DataMatrix code.

Create a custom formula field on the Object.






















IMAGE('https://barcode.tec-it.com/barcode.ashx?data='+ Name + '&code=DataMatrix&multiplebarcodes=false&translate-esc=false&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=%23000000&bgcolor=%23ffffff&qunit=Mm&quiet=0&dmsize=Default', 'DataMatrix Code')

The generated DataMatrix code will show on Account record detail page.




















Now, show this DataMatrix code on the Visualforce page (Render page as PDF).

<apex:page standardController="Account" renderAs="pdf"  docType="html-5.0">
 <apex:form >
     <apex:outputText value="{!account.DataMatrix_Code__c}" escape="false" style="float:right;"/><br/><br/>
      Name: &nbsp; <apex:outputText value="{!account.Name}"/><br/>
      Email: &nbsp; <apex:outputtext value="{!account.AccountNumber}" />
 </apex:form>
</apex:page>

Output: As you can see DataMatrix code is not showing properly on PDF page.




















To make this DataMatrix code render properly on PDF page We need to add this URL ( https://barcode.tec-it.com ) to a remote site setting.






Share:

Apex Trigger: Calculate the sum of child records field value and update in its parent object record field.

/* Name:UpdateParentTotalField
* Description: This trigger will calculate the sum of child records field values and update in its parent object record field. it will fire
* on both insert and update event.
*/

trigger UpdateParentTotalField on TestChild__c (after insert,after update) {
    
    set<id> prentsIds=new set<id>();
    if(Trigger.isInsert || Trigger.isUpdate){
        for(TestChild__c tc:Trigger.new){
            prentsIds.add(tc.TestParent__c);
        }
    }
    list<TestParent__c> parentList=new List<TestParent__c>();
    
    for(TestParent__c tp:[select id,Total_No__c,(select id,No__c from TestChilds__r) from TestParent__c where id IN:prentsIds]){
        TestParent__c obj=new TestParent__c();
        obj.id=tp.Id;
        for(TestChild__c tc:tp.TestChilds__r){
            if( obj.Total_No__c==null){
                obj.Total_No__c=0;
            }
            obj.Total_No__c+=tc.No__c;
        }
        parentList.add(obj);
    }
    if(parentList.size()>0){
        try{
            update parentList;
        }
        catch(DMLException e){
            system.debug('Exception has occured! ' +e.getMessage());
        }
    }
}
Share:

Apex Trigger: Update Fields on Case Records when a task is created/updated on Case.



/* Name: TaskDemoTrigger
 * Description: This Trigger will update a field on Case when a task is created or updated.
*/

trigger TaskDemo1Trg on Task (after insert,after update) {
 
    set<id> parentId=new set<id>();
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Task t:trigger.new){
            if(t.whatid.getSobjectType()==Case.sObjectType){
                parentId.add(t.whatId);
            }
        }
    }
    system.debug('Parent id is: ' +parentId);
    Map<id,Case> caseMap=new Map<id,Case>([select id,Tasks_Created_Date__c from Case where id IN: parentId]);
    list<Case> caseList=new List<Case>();
 
    for(Task t:Trigger.new){
        if(t.WhatId.getSObjectType()==Case.sObjectType){
            if(caseMap.containsKey(t.WhatId)){
                Case c=caseMap.get(t.WhatId);
                if(c!=null){
                    c.Tasks_Created_Date__c=t.Subject +' : '+ string.valueOf(t.CreatedDate);
                }
            }
        }
        if(!caseMap.isEmpty()){
            update caseMap.values();
        }
    }
}
Share:

Salesforce - Tooling API ( How to retrieve Metadata Information using Tooling API )

Tooling API:

Tooling API exposes metadata information of Org, we can access Metadata information using REST or SOAP. Tooling API retrieve the small piece of metadata, we can use this to develop an interactive application or tools for developers.


We can accomplish following tasks using Tooling API.:
  • Retrieve metadata about an object’s field.
  • Retrieve metadata of custom and standard object properties.
  • Get metadata information about Apex Classes, Visualforce Pages, Apex Triggers.
  • Get Code coverage details of Apex classes and Trigger.
  • Retrieve metadata information about ValidationRule and WorkflowRules.
  • Query RecentItems used in your SFDC org.
  • Etc...
In this blog, I am going to retrieve Metadata information about ValidationRule and WorkflowRule. I am using REST (GET) method here to make a callout.


HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); //Get user Session ID
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
string query='Select+id,FullName,createdDate,TableEnumOrId,ValidationName,Metadata+from+ValidationRule';
//string query='Select+id,Name+from+WorkflowRule'; //use this query to get Workflow Rule
req.setEndpoint(domainUrl+'/services/data/v33.0/tooling/query/?q='+query);
req.setMethod('GET');
Http h = new Http();
HttpResponse response = h.send(req);
system.debug(response.getBody()); 

JSON Response:

{
"size": 1,
"totalSize": 1,
"done": true,
"queryLocator": null,
"entityTypeName": "ValidationRule",
"records": [
{
"attributes": {
"type": "ValidationRule",
"url": "/services/data/v33.0/tooling/sobjects/ValidationRule/03df40000002gPOAAY"
},
"Id": "03df40000002gPOAAY",
"FullName": "Account.Account_Name_not_starts_with_Test",
"CreatedDate": "2018-06-15T05:41:21.000+0000",
"TableEnumOrId": "Account",
"ValidationName": "Account_Name_not_starts_with_Test",
"Metadata": {
"description": null,
"errorConditionFormula": "CONTAINS(Name, 'Test')",
"errorDisplayField": null,
"errorMessage": "Account name should not contains Test",
"urls": null,
"active": true
}
}
]
}

We can parse this JSON response according to our requirements.

The Tooling API Objects. https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_list.htm

For more information on this please refer this link






Share:
Trailhead Profile


Follow by Email

Total Pageviews

Followers

Popular Posts

Powered by Blogger.

Contact form

Name

Email *

Message *