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
Guru Vemuru 1Guru Vemuru 1 

How to save searched record into other object?

Hi,

I have Two objects Invoice__c and Test_Search__c. Now i am gona search record of invoce from Test_Search__c object.
Now i want save the record in Test_Search__c.
I written class and page.
i am able to search record but unable to save record in Test_search__c object.

Class:

public class theController {

    public theController(ApexPages.StandardController controller) {

    }


   String searchText;
   List<Invoice__c> results;

   public String getSearchText() {
      return searchText;
   }

   public void setSearchText(String s) {
      searchText = s;
   }

   public List<Invoice__c> getResults() {
      return results;
   }

   public PageReference doSearch() {
      results = (list<Invoice__c>)[FIND :searchText RETURNING Invoice__c (Account_Name__c,Id,Order_Total_Before_Tax__c,Order_Total_After_Tax__c)][0];
      return null;
   }

}


Pages:
<apex:page extensions="theController"  standardController="Test_Search__c">
   <apex:form >
      <apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}"
                                      rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Results" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l"
                               rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.Account_Name__c}"/>
              <apex:column value="{!l.Order_Total_Before_Tax__c}"/>
              <apex:column value="{!l.Order_Total_After_Tax__c}"/>
           </apex:pageBlockTable>
        
        </apex:pageBlockSection>
        <div align="center" draggable="True" >
                        <apex:commandButton action="{!Save}" value="Save"  />&nbsp;
                        <apex:commandButton action="{!Cancel}" value="Cancel"  />          
                    </div>
        
      </apex:pageBlock>
   </apex:form>
</apex:page>

 
Best Answer chosen by Guru Vemuru 1
Abhishek BansalAbhishek Bansal
Hi Guru,

Can you please provide some more details on your requirement. If you want to save the results in the test search object then you have to add a new Save method in your controller class. The method will look like something below:
public PageReference Save(){
	List<Test_Search__c> listOfTestSearchToInsert = new List<Test_Search__c>();
	for(Invoice__c inv : results){
		Test_Search__c tstSearch = new Test_Search__c();
		tstSearch.Name = inv.Name;
		//Add other field values here.
		listOfTestSearchToInsert.add(tstSearch);
	}
	if(listOfTestSearchToInsert.size() > 0){
		insert listOfTestSearchToInsert;
	}
	
	return null;
}
Please let me know if you any further information or help on this.

Thanks,
Abhishek Bansal

All Answers

Abhishek BansalAbhishek Bansal
Hi Guru,

Can you please provide some more details on your requirement. If you want to save the results in the test search object then you have to add a new Save method in your controller class. The method will look like something below:
public PageReference Save(){
	List<Test_Search__c> listOfTestSearchToInsert = new List<Test_Search__c>();
	for(Invoice__c inv : results){
		Test_Search__c tstSearch = new Test_Search__c();
		tstSearch.Name = inv.Name;
		//Add other field values here.
		listOfTestSearchToInsert.add(tstSearch);
	}
	if(listOfTestSearchToInsert.size() > 0){
		insert listOfTestSearchToInsert;
	}
	
	return null;
}
Please let me know if you any further information or help on this.

Thanks,
Abhishek Bansal
This was selected as the best answer
Guru Vemuru 1Guru Vemuru 1
Thank you Abhishek Bansal,
1. I have invoice and invoice line item and they are in master detail relationship.
2.I have another object Test Search__c.
3. from Test_Search object, i need to search invoice object.--- (completed with apex class)
4.Save the record in Test_Search object.----(Completed by adding save method that you given)
5.Record is saving but Invoice how to show line items ---(can you help how to show line items in invoice object)

Updated apex class:

public class theController {

    public theController(ApexPages.StandardController controller) {

    }


   String searchText;
   List<Invoice__c> results;

   public String getSearchText() {
      return searchText;
   }

   public void setSearchText(String s) {
      searchText = s;
   }

   public List<Invoice__c> getResults() {
      return results;
   }

   public PageReference doSearch() {
      results = (list<Invoice__c>)[FIND :searchText RETURNING Invoice__c (Name,Account_Name__c,Id,Order_Total_Before_Tax__c,Order_Total_After_Tax__c)][0];
      return null;
   }
   
   public PageReference Save(){
    List<Test_Search__c> listOfTestSearchToInsert = new List<Test_Search__c>();
    for(Invoice__c inv : results){
        Test_Search__c tstSearch = new Test_Search__c();
        tstSearch.Amount_After_Tax__c= inv.Order_Total_After_Tax__c;
        tstSearch.Amount_Before_Tax__c = inv.Order_Total_Before_Tax__c;
      //  tstSearch.Name = inv.Order_Total_After_Tax__c;
        
        //Add other field values here.
        listOfTestSearchToInsert.add(tstSearch);
    }
    if(listOfTestSearchToInsert.size() > 0){
        insert listOfTestSearchToInsert;
    }
    
    return null;
}

}


 
Abhishek BansalAbhishek Bansal
Hi Guru,

I don't understand what you are trying to say here. Where do you want to show the line items. In our save method we are only inserting Test Search records and not invoice records so how can we show line items? Can you please clansaarify ?

Thanks,
Abhishek  Bansal.
Guru Vemuru 1Guru Vemuru 1
Hi Abhishek,
 This How it look like 3 objects.
User-added image

1.we are searching invoice from Test Seach and we are able to display like Name, Order Total after tax, order toatl before tax
2.We are save information in Test seach object
3. Now i want fetch information from Invoice Line Item like Product  and quantity of that invoce into Test Search Object.
4.How to include this functionality in our code.

Let me know still if you not understand.

 
Abhishek BansalAbhishek Bansal
Hi Guru,

Since there can be multiple Line Items under a single Invoice then how doe we get to know which line item should be used for Product and Quantity values ? Do you want to create as many records of Test Search as there are line items under a single Invoice ?
For Eg : Invoice A has Line Item 1,2 and 3 then do you want to create three Test Search records for each line item?

Thanks,
Abhishek Bansal.
Guru Vemuru 1Guru Vemuru 1
Yes, I want create Multiple line Items under one invoice.
I am thinking I have to create Test Search Line Item__c  Object and then i have to map Invoice line item__c with Test Search Line Item__c object.
Is it correct
 
Abhishek BansalAbhishek Bansal
Hi Guru,

Yes, that would work for you. Please close this question if your query is resolved.

Thanks,
Abhishek Bansal.
Guru Vemuru 1Guru Vemuru 1
Hello Abhishek,
Finally it is working.
Thank you abishek.