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; } }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 oldgetRecordTypeInfosByName()
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;
Method 2
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; }
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0