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
Iswarya Sekar 9Iswarya Sekar 9 

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.
pconpcon
The code you have here will never work because you are adding you opportunity ids a string that equals 'partner' and not the id.  Additionally, you are adding the error opportunity not to the lead.

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
 
trigger giveEndClientName on Lead (before insert, before update) {
    Set<Id> oppIds = new Set<Id>();

    for (Lead l : Trigger.new) {
        if (l.Customer_Type__c == 'partner') {
            oppIds.add(l.ConvertedOpportunityId);
        }
    }

    oppIds.remove(null);

    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([
        select EndClientName__c
        from Opportunity
        where Id in :oppIds
    ]);

    for (Lead l : Trigger.new) {
        if (
            l.Customer_Type__c == 'partner' &&
            (
                oppMap.contains(l.ConvertedOpportunityId) &&
                String.isBlank(oppMap.get(l.ConvertedOpportunityId).EndClientName__c)
            )
        ) {
            l.addError('End client name should be given if it is a partner account');
        }
    }
}

 
Iswarya Sekar 9Iswarya Sekar 9
Hi pcon,
This is the error I'm getting,

Method does not exist or incorrect signature: void contains(Id) from the type Map<Id,Opportunity>
pconpcon
Sorry it should be containsKey
Iswarya Sekar 9Iswarya Sekar 9
where i need to give this?
pconpcon
Updated code below
 
trigger giveEndClientName on Lead (before insert, before update) {
    Set<Id> oppIds = new Set<Id>();

    for (Lead l : Trigger.new) {
        if (l.Customer_Type__c == 'partner') {
            oppIds.add(l.ConvertedOpportunityId);
        }
    }

    oppIds.remove(null);

    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([
        select EndClientName__c
        from Opportunity
        where Id in :oppIds
    ]);

    for (Lead l : Trigger.new) {
        if (
            l.Customer_Type__c == 'partner' &&
            (
                oppMap.containsKey(l.ConvertedOpportunityId) &&
                String.isBlank(oppMap.get(l.ConvertedOpportunityId).EndClientName__c)
            )
        ) {
            l.addError('End client name should be given if it is a partner account');
        }
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors
Iswarya Sekar 9Iswarya Sekar 9
Hi Pcon,
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.
pconpcon
Based on the information I have for your object structure and flow, that's the best I can do
Iswarya Sekar 9Iswarya Sekar 9
Okay thank you.
Musunuru SurekhaMusunuru Surekha
Hello Iswarya,

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');
        }
    }   
}
 
pconpcon
Musunuru, you code will not work as provided for all instances because it is a before trigger and the Id would not be set on a before insert and would not return an opporutnity.  Also, you have lots of places in your provided code that would end in NPEs as well as there is no method just "isBlank" so I'm assuming you mean String.isBlank.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Musunuru SurekhaMusunuru Surekha
Absolutely. This scenario cannot have before insert trigger in first place. Sorry, my bad for the String.isBlank(). Also can you please let me know the places where my code ends in NPE.