+ Start a Discussion
Felix Jong Seok ChaeFelix Jong Seok Chae 

AccountID and AccountName in REST

I am trying to insert a new Case record in salesforce, so I created the REST in Java for that.
In both the APEX developer console and a inserting method in REST I use the field AccountID, but
in Salesforce website I have to choose string-typed name of a company for the field Account Name.
I also checked all possible fields for the case object in developer console and I didn't see AccountName field. However, when I used a specific ID (picked a random one from developer console) for AccountID in REST, the newly created case record acquired the Account Name associated with that Account ID. Is there a way to use Account Name direclty from REST instead of using AccountID?
Mohit Sharma 37Mohit Sharma 37
Hi Felix Jong Seok Chae,
 
You have no need to sent the Account.Name from your REST API. It is Salesforce Standard, if there is a lookup field on any object, then you need to set record id in it and it will show you the Name of that record. It is Salesforce Internal process.

Account is an Lookup field with AccountId Api name on Case. So only set account id. For more detail you need to study about Salesforce fields and object.

Please mark it best answer if you are satisfied.

Have a nice day!!
 
Felix Jong Seok ChaeFelix Jong Seok Chae
Thank you Mohit. Then how do I connect each accountId with corresponding account lookup field?
For example, let's say I would like to add account field like Paypal. How should I choose the accountID associated with this account field?
Mohit Sharma 37Mohit Sharma 37
Ohk,

You have 2 options here:

1) You need to create a custom field on Case with name Account Name (Account_Name__c will be the api name). Use this api name to put the Account Name. Now à Need to create a trigger in Salesforce which will search Account Id on behalf of Account Name automatically and add that id in AccountId lookup field.

2) Query on Account object first get the Id on behalf of Name and now use this Id in your callout like this.
Account objAcc = [Select Id, Name from Account Where Name = ‘Paypal’];
Now Use
objCase.AccountId = objAcc.Id;
Felix Jong Seok ChaeFelix Jong Seok Chae
If I choose the first option, can a custom field be directly related to account.name needed when I create a new record? If yes, could you give me more details on how to connect the custom field to account.name?
Mohit Sharma 37Mohit Sharma 37
If you are choosing the first one you need to write simple in your REST..

LIKE:
Case objCase = new Case();
objCase.Account_Name__c = 'Paypal';
// or add more code as per your requirement
insert objCase;

Now in Salesforce your Case is going to insert, so you need to write a trigger on Case object before insert event
 
trigger trgCase on Case (before insert) {

//Fetching Account id on behalf on Account Name.
List<Account> lstAcc = new List<Account>([Select Id, Name from Account WHERE Name = Trigger.New[0].Account_Name__c Limit 1);
System.debug('--- lstAcc: ' + lstAcc);

//Setting AccouId in Case object.
if(lstAcc != null && lstAcc.size() > 0)
Trigger.New[0].AccountId = lstAcc[0].Id

}

Hope it will help :)

Have a nice day!!