|
|
Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 06 Sep 12 11:38 AM
|
fiogf49gjkf0d I am working on getting ou Opportunities working after being off this project for several months.
Currently they do not save correctly. The opportunity saves but the address from the form does not save.
There is a relationship for the address table that should allow the save.
Does anyone have the original SaveOpportunity code handy that they can upload?
Here is mine which is failing...
namespace Sage.BusinessRules.CodeSnippets
{
public static partial class OpportunityBusinessRules
{
public static void OnSave( IOpportunity opportunity)
{
// TODO: Complete business rule implementation
Sage.Entity.Interfaces.IAddress address = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.IAddress>();
address.Address1 = opportunity.Address.Address1;
address.Address2 = opportunity.Address.Address2;
address.Address3 = opportunity.Address.Address3;
address.Address4 = opportunity.Address.Address4;
address.State = opportunity.Address.State;
address.City = opportunity.Address.City;
address.PostalCode = opportunity.Address.PostalCode;
address.Country = opportunity.Address.Country;
address.County = opportunity.Address.County;
address.Routing = opportunity.Address.Routing;
address.Salutation = opportunity.Address.Salutation;
address.TimeZone = opportunity.Address.TimeZone;
address.Type = opportunity.Address.Type;
address.Description = opportunity.Address.Description;
address.IsPrimary = opportunity.Address.IsPrimary;
address.IsMailing = opportunity.Address.IsMailing;
address.EntityId = opportunity.Id.ToString();
address.Save();
opportunity.AddressID = address.Id.ToString();
opportunity.Save();
}
}
}
Thank you!!
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 10 Sep 12 4:26 PM
|
fiogf49gjkf0d OK. Well that is obviously a bust.
Can anyone show me how to accomplish the following?
I want to save an address for my opportunity.
It is a job site and is specific to the opportunity.
I have tried a relationship to Address with the code above. It fails.
I have tried a separate table for the address with a relationship. It fails.
You would think this would be simple. What am I missing?
Thanks,
W
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 10 Sep 12 8:43 PM
|
fiogf49gjkf0d You are not storing an Address.EntityId value......place the OpportunityId in there and see what happens. I often store other 'entities' addresses in the Address Table.....including Activity Address (where the Meeting was held). |
|
|
| |
| |
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 11 Sep 12 12:00 PM
|
fiogf49gjkf0d My bad, didn't see the EntitiId field being populated....
I've never done this on the WEB side of things.....for all you and I know we are running into some sort of business rule. However, you state you created a new 'OppAddress' table entity and it still doesn't work?
Do you have a relationship between Opp.AddressId and Address.AddressID, or is a 1:1 extension table property? Do you have a relationship between Opp.OpportunityId and Address.EntityId as well? are you displaying the results in a Grid (for a relationship), or fields (1:1 extension).
When you run your save routine, what are you getting out of SLX Profiler.....
I'm not in front of any code at present, but are you absolutely certain that your entity create statement is correct? There doesn't have to be a new clause in it for example. |
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 11 Sep 12 1:12 PM
|
fiogf49gjkf0d Did this to test...
Sage.Entity.Interfaces.IAddress address = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.IAddress>();
throw new Sage.Platform.Application.ValidationException(address.Id.ToString());
I get an error of Object Reference not Set to an INstance of an Object.
So I guess that is where the problem lies. I should probably check to see if an Address exists and update or create based upon that.
I just have to figure out how that works in SageWorld.
|
|
|
| |
| |
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 12 Sep 12 11:28 AM
|
fiogf49gjkf0d Thanks Ryan! That is what I did starting yesterday. I have been looking through Leads for examples.
So far I am not havivng any luck. I am considering just throwing the code into my assembly DLL I use for other stuff and calling it good.
It would be nice if I could just get it to work in the Snippet tho. The task seems simple.
Hae an Address control on the Opportunity Detail page. Save the Address to the Address table.
I should only need the relationship set to 1:M back to Address from Opportunity with SaveUpdateDelete, correct?
Then when the Business Rule for Opportunity Save fires, it should cascade down to the Addres field, correct?
Or do I have to custom code the whole process to get the address into Address and set it to the OpportunityID in EntityID?
Is my relationship correct in using AddressID on both table? EntityID is not referenced.
Thank you!
Wiley
|
|
|
| |
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 7:08 AM
|
fiogf49gjkf0d What is the error when you try to set the Id. Are you certian that Opportunity.Id is a non null value. I would check if that is true or not before you assign the address to it. I would also be concerned but not sure currently if explicity calling Opportunity.Save in side of a OnSave event will cause recursion. |
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 10:43 AM
|
fiogf49gjkf0d Object Reference not Set to an INstance of an Object
Which makes no sense to me if we are passing the object in and it is accessible.
I think I am making progress tho. I updated the OnCreate event to make a new Address and that seems to help.
Updated my relationship to be Address to Address field from Opp table to Address table basically mirroring how Account is setup to do that.
Now I can insert a address at Opportunity creation. I am working on making it save a new address today when editing and none exists.
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 12:48 PM
|
fiogf49gjkf0d Note that IDs are not populated until the record saves. So, if you are referencing Opportunity.Id.ToString(), you will get that 'Object not set to an instance of an object' error in your code. The problem with the entity ID in addresses on insert is that you end up in sort of a chicken and egg situation - you cannot set the Address.EntityId until AFTER you save the opportunity, but you want the cascade the address record when saving the opportunity. Use the ORM to your advantage in this situation:
- Create a 1:M relationship from Opportunity to Address (see the Account Address relationship for an example)
- Create the address record for the opportunity and fill in the values:
- IAddress a = Sage.Platform.EntityFactory.Create<IAddress>();
- a.Address1 = "123 Some St.";
- ...
- Make the association by object - not by ID, and save:
- Opportunity.Address = a;
- Opportunity.Save();
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 1:44 PM
|
fiogf49gjkf0d Thanks Mike!
I did something similar initially. I created my own BR to run on Save for Opportunity.
In that rule, i created a enw address and tried to set the EntityID from Opp.Id.
Trying to save without setting EntityID fresults in an error too.
Now I see why it failed. Thank you!
https://picasaweb.google.com/102437815629000501356/SLXWork#5787733216635440338
My relationship for Opportunity to Address is a mirror of the one in Account.
Is is a Child in the realtionships for Opportunity called Address.
It is M:1 which is exactly how my Account to Address relationship looks.

Since then (yesterday), I have been working in the OnCreate, OnBeforeInsert, and OnBeforeUpdate events for Opportunity.
OnCreate - I create an associated Address record.
OnBeforeInsert - I was getting a blank from opp.Id here but it was isnerting correctly
OnBeforeUpdate - grabbing the address by GetByID and updating.
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 1:52 PM
|
fiogf49gjkf0d Should I dump all the On events and create a BusRule for the Save of Opportunity instead?
How would I get the values from the form entry in a C# CodeSnippet biz rule?
Do I access the relationship to get the values like so since Opportunity is being passed in?
- IAddress a = Sage.Platform.EntityFactory.Create<IAddress>();
- a.Address1 = opportunity.Address.Address1;
- a.Address2 = opportunity.Address.Address2;
- a......... etc, etc
- a.Save();
- Opportunity.Address = a;
- Opportunity.Save();
Does associating by ID take care of the EntityID field in Address that blows up if I try to save otherwise?
|
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 2:03 PM
|
fiogf49gjkf0d Opportunity.Id will always be null in the OnBeforeInsert method.
In your case, you may want to try using the OnAfterInsert method to create the address record. The Opp id would be present at this time. The bad news here is that you will have to issue another save for the Opportunity to save the address record, which will result in the firing of the OnBeforeUpdate and OnAfterUpdate rules. |
|
|
|
Re: Anyone have a copy of the Unaltered SaveOpportunity Code?
Posted: 13 Sep 12 2:08 PM
|
fiogf49gjkf0d BTW when I create the relationship as 1:M as a Child in Opportunity, I cannot see Opportunity.Addresses when I try to setup the Address control on Opportunity Details.
I have rebuilt all the Interfaces. I can only see the relationship if I use M:1. |
|
|
| |
| |
| |
|