+ Start a Discussion
sammy9099sammy9099 

Trigger to update field in two unrelated objects

On a lead record , I have created custom button (ASSIGN) which creates new record in different object ( Partner Id) . There is auto generated field in partner ID object calle NAME (standard field) . I want as soon as user clicks on ASSIGN and new record is created , it should copy that NAME value  in XL_Partner_Id__c on lead object.. I am not sure if this functionality can be achieved bu writing javascript custom button . Therefore , I am trying with trigger. it is giving me error :

Variable does not exist: partid.Name

 

trigger updateelitepartnerid on Partner_Id__c (before insert, before update){

List<ID> LeadIds = New List<ID>();

for(Partner_Id__c partid : Trigger.new){
if(partid.Name != null){
LeadIds.add(partid.Lead);
}
}

List<Lead> leadlist = [SELECT id,  XL_Partner_Id__c FROM Lead WHERE id in :LeadIds];
for(integer i = 0 ; i < leadlist.size(); i++){
leadlist[i].XL_Partner_Id__c= partid.Name ;

}

update leadlist;
}

Naidu PothiniNaidu Pothini
trigger updateelitepartnerid on Partner_Id__c (before insert, before update)
{
	List<ID> LeadIds = New List<ID>();

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			LeadIds.add(partid.Lead);
		}
	}

	Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id,  XL_Partner_Id__c FROM Lead WHERE Id IN :LeadIds]);

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			Lead ld = leadMap.get(partId.Lead);
			ld.XL_Partner_Id__c= partid.Name;

			leadMap.put(partId.Lead, ld);
		}
	}

	update leadMap.values();
/*
	List<Lead> leadlist = [SELECT id,  XL_Partner_Id__c FROM Lead WHERE id in :LeadIds];

	for(integer i = 0 ; i < leadlist.size(); i++)
	{
		leadlist[i].XL_Partner_Id__c = partid.Name ; 
}

update leadlist;
*/

}

 try this.

sammy9099sammy9099

this is the error I am getting:

 

Invalid field Lead for SObject Partner_Id__c at line 9 column 25

 

trigger updateelitepartnerid on Partner_Id__c (before insert, before update)
{
	List<ID> LeadIds = New List<ID>();

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			LeadIds.add(partid.Lead);
		}
	}

	Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id,  XL_Partner_Id__c FROM Lead WHERE Id IN :LeadIds]);

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			Lead ld = leadMap.get(partId.Lead);
			ld.XL_Partner_Id__c= partid.Name;

			leadMap.put(partId.Lead, ld);
		}
	}

	update leadMap.values();
/*
	List<Lead> leadlist = [SELECT id,  XL_Partner_Id__c FROM Lead WHERE id in :LeadIds];

	for(integer i = 0 ; i < leadlist.size(); i++)
	{
		leadlist[i].XL_Partner_Id__c = partid.Name ; 
}

update leadlist;
*/

}

 

Naidu PothiniNaidu Pothini

Replace it with LeadId or Lead__c what ever the API is.

sammy9099sammy9099

hey , 

 

Lead is standard object . I cannot append __c or Id. Even If I append , it is giviving me the same error.

Naidu PothiniNaidu Pothini

Look for the API name of the lead field on the Partner_Id__c object. and replace it with it.

 

so  if Lead_API_Name is the API Name of Lead field on the Partner_Id__c object then it should be like...

 

LeadIds.add(partid.Lead_API_NAME);
sammy9099sammy9099

I think , I am not able to depict the the problem clearly. But , there is no field called "lead " on the object "Partner_Id"

Naidu PothiniNaidu Pothini

then how are you trying to bring the Lead Ids from the Partner_Id__c records?

 

for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			LeadIds.add(partid.Lead);
		}
	}

 

sammy9099sammy9099

Hi , 

 

Thats one problems , I am facing in this scenario . Since , these two objects are not related to each other. I don't have any idea how to achieve this functionality . Is there anyway  to achieve this ? I just want when user clicks the SSIGN button lead record it should create a new record in Partner_Id object. When the new record is created , the auto generated number should be copied to Tx_Partner_Id field. 

sammy9099sammy9099

Hi Naidu , 

 

I have created a lookuop field on Lead called Partner_Gen_Id__c  to object Partner_Id__c .  I was wondering if the value in the Partner ID field ( API name : Name) on Partner_Id__c object can populate the the same value in look up field on Lead object ?  

below is your code is not showing any error but it the trigger is also not updating the value.

 

trigger updateelitepartnerid on Partner_Id__c (before insert)
{
List<ID> LeadIds = New List<ID>();

for(Elite_Partner_Id__c partid : Trigger.new)
{
if(partid.Name != null)
{
LeadIds.add(partid.Name);
}
}

Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Partner_Gen_Id__c FROM Lead WHERE Id IN :LeadIds]);

for(Partner_Id__c partid : Trigger.new)
{
if(partid.Name != null)
{
Lead Ld = leadMap.get(partId.Name);
Ld..Partner_Gen_Id__c= partid.Name;

leadMap.put(partId.Name, Ld);
}
}

update leadMap.values();


}

 

Naidu PothiniNaidu Pothini

You should create the lookup field on the Partner_Id__c object instead of lead object, so you can have a lead id for the Partner_Id__c records. Then this code will work fine... but you still need to chage this part

 

trigger updateelitepartnerid on Partner_Id__c (before insert)
{
    List<ID> LeadIds = New List<ID>();

    for(Elite_Partner_Id__c partid : Trigger.new)
    {
        if(partid.Name != null)
        {
            LeadIds.add(partid.Lookup Id for the Lead on the Partner_Id__c object);
    }
}

 

sammy9099sammy9099

Hey Naidu , 

 

I created a lookup field on Partner Id object , namely , lookup_field__c. I made all the modifications in the code. It is not giving any error, but , it is also not updating Elite_Id__c field on Lead with NAME field value on Partner_Id__C object. Below is the modified code. your help will be highly appreciated.

 

trigger updateelitepartnerid on Partner_Id__c (before insert)
{
List<ID> LeadIds = New List<ID>();

for(Partner_Id__c partid : Trigger.new)
{
if(partid.Name != null)
{
LeadIds.add(partid.lookup_field__c);
}
}

Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Elite_Id__c FROM Lead WHERE Id IN :LeadIds]);

for(Partner_Id__c partid : Trigger.new)
{
if(partid.Name != null)
{
Lead Ld = leadMap.get(partId.Name);
Ld.Elite_Id__c= partid.Name;

leadMap.put(partId.Name, Ld);
}
}

update leadMap.values();


}

 

Naidu PothiniNaidu Pothini
trigger updateelitepartnerid on Partner_Id__c (before insert)
{
	List<ID> LeadIds = New List<ID>();

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			LeadIds.add(partid.lookup_field__c);
		}
	}

	Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Elite_Id__c FROM Lead WHERE Id IN :LeadIds]);

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			Lead Ld = leadMap.get(partId.lookup_field__c);

			Ld.Elite_Id__c = partid.Name;

			leadMap.put(partId.lookup_field__c, Ld);
		}
	}
	update leadMap.values();
}
 

 try this.

sammy9099sammy9099

Hi , 

 

It is still not updating the field.

 

Regards

Naidu PothiniNaidu Pothini
trigger updateelitepartnerid on Partner_Id__c (before insert)
{
	List<ID> LeadIds = New List<ID>();

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			LeadIds.add(partid.lookup_field__c);
		}
	}

	System.debug('---------Trigger Map------------'+Trigger.new);

	Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Elite_Id__c FROM Lead WHERE Id IN :LeadIds]);

	System.debug('---------Lead Map------------'+leadMap);

	for(Partner_Id__c partid : Trigger.new)
	{
		if(partid.Name != null)
		{
			Lead Ld = leadMap.get(partId.lookup_field__c);

			Ld.Elite_Id__c= partid.Name;

			leadMap.put(partId.lookup_field__c, Ld);
		}
	}

	System.debug('-------- Lead values ---------'+leadMap.values());
	update leadMap.values();
}

 can you provide the debug for this code?

sammy9099sammy9099
27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:49:27.037 (37316000)|EXECUTION_STARTED
10:49:27.037 (37355000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:49:27.037 (37376000)|CODE_UNIT_STARTED|[EXTERNAL]|01qQ00000000dKB|updateelitepartnerid on Elite_Partner_Id trigger event BeforeInsert for [new]
10:49:27.038 (38318000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>()
10:49:27.038 (38354000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>()
10:49:27.038 (38581000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Elite_Partner_Id__c>.iterator()
10:49:27.038 (38926000)|SYSTEM_METHOD_EXIT|[5]|LIST<Elite_Partner_Id__c>.iterator()
10:49:27.038 (38952000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:49:27.038 (38977000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:49:27.039 (39051000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:49:27.039 (39066000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:49:27.039 (39102000)|SYSTEM_METHOD_ENTRY|[13]|String.valueOf(Object)
10:49:27.039 (39387000)|SYSTEM_METHOD_EXIT|[13]|String.valueOf(Object)
10:49:27.039 (39413000)|SYSTEM_METHOD_ENTRY|[13]|System.debug(ANY)
10:49:27.039 (39426000)|USER_DEBUG|[13]|DEBUG|---------Trigger Map------------(Elite_Partner_Id__c:{CurrencyIsoCode=USD, LastModifiedById=null, lookup_field__c=null, ConnectionReceivedId=null, OwnerId=005Q0000005yRImIAM, LastModifiedDate=null, ConnectionSentId=null, Name=null, SystemModstamp=null, CreatedById=null, CreatedDate=null, IsDeleted=false, Id=null})
10:49:27.039 (39435000)|SYSTEM_METHOD_EXIT|[13]|System.debug(ANY)
10:49:27.039 (39737000)|SOQL_EXECUTE_BEGIN|[15]|Aggregations:0|select Id, Elite_Partner_Id__c from Lead where Id IN :tmpVar1
10:49:27.044 (44386000)|SOQL_EXECUTE_END|[15]|Rows:0
10:49:27.044 (44534000)|SYSTEM_METHOD_ENTRY|[17]|String.valueOf(Object)
10:49:27.044 (44561000)|SYSTEM_METHOD_EXIT|[17]|String.valueOf(Object)
10:49:27.044 (44573000)|SYSTEM_METHOD_ENTRY|[17]|System.debug(ANY)
10:49:27.044 (44583000)|USER_DEBUG|[17]|DEBUG|---------Lead Map------------{}
10:49:27.044 (44589000)|SYSTEM_METHOD_EXIT|[17]|System.debug(ANY)
10:49:27.044 (44614000)|SYSTEM_METHOD_ENTRY|[19]|LIST<Elite_Partner_Id__c>.iterator()
10:49:27.044 (44636000)|SYSTEM_METHOD_EXIT|[19]|LIST<Elite_Partner_Id__c>.iterator()
10:49:27.044 (44647000)|SYSTEM_METHOD_ENTRY|[19]|system.ListIterator.hasNext()
10:49:27.044 (44661000)|SYSTEM_METHOD_EXIT|[19]|system.ListIterator.hasNext()
10:49:27.044 (44705000)|SYSTEM_METHOD_ENTRY|[19]|system.ListIterator.hasNext()
10:49:27.044 (44719000)|SYSTEM_METHOD_EXIT|[19]|system.ListIterator.hasNext()
10:49:27.044 (44748000)|SYSTEM_METHOD_ENTRY|[31]|MAP<Id,Lead>.values()
10:49:27.044 (44792000)|SYSTEM_METHOD_EXIT|[31]|MAP<Id,Lead>.values()
10:49:27.044 (44808000)|SYSTEM_METHOD_ENTRY|[31]|String.valueOf(Object)
10:49:27.044 (44822000)|SYSTEM_METHOD_EXIT|[31]|String.valueOf(Object)
10:49:27.044 (44833000)|SYSTEM_METHOD_ENTRY|[31]|System.debug(ANY)
10:49:27.044 (44842000)|USER_DEBUG|[31]|DEBUG|-------- Lead values ---------()
10:49:27.044 (44848000)|SYSTEM_METHOD_EXIT|[31]|System.debug(ANY)
10:49:27.044 (44864000)|SYSTEM_METHOD_ENTRY|[32]|MAP<Id,Lead>.values()
10:49:27.044 (44896000)|SYSTEM_METHOD_EXIT|[32]|MAP<Id,Lead>.values()
10:49:27.885 (45077000)|CUMULATIVE_LIMIT_USAGE
10:49:27.885|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 8 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:49:27.885|CUMULATIVE_LIMIT_USAGE_END

10:49:27.045 (45128000)|CODE_UNIT_FINISHED|updateelitepartnerid on Elite_Partner_Id trigger event BeforeInsert for [new]
10:49:27.112 (112324000)|CODE_UNIT_FINISHED|TRIGGERS
10:49:27.112 (112346000)|EXECUTION_FINISHED
Naidu PothiniNaidu Pothini
---------Trigger Map------------(Elite_Partner_Id__c:{CurrencyIsoCode=USD, LastModifiedById=null, lookup_field__c=null, ConnectionReceivedId=null, OwnerId=005Q0000005yRImIAM, LastModifiedDate=null, ConnectionSentId=null, Name=null, SystemModstamp=null, CreatedById=null, CreatedDate=null, IsDeleted=false, Id=null})

 You dont have a lead Id to this Partner_Id__c record and so it would not update any lead... is this a valid case?

sammy9099sammy9099

But , In the lookup_field_c , I am selecting a lead. I don't know how it is showing NULL.