kindly have a look of below mentioned code and the Json Response. There are 3 fields namely Parent, lastmodifiedby and createdby.
In Parent, I don't want Id attribute only last Parent folder, similarily in Lastmodifiedby and createdby, I don't want earlier Id attributes instead last folder attributes with Id and names only. kindly suggest needful.
Apex Class :
global with sharing class API_Account {
@HttpGet
global static responseWrapper getrecordsbyId() {
RestRequest request = RestContext.request;
// grab the SubscriptionId from the end of the URL
String AccountId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Account result = [SELECT name, site, billingAddress, (select id, name from ChildAccounts)CA, createdby.name, description,
lastmodifiedbyId, ParentId, Parent.Name, phone, ShippingAddress, Website from Account
WHERE Id = :AccountId];
string cityadrs;
string streetadrs;
string postalcodeadrs;
string countryadrs;
Address adrs = result.BillingAddress;
if(result.BillingAddress !=null){
cityadrs = adrs.city;
streetadrs = adrs.street;
postalcodeadrs = adrs.postalcode;
countryadrs = adrs.country;
}
string cityadr;
string streetadr;
string postalcodeadr;
string countryadr;
Address adr = result.ShippingAddress;
if(result.ShippingAddress!=null){
cityadr = adr.city;
streetadr = adr.street;
postalcodeadr = adr.postalcode;
countryadr = adr.country;
}
responseWrapper rw = new responseWrapper();
rw.name = result.name;
rw.site = result.site;
if(result.BillingAddress!=null){
rw.billingAddress = streetAdrs+' '+postalcodeadrs+' '+cityadrs+' '+countryadrs;
}
else {rw.billingAddress = null;}
//rw.billingAddress = result.BillingAddress;
rw.childAccount = [select id, name from Account where ParentId =: AccountId];
rw.createdby = [select createdby.name from Account where Id =: AccountId];
rw.description = result.Description;
rw.lastmodifiedby = [select lastmodifiedby.name from Account where Id =: AccountId];
if(result.ParentId!=null){
rw.Parent = [select parentId, Parent.name from Account where Id =: AccountId];
}
else {rw.Parent = null;}
rw.phone = result.phone;
if(result.ShippingAddress !=null){
rw.shippingAddress = streetAdr+' '+postalcodeadr+' '+cityadr+' '+countryadr;
}
else{rw.shippingAddress = null;}
rw.website = result.Website;
return rw;
}
Global Class responseWrapper {
Public string name {get;set;}
Public string site {get;set;}
Public string billingAddress {get;set;}
Public List<Account> ChildAccount {get;set;}
Public List<Account> Createdby {get;set;}
Public string description {get;set;}
Public List<Account> lastmodifiedby {get;set;}
Public List<Account> Parent {get;set;}
Public string phone {get;set;}
Public string shippingAddress {get;set;}
Public string website {get;set;}
public responsewrapper(){
this.name = '';
this.site = '';
this.billingAddress ='';
childAccount = [select id, name from Account limit : 10];
Createdby = [select createdby.name from Account limit : 10];
this.description = '';
lastmodifiedby = [select lastmodifiedbyId, lastmodifiedby.name from Account limit : 10];
Parent = [select id, name from Account limit : 10];
this.phone = '';
this.shippingAddress = '';
this.website = '';
}
}
}
Json Response :
website: null
site: null
shippingAddress: null
phone: null
Parent
0012p00002J83EzAAJ
attributes
ParentId: 0015700002Fcc73AAB
Id: 0012p00002J83EzAAJ
Parent
attributes
Id: 0015700002Fcc73AAB
Name: XXXXXXXXXXXXX
name: XXXXXXXX.com
lastmodifiedby
0012p00002J83EzAAJ
attributes
LastModifiedById: 005D0000003xVPZIA2
Id: 0012p00002J83EzAAJ
LastModifiedBy
attributes
Id: 005D0000003xVPZIA2
Name: XXXXXXXXX
description: Mit mehr als 100 .
Createdby
0012p00002J83EzAAJ
attributes
CreatedById: 00557000007pFp0AAE
Id: 0012p00002J83EzAAJ
CreatedBy
attributes
Id: 00557000007pFp0AAE
Name: XXXXXXXXX
ChildAccount
billingAddress: null
I have put the desired attributes in bold letters. I shall be grateful to you for your kind cooperation.
2 réponses
Hi Eliott and Ankush,
i am extremly grateful to you for your precious time and contribution in it. I am not sure, if both of you got it right. I resolved it with the help of mix guidance from both of you. for an example.
List<string>createdby1 = new list<string>();
string createdbyname;
string createdbyId;
{
createdbyname = result.createdby.name;
createdbyId = result.CreatedById;
createdby1.add(createdbyId);
createdby1.add(createdbyname);
}
and later on
rw.createdby = createdby1; (it gives me both id and name in 1 object).
kindly suggest, if anyone of you were thinking in the same way.