• Chaten Raghav 9
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
Hello
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;
            if(a.Contacts.isEmpty()){
                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;
            }
            else
                count=count+1;
        }
    }

    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.setToAddresses(toAddresses);
        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:
@isTest
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';

        test.startTest();
        GenerateContacts gen=new GenerateContacts(Query); 
        String jobid = Database.executeBatch(gen);
        test.stopTest();
    }
}

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

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

Hi,
     
          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 />
</div><br/><hr/>
<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><br/><br/><br/>

</table>
<table>
<apex:repeat value="{!fields}" var="f">
<tr>
<td>
<strong> <apex:outputlabel value="{!f.label}" /></strong> </td> <td> <apex:inputField value="{!feildvalue[f.fieldPath]}" />
</td>
</tr>
</apex:repeat>
</table>
<apex:commandButton action="{!UpdateEntered}" value="Update" style="margin:30px 0px 10px 100px" />
</apex:panelGrid>
<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"/>
</div>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

Class:

======================================================================================

public class Cls_Update_Size {

public boolean updateButton { get; set; }

public String inputSection { get; set; }

public PageReference UpdateEntered(){
system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+feildvalue);
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];
lstFieldSetMember.addall(SObjectType.Samples_Mgmt__c.FieldSets.Search_Barcode.getFields());
lstwrap=new List<wrap>();
}

public PageReference Find(){
updateButton =true;
selectedFeilds.clear();
for(wrap w: getWarpmthd()) {
if(w.selected == true) {
selectedFeilds.add(w.fl);
}
}
feildvalue=new Samples_Mgmt__c();
system.debug('******************************'+getFeildvalues());
feildvalue=getFeildvalues();
lstwrap.clear();

return null;
}


public Samples_Mgmt__c getFeildvalues(){
SamId='a0UK0000001jFOz';

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;
fl=m;
}
}

}

 

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;
            this.x=false;
        }
   }
   
   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:form>
        <apex:pageblock>
            <apex:pageBlockTable value="{!wrapperList}" var="wp">
                <apex:column headerValue="boolean">
                    <apex:inputCheckbox value="{!wp.x}"/>
                </apex:column>    
                <apex:column value="{!wp.acc.name}"/>            
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

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?

 

Regards

Sam