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
Roopa S 1Roopa S 1 

hi have written this trigger to calculate discount but while saving getting error anybody plz suggest ur solutions

this is the error im getting while saving the code.
Failed to create createContainerMember for containerId=1dc5f000001ETl9AAG: duplicate value found: MetadataContainerId duplicates value on record with id: 4015f000000pZWq, MetadataContainerId duplicates value on record with id: 4015f000000pZWq


trigger bookTrigger on Books__c (after insert) {
    list<Books__c> listOfBooks1 = new list<Books__c>();
    list<Books__c> listOfBooks = new list<Books__c>();
    Set<ID> setOfId = new Set<ID>();
    for(Books__c book: Trigger.New){
        setOfId.add(book.Id);
    }    
    
    listOfBooks = [SELECt Id,Price__c FROM Books__c WHERE Id IN:setOfId];
    if(listOfBooks.size()>0){
       for(Books__c book: listOfBooks){
            book.Price__c =  book.Price__c - (10/100)*book.Price__c;
            listOfBooks1.add(book);
       }
        
    }
    if(listOfBooks1.size()>0){
       update listOfBooks1;
    }
}
Best Answer chosen by Roopa S 1
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Roopa,

Please try this.
 
trigger Bookinsert on Book__c (before insert,before update) {
    for(Book__c book: Trigger.New){
        Decimal discount= 0.1*book.Price__c;
        book.Price__c =  (book.Price__c - discount);
    }    
    
}

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Roopa,

As you are trying to update the field on the book you need to use before Insert context. Please modify the code as below.
 
trigger Bookinsert on Book__c (before insert) {
    for(Book__c book: Trigger.New){
        Decimal discount= 0.1*book.Price__c;
        book.Price__c =  (book.Price__c - discount);
    }    
    
}

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
Roopa S 1Roopa S 1
@Sai Praveen    yes its working for insert but it should also work for update how to do?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Roopa,

Please try this.
 
trigger Bookinsert on Book__c (before insert,before update) {
    for(Book__c book: Trigger.New){
        Decimal discount= 0.1*book.Price__c;
        book.Price__c =  (book.Price__c - discount);
    }    
    
}

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Roopa S 1Roopa S 1
@Sai Praveen  working fine thank you so much