6

I have a vf page and its extensions,I am trying to save the multiple lists of a object against a particular recordtype.But i am unable to fetch the record the record type and then assign to the same object.If you have any idea and then please mention it.Thank you.

public with sharing class LevittAmpBudgetUpdatedCtrl {
     public List<Grant_Report__c> grExpenseObjLst{get;set;}
     public List<Grant_Report__c> grCashObjLst{get;set;}//Matching fund,Cash Object List
     public List<Grant_Report__c> grInKindObjLst{get;set;}//Matching fund,inkind Object List
     public Grant_Report__c grObj{get;set;}
     public Grant_Approval__c gaObj{get;set;}
     private ID ids{get;set;}

     public LevittAmpBudgetUpdatedCtrl(ApexPages.StandardController controller) {
         //
        ids= ApexPages.currentPage().getParameters().get('id'); //
        gaObj=new Grant_Approval__c();
        grExpenseObjLst= new list<Grant_Report__c>();        
        grCashObjLst=new List<Grant_Report__c>();
        grInKindObjLst=new List<Grant_Report__c>();

        RecordType rt= [SELECT id FROM RecordType WHERE Name='Matching Fund Cash'];
        System.debug('ssssss'+rt);

         for(AMP_Budget_Expense__c exObj:AMP_Budget_Expense__c.getAll().Values()){
                        grObj=new Grant_Report__c();
                        grObj.Expense_Category_Label__c=exObj.Expense_Category__c;
                        grExpenseObjLst.add(grObj);    
                    }              
          //check if id is not null
       if(ids!= null)
          {

                gaObj = [SELECT id,AMP_Grant_Request_Amount__c,Cash_Match_Total_Amount__c,
                                InKind_Match_Total_Amount__c,Comment__c FROM Grant_Approval__c WHERE Id= :ids];//   
                for(Grant_Report__c gr: [select id,Matching_Cash_Source__c,Matching_Cash_Amount__c,Matching_Cash_Commitment__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report cash records
                     grCashObjLst.add(gr);               
                }
                for(Grant_Report__c gr: [select id,Matching_inKind_Source__c,Matching_inKind_item__c,Matching_inKind_Amount__c, 
                                               Matching_inKind_Commitment__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report IN-Kind records
                     grInKindObjLst.add(gr);                 
                }
                for(Grant_Report__c gr: [select id,Expense_Total_Amount__c,Expense_Cash_Amount__c,Expense_InKind_Anount__c,Expense_Category_Label__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report IN-Kind records
                     grExpenseObjLst.add(gr);                 
                }
             }
         else{   
                //default 5 lineitem will come
                    for(integer i=0; i<5; i++)
                    {
                        grObj = new Grant_Report__c();
                        grCashObjLst.add(grObj);
                        grInKindObjLst.add(grObj); 
                    }

             }

    }

     //add more row of Matching Funds,Cash
     public pageReference addMoreCashFund(){
         grObj = new Grant_Report__c();
                grCashObjLst.add(grObj);
                return null; 
     }
     //add more row of Matching Funds,Cash
     public pageReference addMoreInkindFund(){
         grObj = new Grant_Report__c();
                grInKindObjLst.add(grObj);
                return null; 
     }

     Public Pagereference savedoc(){
        List<Grant_Report__c> enteredCashGrantReport = new List<Grant_Report__c>();
        List<Grant_Report__c> enteredInKindGrantReport = new List<Grant_Report__c>();  
        List<Grant_Report__c> enteredExpenseReport = new List<Grant_Report__c>();       
        for(Grant_Report__c gr: grCashObjLst){
                 if(gr.id==null)
                   {
                      gr.Grant_Approval__c= ids;
                   }
                 enteredCashGrantReport.add(gr);
          }
         for(Grant_Report__c gr: grInKindObjLst){
                 if(gr.id==null)
                   {
                      gr.Grant_Approval__c= ids;
                   }
                 enteredInKindGrantReport.add(gr);
           }
           for(Grant_Report__c gr: grExpenseObjLst){
                 if(gr.id==null)
                   {
                      gr.Grant_Approval__c= ids;
                   }
                 enteredExpenseReport.add(gr);                 
           }
           grExpenseObjLst=null;
          try{ 
               upsert gaObj;
               upsert enteredCashGrantReport; 
               upsert enteredInKindGrantReport; 
               upsert enteredExpenseReport;              
          }catch(System.DMLException e) {
                ApexPages.addMessages(e);
                return null;
           }  
        return NULL;
      }
      public PageReference Back() 
        {
            PageReference startPage=new PageReference('/apex/Network_Calendar');
            startPage.setRedirect(true);
            return  startPage;
        }

}
  • 1
    if my answer is not what you are looking for, please add code to your question so we can see what did you attempted, and why it's not working. – smukov Sep 19 '16 at 10:35
  • I posted my code.please check – Vicky Singh Sep 19 '16 at 11:01
  • So you want to assign the Matching Fund Cash record type to records in which of the lists in your code? – smukov Sep 19 '16 at 11:14
  • I'm assuming that you want to add the Matching Fund Cash record type to the records before upserting them in your savedoc() method. If that's the case, checkout my updated answer. – smukov Sep 19 '16 at 11:33
  • yes..thats right – Vicky Singh Sep 19 '16 at 12:08
13

SUMMER '18 UPDATE

You can now use the getRecordTypeInfosByDeveloperName() to get a Record Type Id by Developer Name without spending a SOQL query. This should be the preferred approach going forward instead of using the old getRecordTypeInfosByName() method as suggested in the original answer.

Use this approach instead of the old suggestion below, unless you are really trying to get the Record Type Id by RECORD_TYPE_NAME (Label). The rest of the answer still applies:

Id recordTypeId = Schema.SObjectType.OBJECT_NAME.getRecordTypeInfosByDeveloperName()
                      .get('RECORD_TYPE_DEVELOPER_NAME').getRecordTypeId();

You can obtain a Record Type in code in the following way:

Id recordTypeId = Schema.SObjectType.OBJECT_NAME.getRecordTypeInfosByName()
                  .get('RECORD_TYPE_NAME').getRecordTypeId();

Just replace the OBJECT_NAME with your object (e.g - Account), and the RECORD_TYPE_NAME with the record type name for that object.

In order to assign this Record Type to a specific record you can use the RecordTypeId field. Here's an example for Account:

Account acc = new Account(Name='Test Account', RecordTypeId = recordTypeId);

or through a property like this:

acc.RecordTypeId = recordTypeId;

UPDATE

In your case you can obtain the recordTypeId like this:

Id recordTypeId = Schema.SObjectType.Grant_Report__c.getRecordTypeInfosByName()
                  .get('Matching Fund Cash').getRecordTypeId();

And then just assign this recordTypeId to RecordTypeId field of the records in your FOR loops that you need to update. I'm assuming that's in the saveDoc() method:

 Public Pagereference savedoc(){
    Id recordTypeId = Schema.SObjectType.Grant_Report__c.getRecordTypeInfosByName()
                  .get('Matching Fund Cash').getRecordTypeId();

    List<Grant_Report__c> allGrantReports = new List<Grant_Report__c>();

    for(Grant_Report__c gr: grCashObjLst){
             if(gr.id==null)
             {
                  gr.Grant_Approval__c= ids;                   
             }
             gr.RecordTypeId = recordTypeId;//assign record type
             allGrantReports.add(gr);
     }
     for(Grant_Report__c gr: grInKindObjLst){
             if(gr.id==null)
             {
                gr.Grant_Approval__c= ids;
             }
             gr.RecordTypeId = recordTypeId;//assign record type
             allGrantReports.add(gr);
     }
     for(Grant_Report__c gr: grExpenseObjLst){
             if(gr.id==null)
             {
                gr.Grant_Approval__c= ids;
             }
             gr.RecordTypeId = recordTypeId;//assign record type
             allGrantReports.add(gr);                 
     }
     grExpenseObjLst=null;
     try{ 
           upsert gaObj;          
           //saved you 2 DML statements here by joining 3 lists to 1
           upsert allGrantReports ;          
     }catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
       }  
    return NULL;
  }

UPDATE 2

NOTE: No need to use this approach after Summer '18 update

In case you want to get Record Type by developer name, you'll have to use a DML statement:

Id recordTypeId = [select Id from RecordType where DeveloperName = 'Matching_Fund_Cash' AND sObjectType = 'Grant_Report__c' limit 1].Id;
  • Can you add the assign part i.e. setting the RecordTypeId to the answer? – Keith C Sep 19 '16 at 10:41
  • 1
    @KeithC Better? :) – smukov Sep 19 '16 at 10:48
  • 1
    Yep - +1 from me. – Keith C Sep 19 '16 at 11:27
  • Now,I want to get recordType by DeveloperName instead of RecordTypeInfosByName because recordTypeByName is not for all future cases.Is there any method to get developerName?? – Vicky Singh Sep 19 '16 at 12:17
  • @VickySingh see my Update 2 for DeveloperName record type. – smukov Sep 19 '16 at 12:47
0
public with sharing class LevittAmpBudgetUpdatedCtrl {
 public List<Grant_Report__c> grExpenseObjLst{get;set;}
 public List<Grant_Report__c> grCashObjLst{get;set;}//Matching fund,Cash Object List
 public List<Grant_Report__c> grInKindObjLst{get;set;}//Matching fund,inkind Object List
 public Grant_Report__c grObj{get;set;}
 public Grant_Approval__c gaObj{get;set;}
 private ID ids{get;set;}

 public LevittAmpBudgetUpdatedCtrl(ApexPages.StandardController controller) {
     //
    ids= ApexPages.currentPage().getParameters().get('id'); //
    gaObj=new Grant_Approval__c();
    grExpenseObjLst= new list<Grant_Report__c>();        
    grCashObjLst=new List<Grant_Report__c>();
    grInKindObjLst=new List<Grant_Report__c>();

    RecordType rt= [SELECT id FROM RecordType WHERE Name='Matching Fund Cash'];
    System.debug('ssssss'+rt);

     for(AMP_Budget_Expense__c exObj:AMP_Budget_Expense__c.getAll().Values()){
                    grObj=new Grant_Report__c();
                    grObj.Expense_Category_Label__c=exObj.Expense_Category__c;
                    grExpenseObjLst.add(grObj);    
                }              
      //check if id is not null
   if(ids!= null)
      {

            gaObj = [SELECT id,AMP_Grant_Request_Amount__c,Cash_Match_Total_Amount__c,
                            InKind_Match_Total_Amount__c,Comment__c FROM Grant_Approval__c WHERE Id= :ids];//   
            for(Grant_Report__c gr: [select id,Matching_Cash_Source__c,Matching_Cash_Amount__c,Matching_Cash_Commitment__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report cash records
                 grCashObjLst.add(gr);               
            }
            for(Grant_Report__c gr: [select id,Matching_inKind_Source__c,Matching_inKind_item__c,Matching_inKind_Amount__c, 
                                           Matching_inKind_Commitment__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report IN-Kind records
                 grInKindObjLst.add(gr);                 
            }
            for(Grant_Report__c gr: [select id,Expense_Total_Amount__c,Expense_Cash_Amount__c,Expense_InKind_Anount__c,Expense_Category_Label__c from Grant_Report__c where Grant_Approval__c=:ids ]) {//get all related grant report IN-Kind records
                 grExpenseObjLst.add(gr);                 
            }
         }
     else{   
            //default 5 lineitem will come
                for(integer i=0; i<5; i++)
                {
                    grObj = new Grant_Report__c();
                    grCashObjLst.add(grObj);
                    grInKindObjLst.add(grObj); 
                }

         }

}

 //add more row of Matching Funds,Cash
 public pageReference addMoreCashFund(){
     grObj = new Grant_Report__c();
            grCashObjLst.add(grObj);
            return null; 
 }
 //add more row of Matching Funds,Cash
 public pageReference addMoreInkindFund(){
     grObj = new Grant_Report__c();
            grInKindObjLst.add(grObj);
            return null; 
 }

 Public Pagereference savedoc(){
    List<Grant_Report__c> enteredCashGrantReport = new List<Grant_Report__c>();
    List<Grant_Report__c> enteredInKindGrantReport = new List<Grant_Report__c>();  
    List<Grant_Report__c> enteredExpenseReport = new List<Grant_Report__c>();       
    for(Grant_Report__c gr: grCashObjLst){
             if(gr.id==null)
               {
                  gr.Grant_Approval__c= ids;
               }
             enteredCashGrantReport.add(gr);
      }
     for(Grant_Report__c gr: grInKindObjLst){
             if(gr.id==null)
               {
                  gr.Grant_Approval__c= ids;
               }
             enteredInKindGrantReport.add(gr);
       }
       for(Grant_Report__c gr: grExpenseObjLst){
             if(gr.id==null)
               {
                  gr.Grant_Approval__c= ids;
               }
             enteredExpenseReport.add(gr);                 
       }
       grExpenseObjLst=null;
      try{ 
           upsert gaObj;
           upsert enteredCashGrantReport; 
           upsert enteredInKindGrantReport; 
           upsert enteredExpenseReport;              
      }catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
       }  
    return NULL;
  }
  public PageReference Back() 
    {
        PageReference startPage=new PageReference('/apex/Network_Calendar');
        startPage.setRedirect(true);
        return  startPage;
    }

}

  • is it an answer or question ? – Ratan Paul Sep 19 '16 at 11:29
  • Thats my code..its not the answer. – Vicky Singh Sep 19 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.