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
Danny SFDCDanny SFDC 

Upsert Trigger

Hi ppl,
There are 2 objects. Quotation_Pos__c and Product__c with same fields. There is no relationship between them (no lookup). When a record is inserted in Quotation_Pos__c, Product__c should be checked to see if the same record exists. If it exists then update it else create a new one. If Quotation_Pos__c is deleted, also Product__c is deleted.
Everything works fine except a little issue. When i update Quotation_Pos__c , the Product__c  already created before is also updated but creates a new Product__c. If i change Quotation_Pos__c , create new Product__c, and i dont want to do that. 
Can you help me please?
Here is my trigger:
trigger MyQuotationPosTrigger on Quotation_Pos__c (after insert, after update, before delete) {
    
	if (Trigger.isInsert) {
   
                for (Quotation_Pos__c quotationPos : Trigger.new) {
                  
                    //the quotation__c of all quottion_pos__c
                    List<Quotation__c> quotationLst = [Select Id, Name, Active__c, Opportunities__c From Quotation__c Where Id =: quotationPos.Quotation__c];
                     integer i=0;
                    //the quotation__c's opportunity
                    List<Opportunity> oppLst =[Select Id, Name From Opportunity Where Id =: quotationLst[0].Opportunities__c]; 
                    
                    Quotation__c q = quotationLst.get(i);
                    if(q.Active__c == true){
                        
                    Product__c newProd     = new Product__c();
                    newProd.NAME__c        = quotationPos.NAME__c;
                    newProd.MENGE__c       = quotationPos.MENGE__c;
                    newProd.ARTIKEL__c     = quotationPos.ARTIKEL__c;
                    newProd.ISOALPHA2__c   = quotationPos.ISOALPHA2__c;
                    newProd.ARTIKELTEXT__c = quotationPos.ARTIKELTEXT__c;
                    newProd.ZOLLTARIFNR__c = quotationPos.ZOLLTARIFNR__c;
                    newProd.NETTO__c       = quotationPos.NETTO__c;
                    newProd.EINZELPREIS__c = quotationPos.EINZELPREIS__c;
                    newProd.VKME__c        = quotationPos.VKME__c;
                    newProd.SERIENNR__c    = quotationPos.SERIENNR__c;
                    newProd.STDZUB__c      = quotationPos.STDZUB__c;
                    newProd.Opportunity__c = oppLst[0].Id;
                    newProd.PosID__c = quotationPos.id;
                
                   	    i++;
                    
                        try{
                            insert newProd;
                        }
                        catch(Exception e){
                            System.debug(e.getStackTraceString());
                        }
                    }
                }
      }
    
    if (Trigger.isUpdate){
        
			for (Quotation_Pos__c quotationPos : Trigger.new) {
                  
                    //the quotation__c of all quottion_pos__c
                    List<Quotation__c> quotationLst = [Select Id, Name, Active__c, Opportunities__c From Quotation__c Where Id =: quotationPos.Quotation__c];
                 
                    //the quotation__c's opportunity
                    List<Opportunity> oppLst =[Select Id, Name From Opportunity Where Id =: quotationLst[0].Opportunities__c]; 

                    list<Product__c> listofProducts=[select id, PosID__c, NAME__c, MENGE__c, ARTIKEL__c, ISOALPHA2__c, ARTIKELTEXT__c, ZOLLTARIFNR__c, NETTO__c, EINZELPREIS__c, VKME__c, SERIENNR__c, STDZUB__c from Product__c];
                
                for(integer i=0; i< listofProducts.size(); i++ ){
                    if(quotationPos.id == listofProducts[i].PosID__c){
                            listofProducts[i].NAME__c        = quotationPos.NAME__c;
                            listofProducts[i].MENGE__c       = quotationPos.MENGE__c;
                            listofProducts[i].ARTIKEL__c     = quotationPos.ARTIKEL__c;
                            listofProducts[i].ISOALPHA2__c   = quotationPos.ISOALPHA2__c;
                            listofProducts[i].ARTIKELTEXT__c = quotationPos.ARTIKELTEXT__c;
                            listofProducts[i].ZOLLTARIFNR__c = quotationPos.ZOLLTARIFNR__c;
                            listofProducts[i].NETTO__c       = quotationPos.NETTO__c;
                            listofProducts[i].EINZELPREIS__c = quotationPos.EINZELPREIS__c;
                            listofProducts[i].VKME__c        = quotationPos.VKME__c;
                            listofProducts[i].SERIENNR__c    = quotationPos.SERIENNR__c;
                            listofProducts[i].STDZUB__c      = quotationPos.STDZUB__c;
                            try{
                                update listofProducts[i];
                            }
                            catch(Exception e){
                                System.debug(e.getStackTraceString());
                            }
                    }
                    else{
                        Product__c newProd     = new Product__c();
                        newProd.NAME__c        = quotationPos.NAME__c;
                        newProd.MENGE__c       = quotationPos.MENGE__c;
                        newProd.ARTIKEL__c     = quotationPos.ARTIKEL__c;
                        newProd.ISOALPHA2__c   = quotationPos.ISOALPHA2__c;
                        newProd.ARTIKELTEXT__c = quotationPos.ARTIKELTEXT__c;
                        newProd.ZOLLTARIFNR__c = quotationPos.ZOLLTARIFNR__c;
                        newProd.NETTO__c       = quotationPos.NETTO__c;
                        newProd.EINZELPREIS__c = quotationPos.EINZELPREIS__c;
                        newProd.VKME__c        = quotationPos.VKME__c;
                        newProd.SERIENNR__c    = quotationPos.SERIENNR__c;
                        newProd.STDZUB__c      = quotationPos.STDZUB__c;
                        newProd.Opportunity__c = oppLst[0].Id;
                        newProd.PosID__c       = quotationPos.id;
                    
                        try{
                            insert newProd;
                        }
                        catch(Exception e){
                            System.debug(e.getStackTraceString());
                        }
                    }
                }   
               
               	
            }   
        
        
    } 
    
    
    if (Trigger.isDelete){
              set<id> QuotationPosIds = new set <id>();
        	  for(Quotation_Pos__c quotPos : trigger.old){
                  QuotationPosIds.add(quotPos.id);
                 }
              list<Product__c> listofProducts=[select id, PosID__c from Product__c where PosID__c in : QuotationPosIds];
        	  system.debug('listofProducts'+listofProducts);
        	  delete listofProducts;
    }
    
}

 
Maharajan CMaharajan C
Hi Danny,

You have to write the trigger like below:

I just replicated your scenario in my dev org and used the some fields from you. so please copy the below code and make the changes as per your need.

trigger MyQuotationPosTrigger on Quotation_Pos__c (after insert, after update, before delete) {
    
    if (Trigger.isInsert) {
    
    set<Id> qpid =new set<id>();
   
                for (Quotation_Pos__c quotationPos : Trigger.new) {
                  {
                  qpid.add(quotationPos.Id);
                  system.debug('QPId ='+qpid); 
                  }
                  
                  
                    //the quotation__c of all quottion_pos__c
                    List<Quotation__c> quotationLst = [Select Id, Name, Active__c, Opportunities__c From Quotation__c Where Id =: quotationPos.Quotation__c];
                    integer i=0;
                    //the quotation__c's opportunity
                    List<Opportunity> oppLst =[Select Id, Name From Opportunity Where Id =: quotationLst[0].Opportunities__c]; 
                    
                List<Product__c> newinsert = new List<Product__c>();    
                    for (Quotation_Pos__c quotationPos1 : Trigger.new) {               
                    Quotation__c q = quotationLst.get(i);
                    if(q.Active__c == true){
                        
                    Product__c newProd     = new Product__c();
                    newProd.MENGE__c       = quotationPos1.MENGE__c;
                    newProd.ARTIKEL__c     = quotationPos1.ARTIKEL__c;
                    newProd.ISOALPHA2__c   = quotationPos1.ISOALPHA2__c;
                    newProd.ARTIKELTEXT__c = quotationPos1.ARTIKELTEXT__c;                   
                    newProd.Opportunity__c = oppLst[0].Id;
                    newProd.PosID__c = quotationPos1.id;
                    newinsert.add(newProd);   
                        i++;
                    
                      /*  try{   
                            insert newProd;
                        }
                        catch(Exception e){
                            System.debug(e.getStackTraceString());
                        }    */
                    }
                }
                Insert newinsert;
      }
    }
    if (Trigger.isUpdate)
    {
       
            set<Id> qpupid = new set<id>();
            set<Id> quoId = new set<id>();
            for (Quotation_Pos__c quotationPos : Trigger.new) {
                  qpupid.add(quotationPos.Id);
                  quoId.add(quotationPos.Quotation__c);
                  system.debug('QPId ='+qpupid);
                  }
                    //the quotation__c of all quottion_pos__c
                   List<Quotation__c> quotationLst = [Select Id, Name, Active__c, Opportunities__c From Quotation__c Where Id =: quoId];
                 
                    //the quotation__c's opportunity
                   List<Opportunity> oppLst =[Select Id, Name From Opportunity Where Id =: quotationLst[0].Opportunities__c]; 

                    list<Product__c> listofProducts=[select id, PosID__c, MENGE__c, ARTIKEL__c, ISOALPHA2__c, ARTIKELTEXT__c from Product__c where PosID__c =:  qpupid  ];
                
                    Map<Id,Quotation_Pos__c> parentMap=new Map<Id,Quotation_Pos__c>([select id,Name,MENGE__c,ARTIKEL__c,ISOALPHA2__c,ARTIKELTEXT__c from Quotation_Pos__c where id IN:qpupid]);
                    List<Product__c> upprod = new List<Product__c>();
                //for(integer i=0; i< listofProducts.size(); i++ )
                   // if(quotationPos.id == listofProducts[i].PosID__c)
                   if(listofProducts.size()>0)
                   {
                   for(Product__c listofProducts1:[select id, PosID__c, MENGE__c, ARTIKEL__c, ISOALPHA2__c, ARTIKELTEXT__c from Product__c where PosID__c =:  qpupid])
                   {
                   
                            listofProducts1.MENGE__c       = parentMap.get(listofProducts1.PosID__c).MENGE__c;
                            listofProducts1.ARTIKEL__c     = parentMap.get(listofProducts1.PosID__c).ARTIKEL__c;
                            listofProducts1.ISOALPHA2__c   = parentMap.get(listofProducts1.PosID__c).ISOALPHA2__c;
                            listofProducts1.ARTIKELTEXT__c = parentMap.get(listofProducts1.PosID__c).ARTIKELTEXT__c  ;   
                                  upprod.add(listofProducts1);                   
                           /* try{
                                update listofProducts[i];
                            }
                            catch(Exception e){
                                System.debug(e.getStackTraceString());
                            }  */
                    }
                    update upprod;
                    }
                    
                    else{
                        for (Quotation_Pos__c quotationPos2: Trigger.new) {

                        Product__c newProd     = new Product__c();
                        newProd.MENGE__c       = quotationPos2.MENGE__c;
                        newProd.ARTIKEL__c     = quotationPos2.ARTIKEL__c;
                        newProd.ISOALPHA2__c   = quotationPos2.ISOALPHA2__c;
                        newProd.ARTIKELTEXT__c = quotationPos2.ARTIKELTEXT__c;
                        newProd.Opportunity__c = oppLst[0].Id;
                        newProd.PosID__c       = quotationPos2.id;
                    
                       try{
                            insert newProd;
                        }
                        catch(Exception e){
                            System.debug(e.getStackTraceString());
                        } } }
                    }
                   

    
    if (Trigger.isDelete){
              set<id> QuotationPosIds = new set <id>();
              for(Quotation_Pos__c quotPos : trigger.old){
                  QuotationPosIds.add(quotPos.id);
                 }
              list<Product__c> listofProducts=[select id, PosID__c from Product__c where PosID__c in : QuotationPosIds];
              system.debug('listofProducts'+listofProducts);
              delete listofProducts;
    }
    
}

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj