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
S.Mac-IntoshS.Mac-Intosh 

Insert Account

Hey i'm trying to clone an account.

There are no errors in my code but he wont insert the copied account.

My second step is to edit some fields so it wont be exactly the same account.

Does someone know what i need to do?

My code is:

 

Public class InsertClass{


Public string InsertAccount{

Get{

Database.DMLOptions dmo = new database.DMLOptions();
dmo.AssignmentRuleHeader.UseDefaultRule= true;

///list<Account> WillBeClonedAccount = new list<Account>();

Account IsClonedAccount = [SELECT Account.Name, Account.Billing_Term__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

Account CopiedAccount = new Account();
CopiedAccount.Name = IsClonedAccount.Name;
CopiedAccount.Phone = '020384732';
CopiedAccount.Billing_Term__c = IsClonedAccount.Billing_Term__c;


///WillBeClonedAccount.add(CopiedAccount);

CopiedAccount.SetOptions(dmo);
insert(CopiedAccount);

/// insert(WIllbeClonedAccount);
return CopiedAccount.name ;
}
}
}

Jerun JoseJerun Jose
Can you tell us what problem you are facing. I can't find any problems in the pseudo code you posted.
S.Mac-IntoshS.Mac-Intosh

Nothing happens.

No errors and No records are created.

S.Mac-IntoshS.Mac-Intosh

Someone who could help me? (Bump)

SamuelDeRyckeSamuelDeRycke
I don't see anything wrong with your code. Though, why not use the sobject.clone() method ?

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm



neophyteneophyte

Please check if all the following conditions are met:

 

1) InsertClass is the controller for a visualforce page

2) InsertAccount is a accessed in the page (for getter to fire)

3)If id of the account to be cloned is passedin the URL while accessing the page.

 

If all the above is checked, then check the debug logs which is created when the page is accessed. Check if any issue in the log.

 

- Anand

S.Mac-IntoshS.Mac-Intosh

Hey Anand,

 

Al conditions are met!

 

Could you explain me where i can find the debug logs?

neophyteneophyte

Go to Administration Setup - > Monitoring -> Debug logs. Add yourself in the mointored users list, then access the visualforce page. A log will be created in your name.

 

- Anand

 

SamuelDeRyckeSamuelDeRycke

in salesforce

 

Setup menu -> search : debug log (It's under monitoring)

 

Add the user you're running the code, you can do this by clicking the new button on the monitored users section.  This will provide 20 logs, if you want more, reset the user monitoring (you'll see this once you've added one).

 

 

 

 

S.Mac-IntoshS.Mac-Intosh

Thnx!
This is what i get! But i cant find any error:

 

26.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:58:17.049 (49221000)|EXECUTION_STARTED
09:58:17.049 (49282000)|CODE_UNIT_STARTED|[EXTERNAL]|066M0000000D1Ks|VF: /apex/advanced_clone
09:58:17.051 (51621000)|CODE_UNIT_STARTED|[EXTERNAL]|01pM00000000ocz|InsertClass <init>
09:58:17.051 (51648000)|SYSTEM_MODE_ENTER|true
09:58:17.052 (52827000)|METHOD_ENTRY|[1]|01pM00000000ocz|InsertClass.InsertClass()
09:58:17.052 (52974000)|METHOD_EXIT|[1]|InsertClass
09:58:17.053 (53051000)|CODE_UNIT_FINISHED|InsertClass <init>
09:58:17.084 (84174000)|CODE_UNIT_STARTED|[EXTERNAL]|01pM00000000ocz|InsertClass get(InsertAccount)
09:58:17.084 (84203000)|SYSTEM_MODE_ENTER|true
09:58:17.084 (84229000)|CODE_UNIT_STARTED|[EXTERNAL]|01pM00000000ocz|InsertAccount
09:58:17.084 (84521000)|SYSTEM_CONSTRUCTOR_ENTRY|[8]|<init>()
09:58:17.084 (84591000)|SYSTEM_CONSTRUCTOR_EXIT|[8]|<init>()
09:58:17.084 (84620000)|SYSTEM_METHOD_ENTRY|[9]|Database.DMLOptions.__sfdc_AssignmentRuleHeader()
09:58:17.084 (84782000)|SYSTEM_METHOD_EXIT|[9]|Database.DMLOptions.__sfdc_AssignmentRuleHeader()
09:58:17.084 (84801000)|SYSTEM_METHOD_ENTRY|[9]|Database.DMLOptions.AssignmentRuleHeader.__sfdc_UseDefaultRule(Boolean)
09:58:17.084 (84841000)|SYSTEM_METHOD_EXIT|[9]|Database.DMLOptions.AssignmentRuleHeader.__sfdc_UseDefaultRule(Boolean)
09:58:17.085 (85089000)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|<init>()
09:58:17.085 (85140000)|SYSTEM_CONSTRUCTOR_EXIT|[11]|<init>()
09:58:17.085 (85204000)|SYSTEM_METHOD_ENTRY|[13]|ApexPages.currentPage()
09:58:17.085 (85332000)|SYSTEM_METHOD_EXIT|[13]|ApexPages.currentPage()
09:58:17.085 (85381000)|SYSTEM_METHOD_ENTRY|[13]|System.PageReference.getParameters()
09:58:17.085 (85472000)|SYSTEM_METHOD_EXIT|[13]|System.PageReference.getParameters()
09:58:17.085 (85516000)|SYSTEM_METHOD_ENTRY|[13]|MAP<String,String>.get(Object)
09:58:17.085 (85537000)|SYSTEM_METHOD_EXIT|[13]|MAP<String,String>.get(Object)
09:58:17.090 (90883000)|SOQL_EXECUTE_BEGIN|[13]|Aggregations:0|select Account.Name, Account.Billing_Term__c from Account where Id = :tmpVar1
09:58:17.108 (108945000)|SOQL_EXECUTE_END|[13]|Rows:1
09:58:17.109 (109500000)|SYSTEM_METHOD_ENTRY|[21]|LIST<Account>.add(Object)
09:58:17.109 (109559000)|SYSTEM_METHOD_EXIT|[21]|LIST<Account>.add(Object)
09:58:17.109 (109586000)|SYSTEM_METHOD_ENTRY|[22]|SObject.setOptions(APEX_OBJECT)
09:58:17.109 (109926000)|SYSTEM_METHOD_EXIT|[22]|SObject.setOptions(APEX_OBJECT)
09:58:17.109 (109987000)|DML_BEGIN|[24]|Op:Insert|Type:Account|Rows:1
09:58:17.899 (899970000)|ENTERING_MANAGED_PKG|PBSI
09:58:17.901 (901053000)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id, PBSI__Property_Name__c, PBSI__Property_Value__c from PBSI__Property__c where PBSI__Property_Name__c = 'Assets_Tracking'
09:58:17.915 (915636000)|SOQL_EXECUTE_END|[8]|Rows:0
09:58:17.921 (921825000)|DML_END|[24]
09:58:17.921 (921889000)|CODE_UNIT_FINISHED|InsertAccount
09:58:17.921 (921908000)|CODE_UNIT_FINISHED|InsertClass get(InsertAccount)
09:58:18.085 (928207000)|CUMULATIVE_LIMIT_USAGE
09:58:18.085|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of script statements: 13 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:58:18.085|LIMIT_USAGE_FOR_NS|PBSI|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 5 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:58:18.085|CUMULATIVE_LIMIT_USAGE_END

09:58:17.928 (928272000)|CODE_UNIT_FINISHED|VF: /apex/advanced_clone
09:58:17.928 (928284000)|EXECUTION_FINISHED



neophyteneophyte

That is because there is no error. Congrats !! :)  Your code works fine and the record is inserted. Please go to accounts tab, get the latest account which was inserted from the list view (sort by created date in header), you will find the record which got inserted.

 

-Anand

SamuelDeRyckeSamuelDeRycke

Had you by any chance been looking at the recent accounts, and not the all accounts  view ? 

 

 

 

S.Mac-IntoshS.Mac-Intosh

The account doesnt exist.

 

Maybe there is some trigger which deletes the record or some field that needs to be filled in.

 

neophyteneophyte

Add a debug statement System.debug(CopiedAccount.id);

 

This will give you the id of the record inserted. Take the id and copy paste in the browser url: https://instance(eg: cs13).salesforce.com/copyidhere. If the record exists it should be loaded.

 

One thing is certain the record is getting inserted. 

 

-Anand

S.Mac-IntoshS.Mac-Intosh

Does anyone know how i can copy the account owner, created by and last modified date fields?

If this aint possible does anyone know in which format i need to insert these fields? 

These three fields are lookups to users.

S.Mac-IntoshS.Mac-Intosh

Anand,

 

The records doesn't exist.

The fields owner, createdby and last modified date aren't filled in.

These fields are required. These fields are also a lookup to an user!

I cant query these fields.

Any idea how i can fix this?

 

Gr,

 

Sergio

neophyteneophyte

The field which you mentioned are non-writeable (except for owner) system fields. They are populated by Salesforce on insert/update of the record.

 

Owner - Generally contains the user creating the record [Unless there is an assignment rule to assign it to a different user/quere]

CreatedBy - Contains the user who creates the record [non-writeable]

Last Modified Date - Contains the date in which the record was last updated [non-writeable]

 

S.Mac-IntoshS.Mac-Intosh

So these fields should be filled in automatically when i insert a record?

neophyteneophyte

Thats right.

S.Mac-IntoshS.Mac-Intosh

Oke this is weird.


The account is inserted and an ID is created.

But i cant find the account after this.

It is probably deleted.

 

any idea how this can be possible?

neophyteneophyte

What happens when you do what I asked previously :

 

Add a debug statement System.debug(CopiedAccount.id);

 

This will give you the id of the record inserted. Take the id and copy paste in the browser url: https://instance(eg: cs13).salesforce.com/copyidhere. If the record exists it should be loaded.

 

One thing is certain the record is getting inserted. 

 

Do you get a error?

S.Mac-IntoshS.Mac-Intosh

When i paste the id i get this error:

 

"Data Not AvailableThe data you were trying to access could not be found. It may be due to another user deleting the data or a system error. If you know the data is not deleted but cannot access it, please look at our support page."

neophyteneophyte

Weird. I hope you also tried querying [SOQL] for the record using the id.

 

Also please check in your debug logs if there is any delete statement being called. If nothing works out, I'd say - call Salesforce Support.

S.Mac-IntoshS.Mac-Intosh

Is it possible to see the database logs somewhere?

With everything the database has done? Maybe i can see there why the records are deleted or not inserterd.

 

neophyteneophyte

Debug logs are the only logs I know of. You can check for deleted records in Recycle Bin.

S.Mac-IntoshS.Mac-Intosh

Where can i find the recycle bin?

neophyteneophyte

Below the Sidebar (extreme left on the page) in the Home page.

S.Mac-IntoshS.Mac-Intosh

Thnx for all the help!

But i'm still unable to fix this problem.

 

Ill contact SF one of these days.

Ill keep you all posted!

 

Gr,

Sergio

Jerun JoseJerun Jose

A few things that I wanted to point out.

 

Does anyone know how i can copy the account owner, created by and last modified date fields?

If this aint possible does anyone know in which format i need to insert these fields? 

These three fields are lookups to users.

 


The account owner is a lookup to user which gets autopopulated as the current user if you do not explicitly specify a value.

To explicitly specify a value, you will need to provide the ID of the user you want to assign as the account owner.

 

The created and last modified information work a little differently when accessed using the API and the UI. When looking at the UI, the created by field shows you the user as well as the created date. A similar tale for Last modified info. However, when working through the API the user and date parts of the created information exist as two seperate fields - CreatedByID and CreatedDate. The CreatedByID will contain the ID of the user who created it and the CreatedDate will be a DateTime value of when the record was created. Again, its a similar case with Last modified info with the fields LastModifiedByID and LastModifiedDate.

It is correct that the created and last modified info are system fields that cannot be explicitly updated. However, if you are doing a data migration, then you can contact SFDC support to allow you to specify a value for created and last modified dates during record insert.

 

As for the record not showing up part.

It is technically not possible to delete a record as soon as it was inserted. To do something like that would need usage of advanced apex programming. What I presume might have happened was that after the account was created, the transaction met with some exception and so the system rolled back the whole insert process. As part of the roll back that newly inserted account would have been deleted.

To check what is actually happening, you will need to monitor the transaction using the debug logs.

When you do whatever you do to create an account, enable debug logs by Setup->Admin Setup->Monitoring->Debug Logs. Add your name to the debug logs section and then fire the transaction. Once the transaction is completed, refresh the debug logs screen and open up the log for this transaction. There you can see exactly what happened and find out the root of the problem.

S.Mac-IntoshS.Mac-Intosh

I posted the debug logs already a few days ago.

You can see them at page one.

Could you have a look at them and tell me if you can see something wrong at them?

 

 

Jerun JoseJerun Jose

Thanks. I do not see anything untoward in the debug logsEspecially, this section of the debug log confirms the insert.

 

09:58:17.109 (109987000)|DML_BEGIN|[24]|Op:Insert|Type:Account|Rows:1
09:58:17.899 (899970000)|ENTERING_MANAGED_PKG|PBSI
09:58:17.901 (901053000)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id, PBSI__Property_Name__c, PBSI__Property_Value__c from PBSI__Property__c where PBSI__Property_Name__c = 'Assets_Tracking'
09:58:17.915 (915636000)|SOQL_EXECUTE_END|[8]|Rows:0
09:58:17.921 (921825000)|DML_END|[24]

Also, this section of the debug log confirms that only the account insert happened and only 1 account was inserted. I also do not see any future calls being made which should confirm that the record was inserted properly.

 

Number of DML statements: 1 out of 150
Number of DML rows: 1 out of 10000
Number of future calls: 0 out of 10  

I do not see anything that can cause problems with the record insert.

S.Mac-IntoshS.Mac-Intosh

I'm still not able to fix this!

 

Is there someone who got a working sample code which works for me?

 

It would of help me much!