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
divya gourigari 14divya gourigari 14 

When Opportunity StageName equal to approved i need to insert new record in another object

trigger onOpportunity_AddPointofContact on Opportunity (after update)
{
 List<Point_of_Contact__c> getPrimaryLst=[select id,Primary__c from Point_of_Contact__c where Primary__c =:true];
    List<Point_of_Contact__c> insertPointConList=new List<Point_of_Contact__c>();
    for(Opportunity opp:Trigger.new)
    {
        Opportunity oldRec =Trigger.oldMap.get(opp.Id);
        if(oldRec.StageName != opp.StageName && opp.StageName == 'Approved')
        {
           Point_of_Contact__c con=new Point_of_Contact__c();
           ngsCon.Point_of_Contact__c=opp.Program_Officer__c;
           ngsCon.Contact__c=opp.npsp__Primary_Contact__c;
           ngsCon.Type__c='grant';
           ngsCon.Start_Date__c=opp.FGM_Base__Award_Date__c;
            if(getPrimaryLst.size() == 0){
             ngsCon.Primary__c =true;   
            }
           insertPointConList.add(ngsCon);
        }
        
    }
    if(insertPointConList.size()>0)
    {
        try {
            insert insertPointConList;
            system.debug('====>insertPointConList'+insertPointConList);
           }
        catch (Exception Ex){
            system.debug(Ex);
        }
    }
 
}

here i wrote the code but record is inserting duplicate records
Vershley JoyejobVershley Joyejob
Hello,

The trigger looks good, try having the login in a separate class though. 

If duplicate records are being created, you should check whether there's another process updating the Opportunity which causes the trigger to fire again and thus creating another record. Maybe a process builder or another trigger.

Thanks.
 
Ajay K DubediAjay K Dubedi
Hi Divya,

Please try below code: 

Trigger---->
trigger onOpportunity_AddPointofContact on Opportunity (after update)
{
    List<Point_of_Contact__c> getPrimaryLst = [select id,Primary__c from Point_of_Contact__c where Primary__c =:true];
    List<Point_of_Contact__c> insertPointConList = new List<Point_of_Contact__c>();
    for(Opportunity opp:Trigger.new)
    {
        Opportunity oldRec =Trigger.oldMap.get(opp.Id);
        if(oldRec.StageName != opp.StageName && opp.StageName == 'Approved')
        {
           Point_of_Contact__c ngsCon = new Point_of_Contact__c();
           ngsCon.Point_of_Contact__c = opp.Program_Officer__c;
           ngsCon.Contact__c = opp.npsp__Primary_Contact__c;
           ngsCon.Type__c = 'grant';
           ngsCon.Start_Date__c = opp.FGM_Base__Award_Date__c;
            if(getPrimaryLst.size() == 0){
             ngsCon.Primary__c =true;   
            }
           insertPointConList.add(ngsCon);
        }
    }
    if(insertPointConList.size()>0)
    {
        try {
            insert insertPointConList;
            system.debug('====>insertPointConList'+insertPointConList);
           }
        catch (Exception Ex){
            system.debug(Ex);
        }
    }
}

In case your issue is not resolved then do let me know and please provide custom fields Datatype.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com