Home | Forums | Contact | Search | Syndication  
 
 [login] [create account]   Wednesday, August 4, 2021 
 
slxdeveloper.com Community Forums  
   
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
 Web Forums - SalesLogix Web Platform & Application Architect
Forum to discuss the use of the SalesLogix Web Platform, Client and Customer Portals, and the Application Architect (For version 7.2 and higher only). View the code of conduct for posting guidelines.
Forums RSS Feed


 Back to Forum List | Back to SalesLogix Web Platform & Application Architect | New ThreadView:  Search:  
 Author  Thread: Calling Business Rule Method from SData
Daniel
Posts: 12
 
Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 9:03 AM
fiogf49gjkf0d

Hi,


We need to be able to call execute a business rule that applies some fairly complex rules to an Opportunity and creates a new Opportunity from the original.  We need to call this from an external application and don't want to have to reproduce the business logic that's already in place in our Business Rule Method in App Architect.  Has anyone had any experience of executing a Business Rule Method from SData?


I've imported the package reference to SData into my project using Nuget and can query data as follows.  Here's a brief example:


ISDataService



svc =



new SDataService("http://webserver:5555/sdata/slx/dynamic/-/", "lee", ""


);





// Now create the request, passing in the ISDataService we created






// above




var req = new SDataResourceCollectionRequest


(svc);





// Tell it which kind of resource we want to access, in this case






// Contacts. Note, this needs to match the values on the SData tab






// of the entity in Application Architect





req.ResourceKind =


"Contacts"


;





// This part is optional (without it we'd be getting ALL contacts).






// This is our where clause, or condition of which contacts we want.






// In this example we want all contacts whose last name starts with






// the value 'Ab'. We need to use the exact property name as defined






// in the entity (case-sensitive).





req.QueryValues.Add(


"where", "LastName like 'Ab%'"

);


I know the above code is not relevant to what we need to do but I've included it to show the kind of things that we know how to do!  Does anyone have any code examples of how we can invoke a Business Rule Method?  Any pointers/documentation/code examples would be welcomed.


Regards


Daniel


[Reply][Quote]
Mike LaSpina
Posts: 116
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 11:54 AM
fiogf49gjkf0d

Yes, you can.  The thing to keep in mind is that the rule will not have entity context as only scalars are allowed from SData calls.  So, the variable 'opportunity' in the BR below will be NULL when you call it from SData.  The workaround is to write a wrapper rule to call your rule that takes in the Opp ID as a string:


public static void MyRule( IOpportunity opportunity,  string OppId, out Boolean result)<br />{
    result = false;
    if (string.IsNullOrEmpty(OppId))
        return;
    IOpportunity o = Sage.Platform.EntityFactory.GetById<IOpportunity>(OppId);
    if (o == null)
        return;               
    o.OriginalComplexRule();
    result = true;
}


Note that we return a boolean in this case.  You can accept and return any scalar type from this rule, but not objects - thus we pass in OppId and get the entity in the rule.  The call to your original complicated rule is the 2nd to the last line in the rule.

Now, when calling from SData, the code to to this from the client libraries looks something like this:  Note that we create an SDataServiceOperationRequest object and pass it both the entity and operation names.  Setting the parameter for Opportunity id is about mid way down in the code when we are building out the SData payload - look for 'OppId':


SDataServiceOperationRequest sreq = new SDataServiceOperationRequest(_SDataSvc)<br />{
    ResourceKind = "opportunities",
    OperationName = "MyRule"
};
sreq.Entry = new AtomEntry();
sreq.Entry.SetSDataPayload(
new SDataPayload
{
    ResourceName = "OppMyRule",
    Namespace = "http://schemas.sage.com/dynamic/2007",
    Values = {
        {"request",
            new SDataPayload {                                   
                Values = {                                       
                    {"OppId", LocalVariableWithOppId}
                }
            }                                                              
        }
    }
}
);
AtomEntry result = sreq.Create();
var res = (SDataPayload)result.GetSDataPayload().Values["response"];
bool bResult = res.Values["Result"].ToString().ToLower() == "true";
if (bResult)
    // Rule succeeded
else
    // Rule failed


[Reply][Quote]
RJ Samp
Posts: 973
Top 10 forum poster: 973 posts
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 2:22 PM
fiogf49gjkf0d

Sweet!

[Reply][Quote]
Daniel
Posts: 12
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 4:02 PM
fiogf49gjkf0d

Hi Mike,


That's brilliant.  Really appreciate your prompt response.  Your wrapper rule is pretty clever.  I've implemented this and it's nearly there I think.  I get the following error though and I'm quite sure how to resolve it:


"Invalid payload detected, expecting 'OpportunityMyRule' found 'http://schemas.sage.com/dynamic/2007:MyRule'."


It looks like the namespace in the payload doesn't match what's expected in the SDataServiceOperationRequest...


I've tried changing the namespace in the payload but no luck so far.


Any tips would be appreciated.


Cheers


Daniel

 


[Reply][Quote]
Mike LaSpina
Posts: 116
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 4:09 PM
fiogf49gjkf0d

Just make sure to replace "MyRule" with whatever you have called the rule.

[Reply][Quote]
Daniel
Posts: 12
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 4:19 PM
fiogf49gjkf0d

Hi Mike,


The 'MyRule' in my post was just to anonymise the code a bit.  The code actually looks like this and the error message is:

Invalid payload detected, expecting 'OpportunityCreateOppFromExisting' found 'http://schemas.sage.com/dynamic/2007:CreateOppFromExisting'." 


I'm not quite sure what I need to do with the namespaces to resolve this.


The full code is below:


        private void createOppFromExisting(string opportunityId)
        {
            ISDataService svc = new SDataService("http://servername:3333/sdata/slx/dynamic/-/", "admin", "");


            SDataServiceOperationRequest sreq = new SDataServiceOperationRequest(svc) { ResourceKind = "opportunities", OperationName = "CreateOppFromExisting"};
            sreq.Entry = new AtomEntry();
            sreq.Entry.SetSDataPayload(new SDataPayload
            {
                ResourceName = "CreateOppFromExisting", Namespace = "http://schemas.sage.com/dynamic/2007",
                Values =
                {
                    {"request",  new SDataPayload
                        { Values =
                            {
                                {"OppId", opportunityId}
                            }           
                        }                                                                      
                     }
                }
            });
            AtomEntry result = sreq.Create();
            var res = (SDataPayload)result.GetSDataPayload().Values["response"];
            bool bResult = res.Values["Result"].ToString().ToLower() == "true";
        }

[Reply][Quote]
Daniel
Posts: 12
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 4:23 PM
fiogf49gjkf0d

Just to clarify, this occurs on AtomEntry result = sreq.Create();

[Reply][Quote]
Mike LaSpina
Posts: 116
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 02 Feb 12 4:31 PM
fiogf49gjkf0d

Try setting the resourcename to "OpportunityCreateOppFromExisting".


 

[Reply][Quote]
FhgAdmin
Posts: 3
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 13 Aug 14 8:13 AM

Hi Daniel,


could you tell me, whether LaSpina's suggestion works, or not? Thanks for your help!

[Reply][Quote]
Tim Zech
Posts: 40
 
Re: Calling Business Rule Method from SDataYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 11 Jun 15 8:10 AM

Even it's a bit late: the code works except for the last few lines (at least for me on 7.5.4).


The value "response" in the responsePayload does not contain another payload, but a SDataSimpleCollection. So the last lines should be something like:


AtomEntry entry = sreq.Create();
SDataPayload responsePayload = entry.GetSDataPayload();
if (responsePayload != null && responsePayload.Values["response"] != null)
result = ((SDataSimpleCollection)responsePayload.Values["response"])[0].ToString().ToLower() == "true";

 

[Reply][Quote]
 Page 1 of 1 
  You can subscribe to receive a daily forum digest in your user profile. View the site code of conduct for posting guidelines.

   Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
 

 
 slxdeveloper.com is brought to you courtesy of Ryan Farley & Customer FX Corporation.
 This site, and all contents herein, are Copyright © 2021 Customer FX Corporation. The information and opinions expressed here are not endorsed by Sage Software.

code of conduct | Subscribe to the slxdeveloper.com Latest Article RSS feed
   
 
page cache (param): 8/4/2021 1:54:08 PM