• Bernd Bretzelmann
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
I created a trigger which should duplicate a record with its child records to another object, which works. Now I want to call that function with a click on a button. I created an Apex Class which has a method now I want to create a Visualforce page which just calls the method but it says the method is unknown

Here my VF
<apex:page controller="CreatInvoiceRecord" 
action="{!createDuplicateOfInvoice}">
 
</apex:page>

Here my code:

public class CreatInvoiceRecord {

        
        
        public static void createDuplicateOfInvoice(List<Invoice_draft__c> records) {
        
            for(Invoice_draft__c a : records){
        
            RECHNUNGEN_TEST__c rechnung = new RECHNUNGEN_TEST__C();
    
            //Create invoice record with the mapped data from the draft invoice
            rechnung.Name = a.Name;
            rechnung.Zu_zahlen_innerhalb_von_Tagen__c = a.Zu_zahlen_innerhalb_von_Tagen__c;
            rechnung.Account_Invoice__c = a.Account_Invoice__c;
            rechnung.Leistungszeitraum_Start2__c = a.Dateofservice_Start__c;
            rechnung.Leistungszeitraum_Ende2__c  = a.Dateofservice_end__c;
            rechnung.Rechnungsstatus__c = 'Offen';
            insert rechnung; 
         
            //Give a list of child records from the invoice draft
            List<LineItemDraftInvoice__c> lineitemdraftlist = [SELECT Name,Service_good_article__c,Price__c,Numberofunits__c FROM LineItemDraftInvoice__c Where Invoicedraft__c = :a.Id]; //Give me all Positions from theDraft Invoice
            List<Artikel__c> invoicelineitemlist = new List<Artikel__c>();
        
            for(Integer i= 0; i< lineitemdraftlist.size();i++){
        
            
                //Copy the inline items of the draft invoice and create duplicates for the new invoice
                Artikel__c newposition = new Artikel__c();
                newposition.Name = lineitemdraftlist[i].Name;
                newposition.Produkt__c  = lineitemdraftlist[i].Service_good_article__c;
                newposition.Einzelpreis__c = lineitemdraftlist[i].Price__c;
                newposition.Menge__c = lineitemdraftlist[i].Numberofunits__c;
                newposition.Rechnung__c = rechnung.Id;
            
                invoicelineitemlist.add(newposition);
            }
            insert invoicelineitemlist;
            
            }
        
        }
}

 
Hello,
I'm fairly new to coding and I want to create a trigger where I share a document with a certain chatter group automatically and give them contributer rights when I upload a file via the chatter feed.This is how my trigger looks like:

trigger publishFileToGroup on ContentDocumentLink (after insert) {

  List<CollaborationGroup> chattergroup = [SELECT Id FROM CollaborationGroup WHERE Name = 'Apex Trigger Group'];  
  List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

  ContentDocumentLink cdl = New ContentDocumentLink(
 
  LinkedEntityId = chattergroup[0].Id, ContentDocumentId = documents[0].Id, shareType = 'C');
 
  insert cdl;

}

This doesn't work and the following error message appears:
"Apex trigger publishFileToGroup caused an unexpected exception, contact your administrator: publishFileToGroup: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, publishFileToGroup: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Document with ID: 06958000000Aejh is already linked with the entity with ID: 0F9580000005tul: Linked Entity ID: [LinkedEntityId]

From my understanding it says that the document is already linked with the chatter group, which is not true. Any ideas?


 
When the System Administrator logs in as a different user and creates a record the activity history shows that the logged in user created the record, not the admin. How is it possible to know who really created the record?
 
When the System Administrator logs in as a different user and creates a record the activity history shows that the logged in user created the record, not the admin. How is it possible to know who really created the record?