Reputation: 307
I have a custom object1(Lent_c List of books lent out in the library. It has 2 feilds book lookup and person lookup) with 2 lookup feild that has another custom object2(book object. It has 3 feilds Quantity, number of feilds borrowed and ISBN) in the lookup feild. I am trying to change value in the object2 using the object1. This is the code i am using .
trigger Book_lentout on Lent__c (before insert) {
for(Lent__c l:Trigger.new)
{
Book__c k = l.Book_Details__c;
k.Books_Borrowed__c++;
}
}
It gives me a error Error: Compile Error: Illegal assignment from Id to Book__c at line 4 column 9
From what I understood its returning ID instead of Book__c object type
Upvotes: 0
Views: 4481
Reputation: 251
For your code above, no need to code for this one - just have a roll-up field on your Book object which sums up the number of Lent items.
As for your actual question, as long as you know what type of object you are looking for, your above code would be: Book_c book = new Book__c(Id = l.Book_Details__c);
Now, you would have to do a query to get the number of books borrowed so you could increment by 1, so you would have to issue a soql query once gathering all id's. So here's the full solution for learning's sake. trigger Book_lentout on Lent__c (before insert) {
Id[] bookIds = new List<Id>();
for(Lent__c lent : Trigger.new)
{
bookIds.add(l.Book_Details__c);
}
Book__c[] books = [select Books_Borrowed__c from Book__c where Id in :bookIds];
for (Book__c b : books) {
b.Books_Borrowed__c += 1;
}
}
This is not even complete since you could have multiple lent records for each book, but you get the jist (I hope). But again, you should really just do this via a roll-up field.
Upvotes: 1