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
Balajee SelvarajBalajee Selvaraj 

Database.update()

I have two methods in apex class called A and B.
A method have custom custom object called custom_obj.
B method hvae Standartd object called account. 
If Account have dml operations dabase.update or dabase.delete, It should capture error message in exception and this error message should update in salesfoce custom_obj which is in method A. 

Please answer me Freinds.
rajat Maheshwari 6rajat Maheshwari 6

Hi Balajee,

You can try with that code snippet : - 

Database.SaveResult [] updateResult = Database.update(records, false);

for (Database.SaveResult r : updateResult)

{
if (!r.isSuccess())

{
for (Database.Error e : r.getErrors())

{ here you can call those custom object field from A method and assign the value : e.getMessage() }

}

}

Balajee SelvarajBalajee Selvaraj
But where i have to wrote this code snippet!
Balajee SelvarajBalajee Selvaraj
It is not possible in below code?

void A(){
Custom_obj o = new Custom_obj();
0.status__c=''; //error message i have to display
}
void B(){
.............
...........
try{
database.update(listupdate);
datebase.delete(listdelete);
}catch{

}
}
rajat Maheshwari 6rajat Maheshwari 6
Hi Balajee,

public static Custom Object(API Name) A(Id AccountId, String ErrorMessage){
Custom_obj o = new Custom_obj();
o.status__c=ErrorMessage; //error message i have to display
o.Accountid = AccountId;
insert o;
}

public static void B(){
List<Database.SaveResult> updateResults = database.update(listupdate);

for (Database.SaveResult r : updateResults)
{
if (!r.isSuccess())
{
for (Database.Error e : r.getErrors())

{  

Custom Object c = A(r.getId(), e.getMessage()) 
  system.debug('aaaaa'+c);

}



}
}
}


Note : o.Accountid represent lookup field(AccountId) on custom object to associate with account. you can replace AccountId from whatever you are using ur custom lookup field api .

Thanks

     

 
Balajee SelvarajBalajee Selvaraj
Method A is Userdefined method and i want to update status not to insert!
rajat Maheshwari 6rajat Maheshwari 6
Hi Balajee,

Please go with that - 

public static Custom Object(API Name) A(Id AccountId, String ErrorMessage){
Custom Object c = new Custom Object(AccountId = AccountId);
c.status__c=ErrorMessage; //error message i have to display
update c;
}

public static void B(){
List<Database.SaveResult> updateResults = database.update(listupdate);

for (Database.SaveResult r : updateResults)
{
if (!r.isSuccess())
{
for (Database.Error e : r.getErrors())

{  

Custom Object c = A(r.getId(), e.getMessage()) 
  system.debug('aaaaa'+c);

}



}
}
}