You need to sign in to do that
Don't have an account?
Yady Kalsi 9
Assign JSON value to lookup fields
Hi Guys !!
I am fetching JSON data stream using REST API. I'm filling my custom fields with the JSON data. But I'm stuck in populating lookup field.
Below is the snippet that does the parsing:
DeviceReading__c obj;
obj = new DeviceReading__c();
while(parser.nextToken() != null) {
obj.Meter_ID__c = parser.getText(); // Meter_ID__c is the lookup field of Customer__c object
}
Please help me
I am fetching JSON data stream using REST API. I'm filling my custom fields with the JSON data. But I'm stuck in populating lookup field.
Below is the snippet that does the parsing:
DeviceReading__c obj;
obj = new DeviceReading__c();
while(parser.nextToken() != null) {
obj.Meter_ID__c = parser.getText(); // Meter_ID__c is the lookup field of Customer__c object
}
Please help me
What is the value at parser.getText() ? I can help you if you can provide the response values at the node.
obj.Meter_ID__c = parser.getText(); /
I believe you should do it with external Id, not with record Id. use the code here
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_nested_object.htm
Also for adding I'm using upsert, so do I need to change it:
while(parser.nextToken() != null) {
if(parser.getCurrentToken() == JSONToken.FIELD_NAME) {
if(fieldName == 'WaterMeterID') {
Customer__c Cobj = new Customer__c(Name = parser.getText()); //Made Change HERE
obj.Device_ID__c = Cobj.Name; //Made Change HERE
Istobj.add(obj);
count++;
}
}
upsert Istobj Id;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, count + ' Records Synced'));
}
So on making the changes as you suggested, I'm getting the following error:
Invalid id: 16032304
Error is in expression '{!syncData}' in component <apex:page> in page testcontroller: Class.TestController.syncData: line 138, column 1
An unexpected error has occurred. Your development organization has been notified.