• Chaten Raghav 9
  • 0 Points
  • Member since 2014

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 5
I have pulled together a Batchable APEX class below that generates missing Contacts from a list of Accounts:
global class GenerateContacts implements 
    Database.Batchable<sObject>, Database.Stateful{

   global final String Query;
   global integer count;
   global GenerateContacts(String q){Query=q;
     count = 0;

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        Integer sp;
        String ln;
        String fn;
        for(sObject s : scope){
            Account a = (Account)s;
                System.debug('email: ' + a.Email_2__c);
                System.debug('name: ' + a.Name);
                sp = a.Name.LastIndexOf(' ');
                if (sp > 0) {
                    ln = a.Name.substring(sp);
                    fn = a.Name.substring(0, sp);
                sObject ct = new Contact(AccountId = a.Id, Email = a.Email_2__c, FirstName = fn, LastName = ln);
                insert ct;

    global void finish(Database.BatchableContext BC){
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob WHERE Id = :BC.getJobId()];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'me@mydomain.net'};
        mail.setSubject('Contacts Generation ' + a.Status);
        mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures. Count was: '+count);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

My problem is with generating a Test class with greater than 75% coverage.  Here is my Test class:
private class Test_Generate_Contacts {
    static testMethod void test_GenerateContacts() {
        String Query = 'Select a.Id, Email_2__c, First_Name__c, Last_Name__c, Name, (Select Id From Contacts limit 1) from Account a LIMIT 1';

        GenerateContacts gen=new GenerateContacts(Query); 
        String jobid = Database.executeBatch(gen);

This one only gives me 64%.  How can I get it higher?

I am new to Salesforce and therefore want to know the exact functioning of escapeSingleQuotes() method.
My code uses this method as:

soql = 'Select Id, Name, Metals__c from Smelter__c where';
        soql += ' Name LIKE \''+String.escapeSingleQuotes(smelterNameQuery.trim())+'%\'';

The name consists of single quote at the end of string.
So how do I remove the single quote.?

          I want to know how can i implement a custom webservice program by using soap by using this 

how contact can insert into a particular account,how opportunity insert into particular account, how quote can insert particular opportunity can you give some sample code, i geetings those fields from external system

Hi I am getting vf page error saying that id Not specifeid in update call. i got the record values using dynamic query into vf as inputfeild.when try to update the values on click of UpdateBarcode  geting the  error.


Vf :


<apex:page controller="Cls_Update_Size" sidebar="false" showHeader="false" >
<apex:form >
<apex:pageBlock id="repeat" >
<div style="margin:10px 300px 10px 400px"><h3 style="margin:10px 10px 10px 2px">Enter Barcode</h3><br/>
<apex:inputText value="{!Barcode}" size="40"/>
<apex:pageMessages />
<apex:panelGrid >
<apex:facet name="header">Select the Feilds to Update</apex:facet>
<table style="margin:0px 0px 0px 0px">
<apex:repeat value="{!Warpmthd}" var="c" id="table" >
<td><apex:inputCheckbox value="{!c.selected}"/>
<apex:outputlabel value="{!c.fl.Label}"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>

<apex:repeat value="{!fields}" var="f">
<strong> <apex:outputlabel value="{!f.label}" /></strong> </td> <td> <apex:inputField value="{!feildvalue[f.fieldPath]}" />
<apex:commandButton action="{!UpdateEntered}" value="Update" style="margin:30px 0px 10px 100px" />
<div style="margin:10px 0px 30px 1100px" ><br/><br/>
<apex:commandButton action="{!Find}" value="Find" rerender="repeat"/>&nbsp;&nbsp;
<apex:commandButton action="{!Oknext}" value="Ok and Next"/>&nbsp;&nbsp;
<apex:commandButton action="{!Close}" value="Close"/>





public class Cls_Update_Size {

public boolean updateButton { get; set; }

public String inputSection { get; set; }

public PageReference UpdateEntered(){
update feildvalue;
return null;

public Samples_Mgmt__c feildvalue{ get; set; }
Public Id SamId=null;
Public Id OffId=null;
public Samples_Mgmt__c sample;
public string size;
list<Samples_Mgmt__c> lstsam= new list<Samples_Mgmt__c>();
list<offer__c> lstoff= new list<offer__c>();
public String Barcode { get; set;}
public list<wrap> lstwrap {get; set;}
List<Schema.FieldSetMember> lstFieldSetMember =new List<Schema.FieldSetMember>();
List<Schema.FieldSetMember> selectedFeilds = new List<Schema.FieldSetMember>();

public Cls_Update_Size(){

lstsam=[select id,Bar_Code__c from Samples_Mgmt__c];
lstwrap=new List<wrap>();

public PageReference Find(){
updateButton =true;
for(wrap w: getWarpmthd()) {
if(w.selected == true) {
feildvalue=new Samples_Mgmt__c();

return null;

public Samples_Mgmt__c getFeildvalues(){

String query = 'SELECT ';
for(Schema.FieldSetMember f : this.getFields()) {
query += f.getFieldPath() + ', ';
query += 'Id, Name FROM Samples_Mgmt__c where id=:SamId';
return Database.query(query);

public List<Schema.FieldSetMember> getFields(){

return selectedFeilds;

public List<wrap> getWarpmthd(){

for(Schema.FieldSetMember mem :lstFieldSetMember){
lstwrap.add(new wrap(mem));
return lstwrap;
Public class Wrap{
public Boolean selected{get; set;}
public Schema.FieldSetMember fl{get; set;}
public Wrap(Schema.FieldSetMember m){
selected = false;



Thanks in advance...

Hi ,


In our project we have many tables where we are required to display checkboxes and the associated records. So we have used  an equal number of wrapper classes for each object. I was planning to generalize it, by soing something like this(below code), but was unsuccessful. I am not sure how to cast an generic sObject back to a specific object in a VF page


//apex Class:

public class genericObjectCreation{
    public class wrapperClass{
        //using a generic sObject.
        public sObject acc {get;set;}
        public boolean x {get;set;}
        public wrapperClass(sObject a){
            this.acc = a;
   public list<wrapperClass> wrapperList{get;set;}
   public genericObjectCreation(){
       wrapperList = new list<wrapperClass>();
       for(account a : [select name from account]){
            wrapperList.add(new wrapperClass(a));

//VF Page:
<apex:page controller="genericObjectCreation">
            <apex:pageBlockTable value="{!wrapperList}" var="wp">
                <apex:column headerValue="boolean">
                    <apex:inputCheckbox value="{!wp.x}"/>
                <apex:column value="{!wp.acc.name}"/>            

When I open the VF page I get an error stating:
ERROR: Unknown property 'SObject.name'
Error is in expression '{!wp.acc.name}' in component <apex:page> in page genericexample

 The error makes sense coz I am not casting the generic sObject back to account, the VF page is unable to get the value of .name. Any ideas how to go about it?