You need to sign in to do that
Don't have an account?

Update Opportunity fields from lead fields
trigger Giveendclientname on Lead (before insert, before update) { List<ID> OppIds = New List<ID>(); for(lead ld : Trigger.new){ if(ld.Customer_Type__c == 'partner'){ OppIds.add(ld.Customer_Type__c); } List<Opportunity> oppList = [SELECT id, EndClientName__c FROM Opportunity WHERE id in :OppIds]; // for(Lead opp : trigger.new){ if(oppList.EndClientName__c == ''){ // oppList[i].status = 'Approved'; oppList.addError('End client name should be given if it is a partner Account'); } // oppList.add(opp); update oppList; // } } }
The requirement is, when the customer type in lead is partner, then the endclient name in opportunity should not be blank. it should throw an error.
Assuming you ar going based off the ConvertedOpportunityId then the code below should do what you want. Otherwise you will need to replace the CovertedOpportunityId fields below with your Opportunity Id field
This is the error I'm getting,
Method does not exist or incorrect signature: void contains(Id) from the type Map<Id,Opportunity>
NOTE: This code has not been tested and may contain typographical or logical errors
Thanks for the code. but unfortunately its not working for me.even if im saving the record without giving endclient name it's not shwing any error.
As per best practice, you should avoid SOQL and DML inside for loop.
trigger Giveendclientname on Lead (before insert, before update) {
Map<ID,ID> LeadOppMap = New Map<ID,ID>();
for(lead ld : Trigger.new){
if (ld.IsConverted && ld.convertedOpportunityId != null && ld.Customer_Type__c == 'partner' )
LeadOppMap.add(ld.id,ld.convertedOpportunityId);
}
}
Map<ID,Opportunity> oppList = [SELECT id, EndClientName__c FROM Opportunity WHERE id in :LeadOppMap.values()];
for(lead ld : Trigger.new){
Opportunity rec = oppList.get(LeadOppMap.get(ld.id));
if(isBlank(rec.EndClientName__c)){
ld.addError('End client name should be given if it is a partner Account');
}
}
}
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.