You need to sign in to do that
Don't have an account?
Nicu Tanase
The opportunity SyncedQuote field is read only within a trigger
Hello,
I have a trigger that clones an opportunity and tries to insert the cloned one.
Something like that :
Opportunity renewalOpportunity = o.clone();
.... [apex logic]
insert renewalOpportunity;
The problem is that I receive this error: The opportunity SyncedQuote field is read only within a trigger .
Well, the most awkard thing is that SyncedQuote is null.
I've tried to extend my right in order to be able to modify SyncesQuote but i am not able to do this.
I've tried to delete the field from my trigger but i reach another err
Error:Apex trigger createRenewalOpportunity caused an unexpected exception, contact your administrator: : System.NullPointerException: Attempt to de-reference a null object
How can i solve this problem?
Thanks in advance!!
I have a trigger that clones an opportunity and tries to insert the cloned one.
Something like that :
Opportunity renewalOpportunity = o.clone();
.... [apex logic]
insert renewalOpportunity;
The problem is that I receive this error: The opportunity SyncedQuote field is read only within a trigger .
Well, the most awkard thing is that SyncedQuote is null.
I've tried to extend my right in order to be able to modify SyncesQuote but i am not able to do this.
I've tried to delete the field from my trigger but i reach another err
Error:Apex trigger createRenewalOpportunity caused an unexpected exception, contact your administrator: : System.NullPointerException: Attempt to de-reference a null object
How can i solve this problem?
Thanks in advance!!
trigger createRenewalOpportunity on Opportunity (before update) {
if(!MyValidator_cls.hasAlreadyDone()) {
for (Opportunity o : Trigger.new) {
if(o.StageName == 'Won' && oldOpportunity.StageName != 'Won'){
Opportunity renewalOpportunity = o.clone();
system.debug('===o.clone()===renewalOpportunity.SyncedQuoteId='+renewalOpportunity.SyncedQuoteId);
system.debug('===o.clone()===renewalOpportunity.SyncedQuote='+renewalOpportunity.SyncedQuote);
renewalOpportunity.SyncedQuoteId = null;
system.debug('===o.clone()===renewalOpportunity.SyncedQuoteId='+renewalOpportunity.SyncedQuoteId);
system.debug('===o.clone()===renewalOpportunity.SyncedQuote='+renewalOpportunity.SyncedQuote);
renewalOpportunity.Name = 'Renewal - ' + o.Name;
renewalOpportunity.Type = 'Renewal Business';
renewalOpportunity.StageName = 'Closing & Final Negotiation';
renewalOpportunity.ForecastCategoryName = 'Closed Order placed';
renewalOpportunity.LeadSource = 'Renewal';
renewalOpportunity.Offer_Type__c = 'Renewal';
renewalOpportunity.Offer_Type_Discount__c = '35';
system.debug('===before insert===renewalOpportunity.SyncedQuoteId='+renewalOpportunity.SyncedQuoteId);
system.debug('===before insert===renewalOpportunity.SyncedQuote='+renewalOpportunity.SyncedQuote);
insert renewalOpportunity;
MyValidator_cls.setAlreadyDone();
}
}
}
}
How can i skip the SyncedQuote and SyncedQuoteId when cloning?
As a matter of fact when cloning from the interface, these fields are not cloned.
Thanks you for your answer.
try this once
I've tried this earlier only for my SyncedQuote field, but it doesn't work, the rror being : Method does not exist or incorrect signature: [Opportunity].remove(String)
Also i missed a line in my trigger, here is the complete trigger with all your additional code:
trigger createRenewalOpportunity on Opportunity (before update) {
if(!MyValidator_cls.hasAlreadyDone()) {
String strFldApiName;
Map<String, Schema.SObjectField> fields = Opportunity.sObjectType.getDescribe().fields.getMap();
for (Opportunity o : Trigger.new) {
Opportunity oldOpportunity = Trigger.oldMap.get(o.Id);
if(o.StageName == 'Won' && oldOpportunity.StageName != 'Won'){
Opportunity renewalOpportunity = o.clone();
for(Schema.SObjectField field : fields.values()){
if(!field.getDescribe().isUpdateable())
{
strFldApiName = field.getDescribe().getName();
if(renewalOpportunity.containsKey(strFldApiName))
renewalOpportunity.remove(strFldApiName);
}
}
renewalOpportunity.Name = o.Name;
renewalOpportunity.Name = 'Renewal - ' + o.Name;
renewalOpportunity.Type = 'Renewal Business';
renewalOpportunity.StageName = 'Closing & Final Negotiation';
renewalOpportunity.ForecastCategoryName = 'Closed Order placed';
renewalOpportunity.LeadSource = 'Renewal';
renewalOpportunity.Offer_Type__c = 'Renewal';
renewalOpportunity.Offer_Type_Discount__c = '35';
insert renewalOpportunity;
MyValidator_cls.setAlreadyDone();
}
}
}
}
Then we can try this to get desired result.
Opportunity oldOpportunity = Trigger.oldMap.get(o.Id);
Otherwise you will receive this err : Variable does not exist: oldOpportunity.StageName
Do you have any ther idea beside manually unset the SyncedQuoteId?
I am really out of options here :(
OK, so this is going to work :
I've created another opportunity and copied to that oppty all the cloned fields :
Opportunity renewalOpportunity2 = new Opportunity();
for(Schema.SObjectField field : fields.values()){
strFldApiName = field.getDescribe().getName();
strFldApiValue = renewalOpportunity.get(strFldApiName);
if(field.getDescribe().isUpdateable() && strFldApiValue != null)
{
renewalOpportunity2.put(strFldApiName, strFldApiValue);
}
}
Apparently even if Synced QUote is null and is created by the clone() process itself it's a problem... thumbs down for sales force :(
.
Assume, we have following records :
Opp1 (Opportunity)
Quote1, Quote2 (children of Opp1)
Say Quote1 is in sync so Opp1.SyncedQuote = Quote1.
Now we are tring to clone Opp1 with Name 'Opp1-Clone' and copy all other field-value pair into it. Let's imagine we have successfully inserted Opp1-Clone with SyncedQuote = Quote1 ( means Opp1-Clone.SyncedQuote = Quote1). What it indicateds is Opp1-Clone is having Quote1 as a child and it's sync is ON; Then what about Opp1??!! Quote1 was supposed to child of Opp1 right? Also we cannot have Child to have 2 parents (Quote1 to have both Opp1, Opp1) !!
If Opp1-Clone.SyncedQuote = Quote1, it gives a feel like Quote1's parent is changed to Opp1-Clone.
So, ignoring copying of SyncedQuote irrespective of whether it is null or filled is the proper outcome I believe.