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
RajashriRajashri 

How to create Sobject and assign value to it

Hi ,

I have created the below class and want to create sOject and assign value to it.

 

Can anyone guide me , why m i getting the compiler error "Invalid Invoice_Starement_c " type?

Below is my code

 

public

withvsharingvclassvObjectprac {

 

publicvstaticvvoidvobj()

{

sObject s =

newInvoice_Statement__c();

Invoice_Statement__c inv =

new Invoice_Statement__c(Description__c='Test Invoice',Status__c='Pending');

System.debug(

'Invoice'+ inv.Description_c);

}

 

}

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

change the code as follows

 

public withvsharingvclassvObjectprac {

 

publicvstaticvvoidvobj()

{

//sObject s =newI nvoice_Statement__c();

Invoice_Statement__c inv = new Invoice_Statement__c();

inv.Description__c = 'Test Invoice';

inv.Status__c = 'Pending';

//If you are inserting or updatingor some thing else

Insert inv;

System.debug('Invoice'+ inv.Description_c); //here not _c it is__c , your are passing a hardcode values why you are writuing the debug.

}

 

}

All Answers

Suresh RaghuramSuresh Raghuram

hi i didnt get one thing do you have object with fields and you are creating record on it or you would like to create a object by using the custom coding.

 

if you are creating a object with in the class which was not existing, then check how to create and process and procedures.

RajashriRajashri

Hi,

Thanks for the reply.

Actually Custom object is already there but it is giving me error msg Invalid type.

 

Suresh RaghuramSuresh Raghuram

change the code as follows

 

public withvsharingvclassvObjectprac {

 

publicvstaticvvoidvobj()

{

//sObject s =newI nvoice_Statement__c();

Invoice_Statement__c inv = new Invoice_Statement__c();

inv.Description__c = 'Test Invoice';

inv.Status__c = 'Pending';

//If you are inserting or updatingor some thing else

Insert inv;

System.debug('Invoice'+ inv.Description_c); //here not _c it is__c , your are passing a hardcode values why you are writuing the debug.

}

 

}

This was selected as the best answer
RajashriRajashri

Thanks a lot!!

How can i see my record got inserted successfully in the database?

Suresh RaghuramSuresh Raghuram

add the invoice object to to the tab then you will have the access to edit and view the records

 

setup -> create -> tabs