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
Antonio_hotelbedsAntonio_hotelbeds 

Trigger doesnt apply updates on leads

Hi,

 

I just programmed a bulk trigger(or tried to, because Im starting with bulk triggers) in leads that update info in leads depending on the record type and the country of the lead. The problem is that if I change the country of the lead and click on Save, the data doest get updated until I click on Edit and Save again.Isnt that weird? Does anyone know why is that?

 

Here is my trigger code:

 

trigger UpdateMerchantInterface onLead (beforeinsert,beforeupdate) {

 

Set<Id> addLeads = newSet<Id>();

 

for (Lead lead: Trigger.new){

 

if (lead.RecordTypeId=='01220000000YDkG'){

addLeads.add(lead.id);

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELBEDS'){

addLeads.add(lead.id);

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='BEDSONLINE'){

addLeads.add(lead.id);

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELOPIA'){

addLeads.add(lead.id);

}

}

if(addLeads.size()>0){

               Map<Id,Lead> Updatevalues1 = new Map<Id,Lead> ([select route_to_market_del__c,RecordTypeId,Area__c,Lead_Country__c,Merchant__c,Interface__c,Invoicing_Company_Empresas__c from Lead where Id in: addLeads]);

 

     for (Lead ld2:Updatevalues1.values()){

 

               if (ld2.RecordTypeId=='012D00000002iGo' && ld2.Route_to_market_del__c=='HOTELBEDS'){//Caso de HB

                     if (ld2.Lead_Country__c=='SPAIN'){

                              ld2.Interface__c='WL';

                                        ld2.Invoicing_Company_Empresas__c='EW1';

                                        ld2.Merchant__c='WHITELABLEEUR';

                               }

               }

        }

     for (Lead l: Trigger.new){

       l.Interface__c=Updatevalues1.get(l.id).Interface__c ;

       l.Merchant__c=Updatevalues1.get(l.id).Merchant__c;

       l.Invoicing_Company_Empresas__c=Updatevalues1.get(l.id).Invoicing_Company_Empresas__c;

}

//}

}

 

I dont know why is this happening.

 

Thanks!

 

}

Best Answer chosen by Admin (Salesforce Developers) 
CLKCLK

i dont know why u r quering same records again in trigger as you already have those values in trigger context in form of trigger.new. And if you have posted all trigger code.So below code should satify your logic.

test it & let me know.And it's bulk record processing only.

 

for (Lead ld2: trigger.new){

 

if (ld2.RecordTypeId=='01220000000YDkG'|| (ld2.RecordTypeId=='012D00000002iGo' && ld2.Route_to_market_del__c=='HOTELOPIA')){//Hotelopiaif (ld2.Lead_Country__c=='SPAIN'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='PORTUGAL'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='UNITED KINGDOM'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCGBP';

}

elseif (ld2.Lead_Country__c=='IRELAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='DENMARK'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='FRANCE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='ITALY'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='GREECE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='POLAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='MEXICO'){

ld2.Interface__c=

'U';

ld2.Invoicing_Company_Empresas__c=

'MXB';

 

//Para México-Hotelopia no se actualiza el merchant

}

elseif (ld2.Lead_Country__c=='U.S.A.'||ld2.Lead_Country__c=='CANADA'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCUSD';

}

elseif (ld2.Area__c=='MEAPAC'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAME';

}

elseif (ld2.Area__c=='Americas' && !(ld2.Lead_Country__c=='U.S.A.' || ld2.Lead_Country__c=='CANADA' || ld2.Lead_Country__c=='MEXICO')){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAIBA';

}

}

All Answers

CLKCLK

it should not happen. single field update(inline editing) or hole record update(editing record using edit button) works same, both will call update trigger.

 

it thing you are just editing field without changing any value in it and clicking save button, so it won't call update command.

But if you just save record after clicking edit button, it will call update command.

am i right? is this the scenario?

CLKCLK

and you dont need this much code.

 

trigger UpdateMerchantInterface onLead (beforeinsert,beforeupdate) {

 

for (Lead ld2: Trigger.new){

 

               if (ld2.RecordTypeId=='012D00000002iGo' && ld2.Route_to_market_del__c=='HOTELBEDS' && ld2.Lead_Country__c=='SPAIN)

{//Caso de HB

                              ld2.Interface__c='WL';

                      ld2.Invoicing_Company_Empresas__c='EW1';

                      Id2.Merchant__c='WHITELABLEEUR';

   }



}

 

this will do.



Antonio_hotelbedsAntonio_hotelbeds

Hi,

 

It must be something wrong with my trigger because when I change, for example, the country of my lead with the edit button and then click on save it doestn fire the trigger. But, if just after that, without changing anything I click on edit and without making changes save again, it does update the fields.

 

Im really lost with this issue...Any ideas?

 

thanks!

Antonio_hotelbedsAntonio_hotelbeds

Hi again,

 

What I posted is just a part of my code. I just wanted to make it look more simple. There are mucho more info,but the fields that are being updated and the way of checking the info is the same.

 

thanks again

CLKCLK

for (Lead lead: Trigger.new){

bolean bSelect =  false;

if (lead.RecordTypeId=='01220000000YDkG'){

addLeads.add(lead.id);

bSelect =  true;

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELBEDS'){

addLeads.add(lead.id);
bSelect =  true;

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='BEDSONLINE'){

addLeads.add(lead.id);

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELOPIA'){

addLeads.add(lead.id);

bSelect =  true;

}

lead.adderror("fits in criteria:" + bSelect); // if true then record satisfieng your criteria}

 

try this changes, and see what shows on sf page after save in your both test scenario as you are saying.



Antonio_hotelbedsAntonio_hotelbeds

Hi Chetan,

 

Im getting an error when trying to deploy it to sandbox:

 

Set

<Id> addLeads = newSet<Id>();

Boolean bSelect=False;

for (Lead lead: Trigger.new){

 

if (lead.RecordTypeId=='01220000000YDkG'){

addLeads.add(lead.id);

bSelect=True;

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELBEDS'){

addLeads.add(lead.id);

bSelect=True;

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='BEDSONLINE'){

addLeads.add(lead.id);

bSelect=True;

}

elseif (lead.RecordTypeId=='012D00000002iGo' && lead.Route_to_market_del__c=='HOTELOPIA'){

addLeads.add(lead.id);

bSelect=True;

}

lead.adderror(

'fits in criteria:' + bSelect); // if true then record satisfieng your criteria}

}

 

ERROR: Variable bSelect doesnt exist

 

Thanks

 

 

Antonio_hotelbedsAntonio_hotelbeds

I also wrote the code in a non bunk trigger way and it works perfectly.

 

I would just prefer to code it in bunk methodology

Antonio_hotelbedsAntonio_hotelbeds

Chetan,

 

I already tried what you said and apparently it the conditions work fine in the two cases.

 

This is the rest of the code:

 

if(addLeads.size()>0){

Map<Id,

Lead> Updatevalues1 = new Map<Id,Lead>([select Route_to_market_del__c,RecordTypeId,Area__c,Lead_Country__c,Merchant__c,Interface__c,Invoicing_Company_Empresas__c fromLeadwhereId in: addLeads]);

 

for (Leadld2:Updatevalues1.values()){

 

if (ld2.RecordTypeId=='01220000000YDkG'|| (ld2.RecordTypeId=='012D00000002iGo' && ld2.Route_to_market_del__c=='HOTELOPIA')){//Hotelopiaif (ld2.Lead_Country__c=='SPAIN'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='PORTUGAL'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='UNITED KINGDOM'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCGBP';

}

elseif (ld2.Lead_Country__c=='IRELAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='DENMARK'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='FRANCE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='ITALY'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='GREECE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='POLAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='MEXICO'){

ld2.Interface__c=

'U';

ld2.Invoicing_Company_Empresas__c=

'MXB';

 

//Para México-Hotelopia no se actualiza el merchant

}

elseif (ld2.Lead_Country__c=='U.S.A.'||ld2.Lead_Country__c=='CANADA'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCUSD';

}

elseif (ld2.Area__c=='MEAPAC'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAME';

}

elseif (ld2.Area__c=='Americas' && !(ld2.Lead_Country__c=='U.S.A.' || ld2.Lead_Country__c=='CANADA' || ld2.Lead_Country__c=='MEXICO')){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAIBA';

}

 

if

(updatevalues1.size()>0 ){

 

for (Lead l: Trigger.new){

l.Interface__c=Updatevalues1.get(l.id).Interface__c ;

l.Merchant__c=Updatevalues1.get(l.id).Merchant__c;

l.Invoicing_Company_Empresas__c=Updatevalues1.get(l.id).Invoicing_Company_Empresas__c;

}

CLKCLK

i dont know why u r quering same records again in trigger as you already have those values in trigger context in form of trigger.new. And if you have posted all trigger code.So below code should satify your logic.

test it & let me know.And it's bulk record processing only.

 

for (Lead ld2: trigger.new){

 

if (ld2.RecordTypeId=='01220000000YDkG'|| (ld2.RecordTypeId=='012D00000002iGo' && ld2.Route_to_market_del__c=='HOTELOPIA')){//Hotelopiaif (ld2.Lead_Country__c=='SPAIN'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='PORTUGAL'){

ld2.Interface__c=

'W';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='UNITED KINGDOM'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCGBP';

}

elseif (ld2.Lead_Country__c=='IRELAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='DENMARK'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='FRANCE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='ITALY'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='GREECE'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='POLAND'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCEUR';

}

elseif (ld2.Lead_Country__c=='MEXICO'){

ld2.Interface__c=

'U';

ld2.Invoicing_Company_Empresas__c=

'MXB';

 

//Para México-Hotelopia no se actualiza el merchant

}

elseif (ld2.Lead_Country__c=='U.S.A.'||ld2.Lead_Country__c=='CANADA'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HTPIALCUSD';

}

elseif (ld2.Area__c=='MEAPAC'){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAME';

}

elseif (ld2.Area__c=='Americas' && !(ld2.Lead_Country__c=='U.S.A.' || ld2.Lead_Country__c=='CANADA' || ld2.Lead_Country__c=='MEXICO')){

ld2.Interface__c=

'O';

ld2.Invoicing_Company_Empresas__c=

'E15';

ld2.Merchant__c=

'HOTELOPIAIBA';

}

}

This was selected as the best answer