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
karthik Jonnalagaddakarthik Jonnalagadda 

How to Insert a record into custom object from Apex class ?

Hi,
 I am not able to insert records itno custom object. See the below code.
(My Requirement is when i click on custom button need to insert values in another object)

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var callout = "" + sforce.apex.execute("MyWebservice","myFunction",
    {param1:"{!Segment__c.Event__c}",param2:"{!Segment__c.Member_Name__c}"}); 


    if (callout == 'OK') { 
    alert("Webservice call was ok."); 
    window.location.reload(); 

else { 
    alert("Webservice call failed!"); 
}


global without sharing class MyWebservice {

    WebService static String myFunction(String param1,string param2) {

        if (String.isNotBlank(param1)) {

            try {
            
          Segment2__c Segment= new Segment2__c();
            Segment.Name='Karthik';
            Segment.Event__c= param1;
            Segment.Member_Name__c= param2;
             insert Segment;        
           
               return 'OK';    
            }
            catch (Exception e) {
                return 'ERROR';
            }
        }
        else {
            return 'ERROR';
        }
    }
}

I am calling myFunction from script I am getting responce OK But record is not inserting in the object.

Any one please respond quickly 

Thanks in advance. 
Best Answer chosen by karthik Jonnalagadda
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

No issue, each of us here have been new to salesforce sometime or other. Keep trying.

Run this query from developer console 'select id,name from Segment2__c' then check if there is any record.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi karthik Jonnalagadda,

1. Please check your debug logs if record is inserted.
2. If yes, Check your acess permissions over same object.

Let us know if it helps.
 
karthik Jonnalagaddakarthik Jonnalagadda
Hi Ashish Sharma,

Thanks for the quick responce,
 I didn't  find any recodrs in log, I have created this Segment2__c object.
 But , I am able to insert a record from Developer Console with the same code.

Was I did any wrong ?

Please suggest me any other alternative.

Thanks in Advance
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Please try below code. I have put a debug statement ,if record is inserted successfully ,then you will get id there.
Check for that id in your org.
global without sharing class MyWebservice {

    WebService static String myFunction(String param1,string param2) {

        if (String.isNotBlank(param1)) {

            try {
            
          Segment2__c Segment= new Segment2__c();
            Segment.Name='Karthik';
            Segment.Event__c= param1;
            Segment.Member_Name__c= param2;
             insert Segment;        
            system.debug('------Segment--------'+Segment.Id);
               return 'OK';    
            }
            catch (Exception e) {
                return 'ERROR';
            }
        }
        else {
            return 'ERROR';
        }
    }
}

Let us know if it helps.
karthik Jonnalagaddakarthik Jonnalagadda
Hi, 

Record is not inserted into log, 
 User-added image

 I didn't understood what was the mistake I done.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Minitored UsersHi karthik Jonnalagadda,

Please add your name  in 'Monitored users'.Then your debug logs will appear.

Let us know if it helps you.

 
karthik Jonnalagaddakarthik Jonnalagadda
Thanks,

Now log is tracking, but Values not inserting into Object 

User-added image
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi karthik Jonnalagadda,

Please see 3rd line from debug statement ,here it says 'Rows 1' ,so record is inserted.

Have you logged in as sys admin profile ? 
Please check your acess on this object. Go to Security Controls and then see sharing settings for your object .

Let us know if it helps you.



 
karthik Jonnalagaddakarthik Jonnalagadda
HI Ashish Sharma,

Sorry to ask you more questions.
Values are not inserting into Object.
User-added image
User-added image

I am the adim to this accouont, I created this account, Now I am using free trial version.
I am very new to Salesforce


Thanks in advance 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

No issue, each of us here have been new to salesforce sometime or other. Keep trying.

Run this query from developer console 'select id,name from Segment2__c' then check if there is any record.
This was selected as the best answer
karthik Jonnalagaddakarthik Jonnalagadda
Thanks I got the result
sudhir kumar kokilasudhir kumar kokila
hi  I am trying to insert records in custom object using this code and errors are displaying as follows
this is my code--------------------------------
public with sharing class DataLoadTestingclass{
public string phone val{get;set;}
  public string country val{get;set;}
  public string city val{get;set;}
  public string name val {get;set;}  
  public pageRefernce doInsert(){
 DataLoadTest__c objdlt = new DataLoadTest__c();
 objdlt.name = nameval;
 objdlt.city__c = cityval;
 objdlt.country__c = countryval;
 objdlt.phone__c = phoneval;
  insert objdlt;
  pageRefernce ref = new pageRefernce('/apex/insertdlttest' );
  ref.set redirect(true);
  return   ref;
 }
 
}
sudhir kumar kokilasudhir kumar kokila
I have following errors
Invalidtype : DataLoadTest__c
varilable :objdlt doesnt exists
DML requires Sobject or Sobject List type: DataLoadTest__c