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
DhairyaDhairya 

Apex method with update

 

Hi,
    {!addMore} is a coomand button action (VF) and blow code is a part of exdtension. I am getting current record  in variable named budget1 of type Budget__C. 
    I would like to calculate total budget value at project level on field test1__c on Project__C object.
    Question is creation of budget record working fine but budger related project field (test1__c) is not populated.
//Budget Record Save
     public PageReference addMore()
     {
                       
          
          database.Insert(budget1);
         if(budget1.value__c!=Null && budget1.PMO_Project__r.test1__c!=null )
           { 
               budget1.PMO_Project__r.test1__c = 'test';
              
              projects__c project = [select 
              
                                     name from projects__c where projects__c.id=:budget1.PMO_Project__r.Id] ;
                
               
               
              update(project) 
                          
           }       
          
          ApexPages.StandardController budgetController = new ApexPages.StandardController(budget1);
          return budgetController.save();
          //return page.budgetDetail;
     }

 

 

Appreciate Your Help,

 

Dhairya

Prafull G.Prafull G.

Please try below one

 

//Budget Record Save
public PageReference addMore() {
    database.Insert(budget1);
    if(budget1.value__c!=Null && budget1.PMO_Project__r.test1__c!=null ) {
        //budget1.PMO_Project__r.test1__c = 'test';
        projects__c project = [select name from projects__c where projects__c.id=:budget1.PMO_Project__r.Id] ;
        project.test1__c = 'test';
        update project;
    }
    ApexPages.StandardController budgetController = new ApexPages.StandardController(budget1);
    return budgetController.save();
    //return page.budgetDetail;
}

 

Let me know if you are still facing any issues.

 


DhairyaDhairya

Hey Crmtech21,

 

        Thanks for your prompt reply but its not updating test1__C (Text) value on project__C obj.

 

         Do u have any other idea?

 

thanks,

dhairya

Prafull G.Prafull G.

I believe the if clause is not satisfied.

if(budget1.value__c!=Null && budget1.PMO_Project__r.test1__c!=null )

 

Please check if the both of the above conditions are true. I think the second budget1.PMO_Project__r.test1__c is not satisfied and the code within the if statement is not executing.

 

with best,

hgarghgarg
I guess, when you are inserting "Budget1", you are expecting that you will get the field "PMO_Project__r.test1__c" populated automatically. But this assumption is wrong. If It is right, then I suggest that just before your if condition query the Budget1 including the "PMO_Project__r.test1__c" field. like following: budget1 = [select value__c, PMO_Project__r.test1__c from budget where id=:budget1.id];
DhairyaDhairya

Thanks for your reply.

 

I will try that.

 

Dhairya