You need to sign in to do that
Don't have an account?
SFine
Using a trigger to update a multi-picklist from a number of Multi-picklists
Hello everyone, I'm have a bit of trouble trying to a trigger that will update a multipicklist value correctly but the code I'm using seem to cancel out all the semi colons I put in. Also, when the value is blank, it'll always return null despite my efforts to prevent it from doing so. Can anyone provide an idea? Below is the code I'm using:
trigger SaleContractUpdate on Covered_Account__c (After insert, after update) { List<Opportunity> OppList=new List<Opportunity>(); List<Covered_Account__c> CAList=new List<Covered_Account__c>(); Set<Id> trackOppProdUpdate = new Set<Id>(); List<ID> OIDS=new List<ID>(); List<ID> opIDS=new List<ID>(); List<ID> CAIDS=new List<ID>(); for (Covered_Account__c obj : Trigger.new) { // Covered_account__c oldObj=trigger.oldmap.get(obj.id) Oids.add(obj.Opportunity_Name__c); if((trigger.isInsert || (trigger.oldmap.get(obj.id).opportunity_Product__c!=obj.Opportunity_Product__c))) opids.add(obj.Opportunity_product__c); } oppList=[select Sale_Contracting_Event_Type__c, id from Opportunity where id IN: oids]; CAList=[select id, sale_Type__c, Opportunity_Name__c from Covered_Account__c where Opportunity_Name__c IN: oids]; List<Opportunity> olist=new List<Opportunity>(); for(Opportunity opp:opplist) { String entry=''; Set<String> track=new Set<String>(); for(covered_account__c op:calist){ if(op.Opportunity_Name__c == opp.id) { if(!track.contains(op.Sale_Type__c) && (op.sale_type__c!=null && op.sale_type__c!='null' && op.sale_type__c!='')) { system.debug('sales type '+op.Sale_Type__c); entry+=op.Sale_Type__c+'; '; track.add(op.Sale_Type__c); } } System.debug('entry '+entry); } if(entry.length()>0) entry=Entry.substring(0,entry.length()-1); system.debug('opp '+opp); opp.Sale_Contracting_Event_Type__c=entry; system.debug('opp '+opp); update opp; system.debug('opp '+opp); } // update oList; }
Any help would be appreciated
WRT blank fields, the Apex Developer's Guide states that sobject fields that are Strings can never be empty, only null. Given that picklists are stored as strings in the database I'd imagine they are subject to a similar rule (though I don't recall reading an explicit statement of this.
WRT the stripping of the semi-colons:
I've been trying to reproduce your scenario without success, albeit in the system log rather than a trigger, but I can't see that would make any difference.
I've added a multi-select picklist to my account object, which is just TLAs for each month.
I've been able to set this successfully to :
'May;Jun;' (trailing semi-colon)
'May; Jun; ' (with spaces)
'May;Jun' (no spaces or trailing semi-colon).
I've also tried setting these values into a (non multi-select) picklist and they go in okay there too.
Can you post the debug output and the before/after for your records?
Sure, here's the info
Before the change
Sale_Contracting_Event_Type__c=nullNew Product/ServiceNew Product/Servi
Entry that's going into the field - Though I'd like to cut down the entries to one of each type, so that's another hurdle to tackle
Entry: New Product/Service;Early Termination; New Product/Service;
The field after the change
Sale_Contracting_Event_Type__c=New Product/Service;Early Termination; New Product/Service;
Final value on the page layout: nullNew Product/ServiceNew Product/Servi; Early Terminationnull
This is from 5 different multi pick lists
Hope that helps
PS: I've change the code based on your advice and to it helps... a little bit, at least semi-colons seem to appear now: here's the new code for the main loop:
I'm not sure where your nulls are coming from - that would usually indicate that you have appended some text to a String that was initially null, but your code is checking against that.
I'm also suprised that you are seeing multiples - that implies that they are actually subtley different strings. Have you tried dumping out your track variable to see what is in there - you should see one entry for each element in the string.