You need to sign in to do that
Don't have an account?
Shruthi Narsi
Populate Record Id's
Hi
I have written triggers to sync quotes on opportunity products
Can but in opportunity name feild product name is not getting displayed instead some numbers are getting displyed. According to me it is opportunity id's. Can anyone help me with the code.
Below is the snip for your reference
trigger QuoteLineItemTrigger on Quotes__c (after insert, after update)
{
Map<Id,Id> quoteIdToOppId =new Map<Id,Id>();
Map<Id,List<QuoteLineitem__c>> quoteIdToLineItems = new Map<Id,List<QuoteLineitem__c>>();
List<OpportunityLineItem__c> oppLineItems = new List<OpportunityLineItem__c>();
Map<Id,String> oppIdtoName = new Map<Id,String>();
for(Quotes__c q : trigger.new) {
if(q.IsSyncing__c && q.IsSyncing__c != Trigger.oldMap.get(q.Id).IsSyncing__c )
quoteIdToOppId.put(q.id, q.OpportunityId__c);
}
System.debug('UAC: quoteIdToOppId ' + quoteIdToOppId);
for( QuoteLineitem__c qli :[SELECT Id, Product2Id__c,Product2Id__r.name,Quantity__c,ServiceDate__c, QuotesId__c,ListPrice__c,Line_Item_Description__c,Total_Price__c,Product2Id__r.Product_Code__c,UnitPrice__c
FROM QuoteLineitem__c WHERE QuotesId__c =:quoteIdToOppId.keyset() ]) {
List<QuoteLineitem__c> tempList = quoteIdToLineItems.get(qli.QuotesId__c) ;
if(tempList == null) {
templist = new List<QuoteLineitem__c>();
quoteIdToLineItems.put(qli.QuotesId__c, templist);
}
tempList.add(qli);
}
System.debug('UAC: quoteIdToLineItems ' + quoteIdToLineItems);
for(Opportunities__c opp : [SELECT Id , Name FROM Opportunities__c WHERE ID IN :quoteIdToOppId.values()] ) {
oppIdtoName.put(opp.id, opp.Name) ;
}
System.debug('UAC: oppIdtoName ' + quoteIdToLineItems);
for(Quotes__c q : Trigger.new) {
if(q.IsSyncing__c && quoteIdToLineItems.containsKey(q.Id)) {
for(QuoteLineitem__c qli : quoteIdToLineItems.get(q.Id)) {
OpportunityLineItem__c oli = new OpportunityLineItem__c();
oli.OpportunityId__c = quoteIdToOppId.get(qli.QuotesId__c);
System.debug(' qli.Product2Id__r.name ' + qli.Product2Id__r.name);
oli.Name = qli.Product2Id__r.name;
System.debug(' qli.Product2Id__c ' + qli.Product2Id__c);
System.debug(' qli.ListPrice__c ' + qli.ListPrice__c);
System.debug(' qli.Line_Item_Description__c ' + qli.Line_Item_Description__c);
oli.Product2Id__c = qli.Product2Id__c;
oli.Quantity__c = qli.Quantity__c;
oli.ListPrice__c = integer.valueof(qli.ListPrice__c);
oli.Description__c = qli.Line_Item_Description__c;
oli.ServiceDate__c = qli.ServiceDate__c;
oli.Total_Price__c = qli.Total_Price__c;
oli.ProductCode__c = qli.Product2Id__r.Product_Code__c;
oli.Sales_Price__c =qli.UnitPrice__c;
oppLineItems.add(oli);
}
}
System.debug('UAC: oppLineItems ' + oppLineItems);
if(oppLineItems.size() > 0)
insert oppLineItems ;
}
}
I have written triggers to sync quotes on opportunity products
Can but in opportunity name feild product name is not getting displayed instead some numbers are getting displyed. According to me it is opportunity id's. Can anyone help me with the code.
Below is the snip for your reference
trigger QuoteLineItemTrigger on Quotes__c (after insert, after update)
{
Map<Id,Id> quoteIdToOppId =new Map<Id,Id>();
Map<Id,List<QuoteLineitem__c>> quoteIdToLineItems = new Map<Id,List<QuoteLineitem__c>>();
List<OpportunityLineItem__c> oppLineItems = new List<OpportunityLineItem__c>();
Map<Id,String> oppIdtoName = new Map<Id,String>();
for(Quotes__c q : trigger.new) {
if(q.IsSyncing__c && q.IsSyncing__c != Trigger.oldMap.get(q.Id).IsSyncing__c )
quoteIdToOppId.put(q.id, q.OpportunityId__c);
}
System.debug('UAC: quoteIdToOppId ' + quoteIdToOppId);
for( QuoteLineitem__c qli :[SELECT Id, Product2Id__c,Product2Id__r.name,Quantity__c,ServiceDate__c, QuotesId__c,ListPrice__c,Line_Item_Description__c,Total_Price__c,Product2Id__r.Product_Code__c,UnitPrice__c
FROM QuoteLineitem__c WHERE QuotesId__c =:quoteIdToOppId.keyset() ]) {
List<QuoteLineitem__c> tempList = quoteIdToLineItems.get(qli.QuotesId__c) ;
if(tempList == null) {
templist = new List<QuoteLineitem__c>();
quoteIdToLineItems.put(qli.QuotesId__c, templist);
}
tempList.add(qli);
}
System.debug('UAC: quoteIdToLineItems ' + quoteIdToLineItems);
for(Opportunities__c opp : [SELECT Id , Name FROM Opportunities__c WHERE ID IN :quoteIdToOppId.values()] ) {
oppIdtoName.put(opp.id, opp.Name) ;
}
System.debug('UAC: oppIdtoName ' + quoteIdToLineItems);
for(Quotes__c q : Trigger.new) {
if(q.IsSyncing__c && quoteIdToLineItems.containsKey(q.Id)) {
for(QuoteLineitem__c qli : quoteIdToLineItems.get(q.Id)) {
OpportunityLineItem__c oli = new OpportunityLineItem__c();
oli.OpportunityId__c = quoteIdToOppId.get(qli.QuotesId__c);
System.debug(' qli.Product2Id__r.name ' + qli.Product2Id__r.name);
oli.Name = qli.Product2Id__r.name;
System.debug(' qli.Product2Id__c ' + qli.Product2Id__c);
System.debug(' qli.ListPrice__c ' + qli.ListPrice__c);
System.debug(' qli.Line_Item_Description__c ' + qli.Line_Item_Description__c);
oli.Product2Id__c = qli.Product2Id__c;
oli.Quantity__c = qli.Quantity__c;
oli.ListPrice__c = integer.valueof(qli.ListPrice__c);
oli.Description__c = qli.Line_Item_Description__c;
oli.ServiceDate__c = qli.ServiceDate__c;
oli.Total_Price__c = qli.Total_Price__c;
oli.ProductCode__c = qli.Product2Id__r.Product_Code__c;
oli.Sales_Price__c =qli.UnitPrice__c;
oppLineItems.add(oli);
}
}
System.debug('UAC: oppLineItems ' + oppLineItems);
if(oppLineItems.size() > 0)
insert oppLineItems ;
}
}
As in the code:-
you are doing oli.OpportunityId__c = quoteIdToOppId.get(qli.QuotesId__c); where quoteIdToOppId is an map of quoteId to opportunity Id so, that is why it is showing opportunity id instead of name.
Also, in the opportunity name field, you want the opportunity name so, you need to make a map of opportunity id to opportunity name or the whole opportunity itself. Same goes for the product.
Thanks,
Rishabh Bansal