+ Start a Discussion
@anilbathula@@anilbathula@ 

After update trigger is creating duplicate records

Hi friends

 

            iam creating a record in custom object name loan from opportunity.when the opportunity stage is sourcing it should create a record in loan but its creating duplicate records.please help me in this.

 


 

 trigger lol on Opportunity (after update) {
    List<loans__c>ln=new List<loans__c>();
    
    for(opportunity o:Trigger.New){
        
        if(((Trigger.IsInsert)||(Trigger.isUpdate)&& (Trigger.oldmap.get(o.id).StageName!='Sourcing'))&& (o.StageName=='Sourcing'))
        {
        loans__c l=new loans__c(Name=o.Name,Approval_Status__c=o.StageName,Opportunity__c=o.Id);
        ln.add(l);
        }
        try{
        insert ln;
        }
        catch(system.DmlException e){
        System.debug(e);
        }
        }


}


 

Thanks

 

Anil

RadnipRadnip

Well I would do a query to check to make sure it hasn't been created yet, otherwise people changing the stage backwards and forwards would result in duplicates being created anyway.

@anilbathula@@anilbathula@

Hi dataville

 

      can u please help me in this .To get out of this problem.

 

Thanks

Anil.B