External Id plays very important role if you want to update records without knowing the record Ids or want to relate the child record with parent record without knowing the parent record Id.
As a best practice, you should always make External Id unique. If you are performing upsert with External Id, then following situations will occur:
I am going to cover 2 different aspect of using external Id in apex.
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
REST API TUTORIAL FOR SALESFORCE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE
As a best practice, you should always make External Id unique. If you are performing upsert with External Id, then following situations will occur:
- If no record is found in table with provided External Id, then it will create record in table.
- If 1 record is found in table with provided External Id, then it will update record in table.
- If more than 1 records is found in table with provided External Id, then system will throw an error.
I am going to cover 2 different aspect of using external Id in apex.
- Updating a record with External Id
Create a External Id field on Account as Account_Unique_Number__c and mark it as External Id and unique while creating it.Now we will create a new record using upsert.
Execute below command in developer console
List<Account> acclist=new list<Account>();
acc.name='Demo test1';
acc.Account_Unique_Number__c='00001';
acclist.add(acc);
Schema.SObjectField ftoken = Account.Fields.Account_Unique_Number__c;
Database.UpsertResult[] srList = Database.upsert(acclist,ftoken,false);
for (Database.UpsertResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());
System.debug('fields that affected this error: ' + err.getFields());
}
}
}
As there is no record in Account with Account_Unique_Number__c as 00001, system will create a new record.
Now again we will run same script in developer console and will specify some more field values:
List<Account> acclist=new list<Account>();
Account acc=new Account();
acc.name='Demo test1';
acc.Account_Unique_Number__c='00001';
acc.type='Other';
acc.Industry='Banking';
acclist.add(acc);
Schema.SObjectField ftoken = Account.Fields.Account_Unique_Number__c;
Database.UpsertResult[] srList = Database.upsert(acclist,ftoken,false);
for (Database.UpsertResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());
System.debug('fields that affected this error: ' + err.getFields());
}
}
}
Now you will see that system will update the record as it was able to find a Account record with Account_Unique_Number__c as 00001
- Relating a child record with parent record by using parent record Id
In order to understand this, we will create contact record and will relate to account using Account_Unique_Number__c. Execute below code in developer console:
List<Contact> conlist=new list<Contact>();
Contact con=new Contact();
con.lastname='Kumar';
con.Firstname='Kumar';
con.email='sunil02kumar@gmail.com';
Account acc=new Account(Account_Unique_Number__c='00001');
con.Account=acc;
conlist.add(con);
Database.UpsertResult[] srList = Database.upsert(conlist,false);
for (Database.UpsertResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());
System.debug('fields that affected this error: ' + err.getFields());
}
}
}
This will create a new contact for Account which have Account_Unique_Number__c as 00001.
In above code snippet, you can see that in order to relate contact with account, we are not specifying the account 15 or 18 digit record id. We are just specifying the external Id of account and system will maintain the relationship.
If you refer custom object as parent object then refer it with __r. For example in above scenario, if i have to relate contact with custom object say Parent_Obj__c, then I will use below code:
If you refer custom object as parent object then refer it with __r. For example in above scenario, if i have to relate contact with custom object say Parent_Obj__c, then I will use below code:
Parent_Obj__c obj = new Parent_Obj__c(Unique_Number__c='00001');
con.Parent_Obj__r = obj;
Why it is recommended to mark External Id as unique?
Imagine you are creating a contact and specified External Id of parent. Suppose there are 2 records in account table with same value, then system will not able to identify with whom it needs to relate the contact and will throw error saying more than 1 match found.
Same is applicable when you update the record with External Id.
Same is applicable when you update the record with External Id.
More Blogs>>:
DYNAMIC APEX IN SALESFORCE
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
REST API TUTORIAL FOR SALESFORCE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE
AngularJS is a toolset for building the framework most suited to your application development. It is fully extensible and works well with other libraries. Every feature can be modified or replaced to suit your unique development workflow and feature needs. Read on to find out how.
ReplyDeleteAngularJS Training in Chennai
DeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Some phone systems distribute the database across multiple servers. ShoreTel, for example, distributes components of the database to application servers and distributed voice mail servers that characterize the single image architecture in a multi-site environment. mysql dashboards
ReplyDeleteThose guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeleteDigital marketing in chennai
I like the blog format as you create user engagement in the complete article. Thanks for the informative posts.
ReplyDeleteSelenium Training in Chennai
Selenium Course in Chennai
iOS Training
iOS Course in Chennai
Digital Marketing Course in Chennai
Digital Marketing Training in Chennai
I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
ReplyDeleteFor more details: ReactJs training in Velachery | ReactJs training in chennai
I am getting the error as VARIABLE_ASSIGNMENT [7]|ftoken|"common.apex.runtime.impl.ApexFieldToken@3db6c477"|0x3db6c477 when updating a record with external id.Could you please guide me how to resolve this?
ReplyDelete
ReplyDeleteThis is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to article very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
DedicatedHosting4u.com
Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information.
ReplyDeleteThanks
Cpa offers
Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
ReplyDeletewhat is seo
types of seo
ReplyDeleteThis is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
Android Application development
Web application
Excellent Blog. Thank you so much for sharing.
ReplyDeletebest react js training in chennai
react js training in Chennai
react js workshop in Chennai
react js courses in Chennai
react js tutorial
reactjs training Chennai
react js online training
react js training course content
react js online training india
react js training courses
react js training topics
react js course syllabus
react js course content
react js training institute in Chennai
Thank you for this informative blog
ReplyDeleteTop 5 Data science training in chennai
Data science training in chennai
Data science training in velachery
Data science training in OMR
Best Data science training in chennai
Data science training course content
Data science certification in chennai
Data science courses in chennai
Data science training institute in chennai
Data science online course
Data science with python training in chennai
Data science with R training in chennai
Flying Shift - Packers & Movers in Bhopal
ReplyDeleteNice blog was really feeling good to read it. Thanks for this information.
ReplyDeleteSpoken English Classes in Chennai
English Speaking Course in Chennai
french classes
pearson vue test center in chennai
IoT Training in Chennai
Xamarin Training in Chennai
Node JS Training in Chennai
Spoken English Classes in OMR
Spoken English Classes in Porur
Thanks for sharing valuable information.
ReplyDeletedigital marketing training
digital marketing in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification
digital marketing course training in velachery
digital marketing training and placement
digital marketing courses with placement
digital marketing course with job placement
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
Great blog thanks for sharing Looking for the best creative agency to fuel new brand ideas? Adhuntt Media is not just a digital marketing company in chennai. We specialize in revamping your brand identity to drive in best traffic that converts. Buckle up for a ride that is going to be filled with SEO, Social media marketing, unique Graphic & Logo design and efficient ads strike the perfect chord! Join the Adhuntt Media adventure right at Adhuntt Media.
ReplyDeletedigital marketing company in chennai
seo service in chennai
web designing company in chennai
social media marketing company in chennai
Nice blog thanks for sharing Is this a special day for you? Beautiful and fragrant flowers are sure to make it even more amazing of a day no doubt. This is why Karuna Nursery Gardens offers you the best rental plants in Chennai that too at drop dead prices.
ReplyDeleteplant nursery in chennai
rental plants in chennai
corporate gardening service in chennai
Excellent blog thanks for sharing Buy the best beauty parlour products wholesale in Chennai at Pixies Beauty Shop. Thousands of global top-tier brands to choose from and friendly faces all over, we would love to make your Salon journey, one the world recognizes.
ReplyDeletebeauty Shop in Chennai
keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
ReplyDeletedigital marketing training in bangalore / https://www.excelr.com/digital-marketing-training-in-bangalore
Very useful blog thanks for sharing IndPac India the German technology Packaging and sealing machines in India is the leading manufacturer and exporter of Packing Machines in India.
ReplyDeleteNice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
ReplyDeletedigital marketing course in chennai
SKARTEC Digital Marketing
best digital marketing training in chennai
seo training in chennai
online digital marketing training
best marketing books
best marketing books for beginners
best marketing books for entrepreneurs
best marketing books in india
digital marketing course fees
best seo service in chennai
SKARTEC SEO Services
digital marketing resources
digital marketing blog
digital marketing expert
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
ReplyDeletehttps://360digitmg.com/digital-marketing-training-in-hyderabad
nice blog
ReplyDeletegreat information.
Makeup class
is an integral part of Cosmetology and is a blooming industry in Beauty and Wellness Industry. At VLCC Institute, aspiring Cosmetologist can enroll into different Aesthetic Courses as per their requirements.
Very nice job... Thanks for sharing this amazing and educative blog post! ExcelR Digital Marketing Class In Pune
ReplyDeleteWeb and Pro Infomedia is Best Digital Marketing Company in Lucknow
ReplyDeleteThis was an excellent post and very good information provided, Thanks for sharing.
ReplyDeleteSelenium Training in Chennai
Selenium Course in Chennai
Python Training in Bangalore
Python Course in Bangalore
Selenium Training in Bangalore
Selenium Course in Bangalore
Selenium Training in Coimbatore
salesforce institute in bangalore
Software Testing Course in Bangalore
Software Testing Course in Madurai
First of all I would like to thank you for writing this post I love both writing and reading new posts and I was just looking at new posts to see me something new, only then I saw your post and the rest of the post is praiseworthy.
ReplyDeletesofeeya.com
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
I went through your blog its really interesting and holds an informative content. Thanks for uploading such a wonderful blog.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ReplyDeleteData Science Institute in Bangalore
I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
ReplyDeletedata science course in guntur
https://digitalweekday.com/
ReplyDeletehttps://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
ReplyDeletehttps://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
Thanks for posting useful information.You have provided an nice article, Thank you very much for this one.
ReplyDeleteacte chennai
acte complaints
acte reviews
acte trainer complaints
acte trainer reviews
acte velachery reviews complaints
acte tambaram reviews complaints
acte anna nagar reviews complaints
acte porur reviews complaints
acte omr reviews complaints
Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging. Thank you for sharing wonderful information with us to get some idea about that content.
ReplyDeleteoracle training in chennai
oracle training institute in chennai
oracle training in bangalore
oracle training in hyderabad
oracle training
oracle online training
hadoop training in chennai
hadoop training in bangalore
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
ReplyDeletepython training in chennai
python course in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python training
python flask training
python flask online training
python training in coimbatore
This is really good post here. Thanks for such valuable information. Quality content is what always gets the visitors coming.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
Fantastic post found to be very impressive to come across such an awesome blog. I really felt enthusiast while reading and enjoyed every bit of your content. Certainly, since this blog is being more informative it is an added advantage for the users who are going through this blog. Once again nice blog keep it up.
ReplyDelete360DigiTMG IoT Course
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work. nice to read.
ReplyDeleteselenium training in chennai
selenium training in chennai
selenium online training in chennai
selenium training in bangalore
selenium training in hyderabad
selenium training in coimbatore
selenium online training
selenium training
Brilliant work and I really liked the blogging style and content in this, keep it up and also check this out
ReplyDeleteFull Stack Course Chennai
Full Stack Training in Bangalore
Full Stack Course in Bangalore
Full Stack Training in Hyderabad
Full Stack Course in Hyderabad
Full Stack Training
Full Stack Course
Full Stack Online Training
Full Stack Online Course
I simply want to mention I am just all new to blogging and site-building and truly loved you’re web page. Almost certainly I’m planning to bookmark your site . You really have outstanding stories. Many thanks for revealing your webpage.…
ReplyDeleteAzure Training in Chennai
Azure Training in Bangalore
Azure Training in Hyderabad
Azure Training in Pune
Azure Training | microsoft azure certification | Azure Online Training Course
Azure Online Training
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
Superb blog post! And this blog clearly explain about for useful information. I would Thanks for sharing this wonderful content. Keep it up!
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
I like the blog format as you create user engagement in the complete article. Thanks for the informative posts.Digital Marketing Training in Chennai
ReplyDeleteDigital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital Marketing Training in Annanagar
wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
ReplyDeletebusiness analytics training in Hyderabad
Thanks for giving great kind of information
ReplyDeletelearn salesforce online
salesforce online training in hyderabad
Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks Bethlehem Website Development
ReplyDeleteTruly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.
ReplyDeleteDigital Marketing Course in Raipur