Hi Guys!!
Hope you doing well.
One of my blog reader asked me to share code of trigger on Attachment object . Although , I have already written a trigger on attachment object. For the same click here. but this time I have got a different scenario.
Caution Notice :P :- This blog is meant for beginners like me, who are still learning salesforce or must say it is for those who are loving salesforce.
Scenario:- When an attachment is inserted under an account or a contact or Lead etc.Now, on the basis of a custom field on parent of attachment object, it should check whether any other record exist with same value of field . If there exist some record then this lead should get deleted and and the attachment should be attached to older one.
So , lets begin with it . Lets Say , I have acustom field on Lead object name as My_ name__c on lead objec. On the basis of this field I ll check whether there exist some records or not. so lets Jump to code .
Please give a look at my awesome friends Blog : piyush Blog
Hope you doing well.
One of my blog reader asked me to share code of trigger on Attachment object . Although , I have already written a trigger on attachment object. For the same click here. but this time I have got a different scenario.
Caution Notice :P :- This blog is meant for beginners like me, who are still learning salesforce or must say it is for those who are loving salesforce.
Scenario:- When an attachment is inserted under an account or a contact or Lead etc.Now, on the basis of a custom field on parent of attachment object, it should check whether any other record exist with same value of field . If there exist some record then this lead should get deleted and and the attachment should be attached to older one.
So , lets begin with it . Lets Say , I have acustom field on Lead object name as My_
trigger DeleteLeadIFNewMatchesOld on Attachment (Before insert) {
Set<Id> setOfNewLead = new Set<Id>();
Set<String> setOfMyname = new Set<String>();
Set<Id> setOfDeleteLeads = new Set<Id>();
for(attachment a : trigger.new ){
setOfNewLead.add(a.ParentId);
}
Map<Id,lead> mapOfNewLead = new Map<Id,Lead>([Select id , My_name__c from Lead where id in : setOfNewLead]);
Map<Id,Lead> mapOfOldLead = new Map<Id,Lead>([Select id , My_name__c from Lead where id not in : setOfNewLead]);
// Map<Id,String> = new Map<Id,String>();
System.debug(mapOfOldLead);
//if(mapOfNewLead.get(a.ParentId).My_name__c == mapOfOldLead.get())
for(Lead ld : mapOfNewLead.values()){
//System.debug('myname'+myname);
System.debug('mapOfNewLead.values()'+mapOfNewLead.get(ld.Id).My_name__c);
String myname = mapOfNewLead.get(ld.Id).My_name__c;
setOfMyname.add(myname);
}
for(attachment a : trigger.new ){
System.debug(setOfMyname);
System.debug('==19'+mapOfNewLead.get(a.ParentId).My_name__c);
System.debug('===');
//Id testId = [Select Id from Lead where My_name__c in]
for(Id getIdMap : mapOfOldLead.keySet()){
if(setOfMyname.contains(mapOfOldLead.get(getIdMap).My_name__c)){
setOfDeleteLeads.add(a.ParentId);
a.ParentId = getIdMap;
}
}
}
List<Lead> deleteList = [Select id from Lead where Id in : setOfDeleteLeads];
delete deleteList;
}
try this Code and then let me know how can I improve this code . I have made it quite complex.
ThanksAnkush
Cheers
ankushsalesforce@gmail.com
Please give a look at my awesome friends Blog : piyush Blog
No comments:
Post a Comment