Wednesday, 16 December 2015

Fetech contacts data in excel sheet through visual force page


<apex:page controller="FetchdatathroughCSV" sidebar="false" action="{!Fetechdata}" cache="false" contentType="text/plain/#contacts.csv">
<apex:repeat value="{!con}" var="r">
  {!r.firstname}{!r.lastname}
</apex:repeat>
</apex:page>
public class FetchdatathroughCSV {  public List<Contact> con {get;set;}  public PageReference Fetechdata()  {    con = [Select Id ,firstName ,LastName from contact limit 10];    return null;  } }

Dynamically select the record type by using custom button javascript

When i tried to create a contact record without selection of record type it is not possible from the standard button. So i came up with some custom java script button.

To create a custom button navigate to Setup- Customize - Contacts - Buttons, Links, and Actions

Click on Buttons, Links, and Actions and click new Button or link. Give the Name and api name and select the Display Type as List Button, behavior as Execute javascript and content source as onclick javascript.

Paste the below code in the space provided. change the recordtype name to your record type name.

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
 
var qr = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType ='" + "Contact" + "'" +" AND DeveloperName = '"+"RecordTypeName"+ "'"); 
var records = qr.getArray("records"); 
 
if(!records.isEmpty)
 var redirecturl="/003/e?retURL={!Account.Id}&con4={!Account.Name}&con4_lkid={!Account.Id}&RecordType="+qr.records.Id; 
 window.location=redirecturl; 
}

Tuesday, 3 November 2015

sample Batch Example

global class BatchExample implements Database.Batchable<sobject>
{

 global Database.QueryLocator start (Database.BatchableContext bc)
 {
  string qry = 'Select Id,Name from Account';
  return Database.getQueryLocator(qry);
 }
 
 global void execute (Database.BatchableContext bc,List<Account> scope)
 { 
   for(Account acc :scope)
   {
     acc.name = acc.name + 'vara';
      // acc.name = acc.Name.substring(0,acc.name.Indexof('vara'));
   }
     update scope;
     system.debug('-------------Batch--------- '+scope);
 }
 
 global void finish (Database.Batchablecontext bc)
 {
 
 }

}

Friday, 9 October 2015

update the field values from child - to - Parent Trigger

parent --> quote
child --->quote_product__c

when ever quote _Product__c  object update values related quote object fileds also get updated

trigger UpdateFieldsfromQuote on Quote_Product__c (after update)
{
 Map<id,Quote_Product__c> Mapquoteprod =new Map<Id,Quote_Product__c>();
 for(Quote_Product__c qtp :trigger.new)
 {
    Mapquoteprod.put(qtp.Quote__c,qtp);
 }
    List<Quote> qtList = new List<Quote>();

 for(Quote qt  : [Select id,Brightness__c ,Color_styles__c,Ash__c,Bulk_cc__c
                                                                            from Quote where id =:Mapquoteprod.keyset()])
 {
   qt.Brightness__c    = Mapquoteprod.get(qt.id).Brightness__c;
   qt.Color_styles__c  = Mapquoteprod.get(qt.id).of_Colors_Styles__c;
   qt.Ash__c           = Mapquoteprod.get(qt.id).Ash__c;
   qt.Bulk_cc__c       = Mapquoteprod.get(qt.id).Bulk_CC_gm__c;
   qtList.add(qt);
 }
   if(!qtList.isempty())
   {
     update qtList;
   }
}

update the Field values from parent to child Trigger

Where Account name, phone is update  related contact Lastname and phone also update.

trigger ContactUpdate on Account (after update)
 {
   set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Contact> listContact = new list<Contact>();
    for(Account acct : trigger.new) 
    {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    listContact = [SELECT LastName ,Phone , AccountId FROM Contact WHERE AccountId IN : acctIds];
    
   if(listContact.size() > 0) 
   {
        for(Contact con : listContact)
        {
            con.LastName= mapAccount.get(con.AccountId).Name;
            con.phone= mapAccount.get(con.AccountId).phone;
        }
        update listContact;
    }

}

display multiple object Records using wraper class

public with sharing class wrappercls {

public class accwrapper{
public Account acc{get; set;}
public List<Contact> lstcon{get; set;}
public List<Case> lstcases{get; set;}
public accwrapper(Account a, List<Contact> lst, List<case> lstcase){
acc = a;
lstcon = lst;
lstcases = lstcase;
}
}

public List<accwrapper> lstwrap{get; set;}

public wrappercls(){

lstwrap = new List<accwrapper>();
List<account> lst = [Select id, name,(Select id, name from Contacts),(Select id,CaseNumber  from cases) from account order by name];

for(Account acc : lst){
if(acc.contacts.size()>1)
lstwrap.add(new accwrapper(acc,acc.contacts,acc.cases));
}
}

}


<apex:page controller="wrappercls">
<apex:form >
<apex:pageblock >
<apex:pageblocktable value="{!lstwrap}" var="w">
<apex:column value="{!w.acc.name}"/>
<apex:column >
<apex:repeat value="{!w.lstcon}" var="c">
{!c.name}<br/>
</apex:repeat>
</apex:column>
<apex:column >
<apex:repeat value="{!w.lstcases}" var="c">
{!c.casenumber}<br/>
</apex:repeat>
</apex:column>
</apex:pageblocktable>
</apex:pageblock>
</apex:form>
</apex:page>

Sunday, 9 August 2015

javascript in visualforce page

<apex:page showHeader="false" sidebar="false">
<apex:includeScript value="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js" />
<apex:includeScript value="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular.min.js"/>
<apex:includeScript value="{!$Resource.jsForce}" />
<html xmlns:ng="http://angularjs.org" ng-app="hello" lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.no-icons.min.css" rel="stylesheet"/>
<link href="https://netdna.bootstrapcdn.com/font-awesome/2.0/css/font-awesome.css" rel="stylesheet"/>
<style>
.input-mysize { width: 900px }
.search-query {
padding-left: 469px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ5JREFUeNpi+P//PwMQMANxERCfAeI/UBrEZwbJQ9WAFR0A4u1AbAnEbFB6O1ScGaawGoi3wHQiYyBYDZKHKbwHxLo4FOqC5GEKf4Ksw6EQ5IyfIDYTkPEUiNUZsAOQ+F9GRkYJEKcFiDficSOIcRjE4QTiY0C8DuRbqAJLKP8/FP9kQArHUiA+jySJjA8w4LAS5KZd0MAHhaccQIABALsMiBZy4YLtAAAAAElFTkSuQmCC);
background-repeat: no-repeat;
background-position: 562px 8px;
}
</style>
</head>
<div class="ng-app">
<div class="navbar">
<div class="navbar-inner">
<apex:image url="{!$Resource.jsforcelogo}" width="50" height="50"/>
<a class="brand" href="">JSforce with Angularjs on Force.com - By OyeCode (Harshit) </a>
</div>
</div>
<div ng-controller="ctrlRead">
<div class="input-append">
<input type="text" ng-model="query" class="input-mysize search-query" placeholder="Search"/>
</div>
<table class="table table-hover success">
<thead>
<tr>
<th class="id">Id&nbsp;<a ng-click="sort_by('id')"><i class="icon-sort"></i></a></th>
<th class="name">Name&nbsp;<a ng-click="sort_by('name')"><i class="icon-sort"></i></a></th>
<th class="Phone">Phone&nbsp;<a ng-click="sort_by('Phone')"><i class="icon-sort"></i></a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in pagedItems|filter:query" class="success">
<td class="info">
<apex:outputLink value="/{{item.Id}}">
<apex:outputText value="{{item.Name}}" />
</apex:outputLink>
</td>
<td>
<apex:outputText value="{{item.Phone}}" />
</td>
<td>
<apex:outputText value="{{item.Title}}" />
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!--- Javascript -->
<script type="text/javascript">
function ctrlRead($scope){
var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}' });
var query = "SELECT Id, Name, Phone, Title from Contact ORDER BY Name ASC LIMIT 1000";
conn.query(query , function(error, res) {
if (error) {
console.log("error");
} else {
$scope.pagedItems = res.records;
$scope.$apply();
}
});
}
</script>
</html>
</apex:page>