You need to sign in to do that
Don't have an account?
DaveSt
SObject field expression for put method
Hi,
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
Global class RefreshScheduleMirror implements Schedulable { global void execute(SchedulableContext SC) { RefreshSchedule(); } public void RefreshSchedule() { /// Delete the old mirror records /// ScheduleMirror__c[] doomedMirrors = [SELECT Name FROM ScheduleMirror__c]; try { delete doomedMirrors; } catch (DmlException e) { // Process exception here } /// use generic object to pull combined line item and schedule records SObject[] allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue,Quantity, Type, Description, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId, OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode FROM OpportunityLineItemSchedule]; SObject IsoCode = getSObject('CurrencyIsoCode'); /// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror ) List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>(); /// for (OpportunityLineItem Schedule myschedule : allSchedules) { for (SObject myschedule : allSchedules) { // create variable 'mymirror' to hold the record values ScheduleMirror__c mymirror = new ScheduleMirror__c(); mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode); mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name'); //mymirror.LineItemID__c = string.valueOf(myschedule.get('OpportunityLineItemId')); //mymirror.Opportunity__c = string.valueOf(myschedule.get('OpportunityId')); //mymirror.Schedule_Amount__c = string.valueOf(myschedule.get('Revenue')); //mymirror.Schedule_Date__c = string.valueOf(myschedule.get('ScheduleDate')); //mymirror.Schedule_Description__c = string.valueOf(myschedule.get('Description')); //mymirror.Schedule_Quantity__c = string.valueOf(myschedule.get('Quantity')); //mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Type')); //mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Product2Id')); //mymirror.Product_Code__c = string.valueOf(myschedule.get('ProductCode')); //mymirror.Name = string.valueOf(myschedule.get('Name')); // Update the list with all the records retrieved in the for loop newmirrors.add(mymirror); } // update database outwith the for loop!!!! upsert newmirrors; } }
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode); mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name');
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave
May i know why you are using a Sobject[] to get records from OpportunityLineItemSchedule ?
Try modifying your query as
List<OpportunityLineItemSchedule> allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue,Quantity, Type, Description, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId,OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode FROM OpportunityLineItemSchedule];
and then try to iterate over this list like:
Let me know if this helps!
Thanks,
Apoorv
All Answers
You need to do something like this :
Please mark this question as Solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv
I initially tried something like you suggested but when i have: i get an error
"Field expression not allowed for generic SObject" and that is why i went down the route of using the "get" method
Thanks
Dave
May i know why you are using a Sobject[] to get records from OpportunityLineItemSchedule ?
Try modifying your query as
List<OpportunityLineItemSchedule> allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue,Quantity, Type, Description, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId,OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode FROM OpportunityLineItemSchedule];
and then try to iterate over this list like:
Let me know if this helps!
Thanks,
Apoorv