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
nagarjuna gurajanagarjuna guraja 

Need Trigger

Write a trigger such that whenever Opportunity stage changes to contract sent but there is no contract sent it will throw error"Create contract sent"
CharuDuttCharuDutt
Hii Nagarjuna\
Can You Please Explain More About The Requirement
nagarjuna gurajanagarjuna guraja
Write a trigger on opportunity such that whenever opportunity stage changes to contract sent..Both Opportunity and Contract are linked with each other.But there was no Contract record..It will throw error like"Create contract record".
CharuDuttCharuDutt
Hii Nagarjuna
Try below Code
trigger OpportunityContractSent on Opportunity ( before update) {

    if(trigger.isBefore && trigger.isUpdate){
        OpportunityContractSenthelper.updatemethod(trigger.new,trigger.oldmap);
    }
}

#############################################################################################

public class OpportunityContractSenthelper {
    public static void updatemethod(list<opportunity> newOPP, map<Id,opportunity> oldmap){
        set<Id>oppIds = new set<Id>();
        for(opportunity opp : newOPP){
            if(opp.StageName == 'contract sent' && opp.StageName != oldmap.get(opp.Id).StageName){
                oppIds.add(Opp.Id); 
            }
        }
        list<Contract>lstCon = [Select Id,Opportunity__c from Contract where Opportunity__c In : oppIds ];
        
        for(opportunity opp : newOPP){
            integer i = 0;
            for(Contract oCon : lstCon){
                
                if(opp.Id == oCon.Opportunity__c){
                    i++;
                }
            }
            if(i == 0){
                opp.addError('Create contract record');
            }
        }
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
CharuDuttCharuDutt
Hii Nagarjuna
Try Below Code Little Bit Changed
trigger OpportunityContractSent on Opportunity ( before update) {

    if(trigger.isBefore && trigger.isUpdate){
        OpportunityContractSenthelper.updatemethod(trigger.new,trigger.oldmap);
    }
}####################################################

public class OpportunityContractSenthelper {
    public static void updatemethod(list<opportunity> newOPP, map<Id,opportunity> oldmap){
        set<Id>oppIds = new set<Id>();
        for(opportunity opp : newOPP){
            if(opp.StageName == 'contract sent' && opp.StageName != oldmap.get(opp.Id).StageName){
                oppIds.add(Opp.Id); 
            }
        }
        list<Contract>lstCon = [Select Id,Opportunity__c from Contract where Opportunity__c In : oppIds ];
        
        for(opportunity opp : newOPP){
           
            if(lstCon.IsEmpty(){
                opp.addError('Create contract record');
            }
        }
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!