function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sathisathi 

pls give solution

hi, i have an application library management and i have three fields  like bookid,book name ,author name.......my problem is when i entered the book id ,the fields book name and author name should be updated with book name and author name regarding with book id.......

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Trigger updateRelated Fields on Object__c(before insert,before update){

 

List<Id> bookIds = new List<Id>();

 

for(Object__c obj : trigger.new){

bookIds.add(obj.book__c); //assuming the API name for bookid is book__c

 

}

 

List<Book__c> bookList = [select book__r.name,book__r.authorname__c from Book__c where id in:bookIds];

}

 

for(Object__c obj : trigger.new){

obj.bookname__c = bookList[0].book__r.name;

obj.authorname__c = bookList[0].book__r.authorname__c;

 

}

All Answers

Vinit_KumarVinit_Kumar

Do you want to update the fields the record is saved,if so you can write an APex Trigger on that object and populate the fields based on book id in insert or update events.

sathisathi

thank you, If you have any example trigger ,can u post me pls

Vinit_KumarVinit_Kumar

Trigger updateRelated Fields on Object__c(before insert,before update){

 

List<Id> bookIds = new List<Id>();

 

for(Object__c obj : trigger.new){

bookIds.add(obj.book__c); //assuming the API name for bookid is book__c

 

}

 

List<Book__c> bookList = [select book__r.name,book__r.authorname__c from Book__c where id in:bookIds];

}

 

for(Object__c obj : trigger.new){

obj.bookname__c = bookList[0].book__r.name;

obj.authorname__c = bookList[0].book__r.authorname__c;

 

}

This was selected as the best answer
sathisathi

thank you i got it ...