-3

I was trying to implement a lead trigger where upon conversion and adding the optional opportunity, it will change the contact custom picklist field (Lead_Status__c) to "Qualified" (after the conversion).

I have the code below but am getting an error:

Error: Compile Error: Variable does not exist: oppToContact at line 20 column 21

Trigger Lead on Lead (after update)
{
    if (Trigger.isAfter)
    {
        if (Trigger.isUpdate)
        {
            Set<Id> setContact = new Set<Id>();
            Set<Id> setOpp = new Set<Id>();
            //Map<ID,ID> oppToContact = new Map<ID,ID>();

            for (Lead ld : Trigger.new)
            {

                // Find all converted Leads with Opportunity and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null)
                {  

                    setContact.add(ld.ConvertedContactId );

                    setOpp.add(ld.ConvertedOpportunityId );

                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID); // Error Line 

                }

            }

            if(setContact.size() > 0 )
            {

                List<Contact> lstContact = [select id , Lead_Status__c from contact where id in:setContact];

                for(Contact cont : lstContact)
                {
                    cont.Lead_Status__c ='Qualified';
                }

                update lstContact ;
            }
        }
    }
}

after editing the code like below, I'm now getting a different error: Error Error: Compile Error: map must have exactly 2 type arguments at line 6 column 9

Trigger Lead on Lead (after update)

{ if (Trigger.isAfter) { if (Trigger.isUpdate) { Set setContact = new Set(); Set setOpp = new Set(); Map oppToContact = new Map(); for (Lead ld : Trigger.new) { // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null) { setContact.add(ld.ConvertedContactId ); setOpp.add(ld.ConvertedOpportunityId ); oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID); } } if(setContact.size() > 0 ) { List lstContact = [select id,Lead_Status__c from contact where id in:setContact]; for(Contact cont : lstContact) { cont.Lead_Status__c ='Qualified'; } update lstContact ; } } } }

Thanks for the answers, you guys are awesome

2

Whats the type of your set Set setContact = new Set(); syntax should be like below

For String the syntax is like this : Set s1 = new Set();

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm

Trigger Lead on Lead (after update) {
    if (Trigger.isAfter) {
        if (Trigger.isUpdate) {
            Set<Id> setContact = new Set<Id>();
            Set<Id> setOpp = new  Set<Id>();
            Map<Id,String> oppToContact = new  Map<Id,String>();
            for (Lead ld : Trigger.new) {
                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null) {
                    setContact.add(ld.ConvertedContactId );
                    setOpp.add(ld.ConvertedOpportunityId );
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID); 
                }
            }
            if(setContact.size() > 0 ) {
                List<contact> lstContact = [select id,Lead_Status__c from contact where id in:setContact];
                for(Contact cont : lstContact) {
                    cont.Lead_Status__c ='Qualified';
                }
                update lstContact ;
            }
        }
    }
}

Above code stays

  • Hi RedDevil, Following your code, I get a different compiler error: Error Error: Compile Error: expecting a semi-colon, found 'setContact' at line 6 column 20 Set setContact = new Set(); Please advise – Raelis Silvestre Jun 5 '17 at 10:37
  • As i explained mention the type of set Set<Id> setContact = new Set<Id>(); This is just illustration please mention based on your scenario – RedDevil Jun 5 '17 at 10:38
  • it supposed to be a String since it will find the unique Id of the Contact and the Opp – Raelis Silvestre Jun 5 '17 at 10:49
  • then it will be Set<String> setContact = new Set<String>(); – RedDevil Jun 5 '17 at 10:50
0

Hello Raelis Silvestre, Check your code on line:20, method/variable name is ConvertedContactId not ConvertedContactID.

Trigger Lead on Lead (after update) {
    if (Trigger.isAfter) { 
        if (Trigger.isUpdate) {
            Set<String> setContact = new Set<String>();
            Set<String> setOpp = new  Set<String>();
            Map<String,String> oppToContact = new  Map<String,String>();
            for (Lead ld : Trigger.new) {
                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null) {
                    setContact.add(ld.ConvertedContactId );
                    setOpp.add(ld.ConvertedOpportunityId );
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactId); 
                }
            }
            if(setContact.size() > 0 ) {
                List<Contact> lstContact = [select id,Lead_Status__c from Contact where id = :setContact];
                for(Contact cont : lstContact) {
                    cont.Lead_Status__c ='Qualified';
                }
                update lstContact ;
            }
        }
    }
}

Regards, Alap Mistry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.