You need to sign in to do that
Don't have an account?
mac admin
Trigger to update the picklist field
Hi all,
Can anyone help me how to write a trigger to update the picklist field in custom object when an record is created in another custom object, If the email ID's are same then picklist value should update.
Thanks in advance.
Regards,
mac.
Can anyone help me how to write a trigger to update the picklist field in custom object when an record is created in another custom object, If the email ID's are same then picklist value should update.
Thanks in advance.
Regards,
mac.
Let's have the custom object as customObj__c and another std object as stdObject to which you have to update the field when
both has the email field as email__c.Let's assume that the stdObject has a field called updateText which you should update using trigger.
Since you need the customObj__c id ,you should use after insert
trigger myTrigger on customObj__c (after insert) {
Set<String> allnewInsertEmailId = new Set<String>();
for (customObj__c acc : Trigger.new) {
allnewInsertEmailId.add(acc.email__c);
}
//now we have got all unique emailid after insert of custom object
List<stdObject> allStdObjectwithMatchingEmail=[select id,updateText,email__c where email__c in :allnewInsertEmailId];
for(stdObject std :allStdObjectwithMatchingEmail)
{
std.updateText= std.updateText+' changed';
}
update allStdObjectwithMatchingEmail;
}
Please let me know if you face any issue further .AlsoThis is somewhat a simple implementation , if you post your requirement more specific i can provide you better solution
Thanks and Regards,
Shiva RV
Thanks for the reply. In my scenario two objects are custom objects. and when creating a record in second object if the email is matched with any record of the first object then custom picklist field in that record should update.
Can you help me over here.
Regards,
mac.
Hi mac,
let's do follow this approach : -
trigger secondObjectTrigger on secondObject (before insert,before update, after insert, after update)
{
Map<String,FirstObject> mp_FrstObject;
Set<String> set_String = new Set<string>();
if(trigger.isAfter && Trigger .isInsert)
{
for(SecondObject s : Trigger.new)
{
if(s.Email__c!=null)
set_String.add(s.Email__c);
}
for(FirstObject frstObj : [Select Id,Name,Email__c from FirstObject where Email__c IN:set_String])
{
if(mp_FrstObject==null)
mp_FrstObject = new Map<String,FirstObject>();
mp_FrstObject.put(frstObj.Email__c,frstObj);
}
for(SecondObject s : Trigger.new)
{
if(s.Email__c!=null && mp_FrstObject!=null && mp_FrstObject.containsKey(s.Email__c))
{
mp_FrstObject.get(s.Email__c).picklistField = 'anyvalue';
}
}
if(mp_FrstObject!=null && mp_FrstObject.values()!=null)
update mp_FrstObject.values();
}
Thanks
Thanks for the replay. I'm facing the issue in the production environment throwing error as below :
System.LimitException: Too many query rows: 50001
Can you help me over here.
Thanks in advance.
Regards,
mac.
Hi mac,
Please mark as the best answer, if it works for you , otherwise ping me the issue, I will solve the same.
Thanks
System.LimitException: Too many query rows: 50001
Can you help me over here.
Thanks in advance.
Regards,
mac.
Hi mac,
It's a salesforce limit, Query can return 50,000 rows, so As per your issue, The query return more than 50,000 records of first object on basis of email matched with created secondObject email.
You can use limit in query ex - : for(FirstObject frstObj : [Select Id,Name,Email__c from FirstObject where Email__c IN:set_String limit 50000]);
I have given the limit 50000 but the issue is not resolved. Can you please help me over here.
Regards,
mac.
Hi mac,
could you please paste your code over here, so that I will troubleshoot the exact issue and will be provided you the solution.
Thanks
System.LimitException: Too many query rows: 50001
Regards,
mac
Hi mac,
To resolve this issue, we can utilize below things : -
We can add another where condition , for example., we can utilize the Inquiry_Form_Leads__c records who got created after oct 2016 for ex-
Select Id,Name,Email__c from Inquiry_Form_Leads__c where Email__c IN:set_String and createdDate > 2016-10-01T00:00:00Z
Please let us know, if you are able to retrieve the records without exception, then we will do workaround to use two or three queries on basis of created Date.
Thanks
You have used soql query inside for loop so in that case the limitation on the size of returned list of data is only 1000
refer this . https://developer.salesforce.com/forums/?id=906F00000008y4lIAA
Make a little change in your code as follows
for(List<Inquiry_Form_Leads__c> frstObjs : [Select Id,Name,Email__c from Inquiry_Form_Leads__cwhere Email__c IN:set_String])
{
for(Inquiry_Form_Leads__c firstObj : frstObjs)
{
}
}
Now it should work . Your code with modification as below
Also
Instead of
for(UnderGradute__c s : Trigger.new)
{
if(s.Email__c!=null && mp_FrstObject!=null && mp_FrstObject.containsKey(s.Email__c))
{
mp_FrstObject.get(s.Email__c).Converted_Status__c = 'Matched';
}
}
Use
if(s.Email__c!=null && mp_FrstObject!=null && mp_FrstObject.containsKey(s.Email__c))
{
for (String recordId : mp_FrstObject.keySet())
{
for(Inquiry_Form_Leads__c leadObj : mp_FrstObject.get(recordId))
{
leadObj.Converted_Status__c='Matched';
}
}
}
// haven't tested the map method , so try with your traditional method first
Do let me know if the code worked proper for you
Thanks and Regards,
Shiva RV
While Replaceing with the method as you mentioned above it throwing error as below
Loop must iterate over a collection type: Inquiry_Form_Leads__c at line 29 column 65.
Can you help me over here.
Regards,
mac.
Even after changing the query I'm getting the same issue. Can we do this in any other way. Or is any permissions need to be give for this Object..?
Regards,
mac.
Hi mac,
Could you please let me know about following question -
1. How many records (Inquiry_Form_Leads__c) exist in your org ?
2. Did you include createdDate in where clause of query ?
3. You can quick give a try with createdDate > 2017-01-01T00:00:00Z and let me know, Does you get records retrieved from query or not
4. Inquiry_Form_Leads__c have unique email or not ?
Thanks
I have changed my code as you mentioned. Below is my modified code. In my Inquiry Leads there are only 1600 records are there. Yes the email fields is Unique. Is the uniqueness will effect ..? to hte record creating in another custom object..?
Regards,
mac.
I have changed my code as you mentioned. Below is my modified code.
trigger verifyINQ on UnderGradute__c (before insert,before update, after insert, after update)
{
Map<String,Inquiry_Form_Leads__c> mp_FrstObject;
Set<String> set_String = new Set<string>();
if(trigger.isAfter && Trigger.isInsert)
{
for(UnderGradute__c s : Trigger.new)
{
if(s.Email__c!=null)
set_String.add(s.Email__c);
}
for(List<Inquiry_Form_Leads__c> frstObjs : [Select Id,Name,Email__c from Inquiry_Form_Leads__c where Email__c IN:set_String and createdDate > 2016-10-01T00:00:00Z])
{
for(Inquiry_Form_Leads__c frstObj:frstObjs)
{
if(mp_FrstObject==null)
mp_FrstObject = new Map<String,Inquiry_Form_Leads__c>();
mp_FrstObject.put(frstObj.Email__c,frstObj);
}
}
for(UnderGradute__c s : Trigger.new)
{
if(s.Email__c!=null && mp_FrstObject!=null && mp_FrstObject.containsKey(s.Email__c))
{
mp_FrstObject.get(s.Email__c).Converted_Status__c = 'Matched';
}
}
if(mp_FrstObject!=null && mp_FrstObject.values()!=null)
update mp_FrstObject.values();
}
}
In my Inquiry Leads there are only 1600 records are there. Yes the email fields is Unique. Is the uniqueness will effect ..? to hte record creating in another custom object..?
Regards,
mac.
Hi mac,
Sorry for such issue happened, but I am wondering when records count is 1600, then why soql for loop trying to returned more records,
Anyway, please you can backup to this code and try to remove listSoqlFor loop.
Give it a try with
for(Inquiry_Form_Leads__c frstObjs : [Select Id,Name,Email__c from Inquiry_Form_Leads__c where Email__c IN:set_String])
Just like before,and let's create one record (UnderGradute__c) with email ,and let me know the result
Thanks
No the issue is same. I tried by removing the unique to email Id in Inquiry_Form_Leads__c even then the issue same.
Other than this can we achive this any other way..?
Regards,
mac.
No worry, Could you please provide your best time for meeting ? I want to see, what the procedural steps You are doing and will discuss the approaches to troubleshoot the issue, in fast manner.
Thanks
Can you please let me know your email ID so that I can provide you the details.
Hi mac.
Email Id : rajatzmaheshwari@gmail.com
Thanks