function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
catchforshivacatchforshiva 

How to insert records CSV format in child Obj based on field in parent Obj without using Dataloader

Parent Object:

 

PName: (Primary field)

price:

county:

customerID:(child lookup field)


Given the above record on the parent object, I want the following records to be automatically created on the child object:

 

Child Object:

 

customerID:

customerName:

Address:

Tprice:

 
How to archive this ?

catchforshivacatchforshiva

This code :

VF Code


<apex:page controller="UploadDoc">
    <apex:form >
       <apex:sectionHeader title="Upload a Document into Salesforce"/>
          <apex:pageblock >
              <apex:panelGroup >
                <center>   
                    Import DTR File &nbsp;&nbsp;&nbsp;
                    <apex:inputfile value="{!myImage}"/>
                    <P align="">(file formats .csv, .txt)</p><br/><br/><br/>
                    <apex:commandButton action="{!readFile}" value="Submit"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;                    
                    <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
               </center>
            </apex:panelGroup>
        </apex:pageblock>   
    </apex:form>
</apex:page>

Apex Class:


public class UploadDoc {

    public PageReference cancel() {
        return null;
    }

       public blob myImage { get; set; }
       //file (blob) as String

       public PageReference readFile() {

       String fContent=myImage.toString();
        // line by line
       String[] NewLineSplit=fContent.split('\n');

       List<Property__c> lstpropty=new List<Property__c>();
       for(Integer i=0;i<NewLineSplit.size();i++){
        //Each line (record)
       String[] inputvalues = NewLineSplit[i].split(',');
       
    Property__c objpropty=new Property__c();
       
       objpropty.Name=inputvalues[0];     
       objpropty.price__c= Integer.valueOf(inputvalues[1]);
       objpropty.county__c= Integer.valueOf(inputvalues[2]);
       objpropty.Name.CustmerID__c=String.valueOf(inputvalues[3]);
           lstpropty.add(objpropty);
       }
       
try
{
// Inserting the data in to object
       insert lstpropty;        
}

catch(Exception e)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'An error has occured. Please check the template or try again later'+e));
return null;
}

}

catchforshivacatchforshiva

Hi Friends...

 

Any one can achieve this problem....

 

reply as soon as ....