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
Nivedita Shukla 3Nivedita Shukla 3 

Update terriority custom field of opportunity when lead is converted based on lead city ,state and country

Below is the trigger written for the same .
I'm stuck to get terriority based on field . how can i achieve this?
trigger trigMapFields on Lead (after Update) {
TriggerHelper.createOpportunity(trigger.new);
}
createOpportunity(List listList){ List oUpdList = new List (); for(Lead lead: LeadList){ if (lead.IsConverted && lead.convertedOpportunityId != null){ //How to get terriority based on city state and country Opportunity opp=new Opportunity(id = lead.convertedOpportunityId, terriority__c =:terriority oUpdList.add(opp); } } update oUpdList ; }
any help would be appriciated!
Ajay K DubediAjay K Dubedi
Hi Nivedita,
Try this code:
 
createOpportunity(List<Lead> LeadList){
    List<Opportunity> oUpdList = new List<Opportunity>(); 
    Set<Id> opIdSet = new Set<Id>();
    for(Lead lead: LeadList){
        if (lead.IsConverted && lead.convertedOpportunityId != null)
        { 
            opIdSet.add(lead.convertedOpportunityId);
        } 
    }
    Map<Id, Opportunity> IdVsOpportunityMap = new Map<Id, Opportunity>([SELECT Id, terriority__c FROM Opportunity WHERE Id IN: opIdSet]);
    
        if(IdVsOpportunityMap.size() > 0) {
        for(Lead le : LeadList) {
            if(IdVsOpportunityMap.containsKey(le.convertedOpportunityId)) {
                IdVsOpportunityMap.get(le.convertedOpportunityId).terriority__c = le.Country;/put what you want.....
                oUpdList.add(IdVsOpportunityMap.get(le.convertedOpportunityId));
            }
        }
    }
    update oUpdList;
}



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