Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Nickel Contributor
Posts: 47
Registered: ‎05-25-2011
Accepted Solution

SDATA - Creating Lead, how to set owner?

[ Edited ]

Hi,

 

I'm coding a Win32 program to create leads in saleslogix using sdata client library. I was able to insert a lead and the associated LeadAddress by instantiating a SDataPayload that looks like this:

 

SDataPayload leadPayload = new SDataPayload();
            leadPayload.ResourceName = "Lead";
            leadPayload.Namespace = "http://schemas.sage.com/dynamic/2007";
            
            leadPayload.Values.Add("Company", entityDictionary["Company"]);
            leadPayload.Values.Add("FirstName", entityDictionary["FirstName"]);
            leadPayload.Values.Add("MiddleName", entityDictionary["MiddleName"]);
            leadPayload.Values.Add("LastName", entityDictionary["LastName"]);
            leadPayload.Values.Add("Title", entityDictionary["Title"]);
            leadPayload.Values.Add("WorkPhone", entityDictionary["WorkPhone"]);
            leadPayload.Values.Add("EMail", entityDictionary["EMail"]);
            leadPayload.Values.Add("Notes", entityDictionary["Notes"]);
            

            //Create Address
            SDataPayload addressPayload = new SDataPayload();
            addressPayload.ResourceName = "Address";
            addressPayload.Values.Add("Address1", entityDictionary["Address1"]);
            addressPayload.Values.Add("City", entityDictionary["City"]);
            addressPayload.Values.Add("State", entityDictionary["State"]);
            addressPayload.Values.Add("PostalCode", entityDictionary["PostalCode"]);
            leadPayload.Values.Add("LeadAddress", addressPayload);

 So far, so good, this works ok, the entity is created without problem, by default it is assigned owner "Everyone".

 

Now, I'm not sure how to explicitly set the Owner of that Lead. I tried setting a nested payload, using a similar pattern to the address entity, like this:

 

SDataPayload ownerPayload = new SDataPayload();
            ownerPayload.ResourceName = "Owner";
            ownerPayload.Values.Add("OwnerId", entityDictionary["SeccodeId"]);
            leadPayload.Values.Add("Owner", ownerPayload);

Unfortunately that doesn't seem to work.

 

I've been googling around for maybe an hour without much luck. Could anyone shed some light into this???

 

Thanks!!!

Nickel Contributor
Posts: 59
Registered: ‎07-30-2010

Re: SDATA - Creating Lead, how to set owner?

[ Edited ]

Hello nraddatz,

 

try;

 

SDataPayload ownerPayload = new SDataPayload
{
     Key = entityDictionary["SeccodeId"],
     ResourceName = "Owner"
};

leadPayload.Values.Add("Owner", ownerPayload);

 

Hopefully the above works!

 

Thanks,

Richard Anderson

 

Richard Anderson
CRM Consultant
K3 CRM
Nickel Contributor
Posts: 47
Registered: ‎05-25-2011

Re: SDATA - Creating Lead, how to set owner?

That worked!!!

 

Thanks Richard!

Bronze Super Contributor
Posts: 141
Registered: ‎02-22-2010

Re: SDATA - Creating Lead, how to set owner?

Hi

 

I am having similiar requirement. But want to insert a lead. Everything is going fine except accountmanager field. Can someone explain how to insert accountmanager in to lead table through sdata. I have that user's email id that is stored in userinfo table of saleslogix

 

 

Thanks,

Jack

Bronze Super Contributor
Posts: 152
Registered: ‎02-01-2011

Re: SDATA - Creating Lead, how to set owner?

You'll need the userid to instantiate the account manager user entity. If you only have the email, you can do an SData query to get the userid (where email = 'foo@bar.com'). 

 

string email = "foo@bar.com";
ISDataService svc = new SDataService("http://localhost/sdata/slx/dynamic/-/", "username", "password"); var req = new SDataResourceCollectionRequest(svc); req.ResourceKind = "Users"; req.QueryValues.Add("where", String.Format("UserInfo.Email eq '{0}'", email)); AtomFeed feed = req.Read(); foreach (AtomEntry entry in feed.Entries) { leadPayload.Values.Add("User", entry.GetSDataPayload()) break; }

 

Andy Freeman
TrellisPoint, LLC
Bronze Super Contributor
Posts: 141
Registered: ‎02-22-2010

Re: SDATA - Creating Lead, how to set owner?

Hi Andi,

The above code  is not working instead it is throwing some exception. When i tried to create an account from sdata i am using a network user credentials. The username is saleslogix api.

 

By default the account manager is getting inserted as saleslogix api.

 

Not sure what to do.Remaining everything is working fine except account manager

 

I am using the below code for assigning the account manager to account payload in code.

var accTemplate = new SDataTemplateResourceRequest(svc);
                accTemplate.ResourceKind = "accounts";
                accTemplate.QueryValues.Add("Include", "Address");

                AtomEntry entry = svc.ReadEntry(accTemplate);
                SDataPayload payload = entry.GetSDataPayload();

                var accResource = new SDataSingleResourceRequest(svc);
                accResource.ResourceKind = "accounts";
                accResource.Include = "address";
                sdCall = accResource.ToString();

                if (acc.Company != null) { payload.Values["AccountName"] = acc.Company; }
                if (acc.Industry != null) { payload.Values["Industry"] = acc.Industry; }
                if (acc.Phone != null) { payload.Values["MainPhone"] = acc.Phone; }
                if (acc.Comments != null) { payload.Values["Notes"] = acc.Comments; }
                /*if (acc.Type != null) { payload.Values["Type"] = acc.Type; }  */
                payload.Values["Type"] = "Pending Customer";
                acc.Status = "Pending Customer";
                if (acc.Aka != null) { payload.Values["Aka"] = acc.Aka; }
                if (acc.BusinessDescription != null) { payload.Values["BusinessDescription"] = acc.BusinessDescription; }
                if (acc.CreateSource != null) { payload.Values["CreateSource"] = acc.CreateSource; }
                if (acc.Division != null) { payload.Values["Division"] = acc.Division; }
                if (acc.DoNotSolicit != null) { payload.Values["DoNotSolicit"] = acc.DoNotSolicit; }
                if (acc.Email != null) { payload.Values["Email"] = acc.Email; }
                { payload.Values["Employees"] = acc.Employees; }  //type = int
                if (acc.ExternalAccountNumber != null) { payload.Values["ExternalAccountNumber"] = acc.ExternalAccountNumber; }
                if (acc.Fax != null) { payload.Values["Fax"] = acc.Fax; }
                if (acc.ImportSource != null) { payload.Values["ImportSource"] = acc.ImportSource; }
                if (acc.InternalAccountNumber != null) { payload.Values["InternalAccountNumber"] = acc.InternalAccountNumber; }
                /* if (acc.Status != null) { payload.Values["Status"] = acc.Status; }  */
                payload.Values["Status"] = "Billable";
                if (acc.SubType != null) { payload.Values["SubType"] = acc.SubType; }
                if (acc.WebAddress != null) { payload.Values["WebAddress"] = acc.WebAddress; }
                if (acc.ReferredBy != null) { payload.Values["ReferredBy"] = acc.ReferredBy; }
                if (acc.IncumbentSoftware != null) { payload.Values["Incumbent_software"] = acc.IncumbentSoftware; }
                payload.Values["Address"] =
                            new SDataPayload
                            {
                                Values = {
                            { "Address1", acc.Address1},
                            { "Address2", acc.Address2},
                            { "City", acc.City},
                            { "County", acc.County },
                            { "State", acc.State },
                            { "PostalCode", acc.Zip}
                         }
                            };
                
                payload.Values["AccountManager"] = DefaultAccMgr;
                entry.SetSDataPayload(payload);
                var newEntry = svc.CreateEntry(accResource, entry);
                SendToLog(methodName, sdCall, payload, addAnote, "Incoming");
                              


                    //GET RETURN FROM SLX
                    SDataPayload returnPayload = newEntry.GetSDataPayload();
                    acc.AccountID = returnPayload.Key;

                    string clause = String.Format("Id eq '{0}'", returnPayload.Key);
                    acc = GetSLXAccount(clause);
                    SendToLog(methodName, "", returnPayload, addAnote, "Return");

                  return acc;

 

 

 

 public SDataPayload DefaultAccMgr
        { get { return GetDefaultAccountManager(AccManager); } }

 

 

 public SDataPayload GetDefaultAccountManager(string UserEmail)
        {
            ISDataService svc = new SDataService(baseURI, userName, password);
            methodName = MethodBase.GetCurrentMethod().Name.ToString();
            SDataPayload payload;
            var b = new SDataResourcePropertyRequest(svc);
            b.ResourceKind = "userInfo";

            try
            {
                b.QueryValues.Add("where", string.Format("Email eq '{0}'", UserEmail));
                AtomEntry ae = b.Read();
                payload = ae.GetSDataPayload();
            }
            catch
            {
                try
                {
                    SendErrorToLog(methodName, b.ToString(), "User Not Found! Using Admin");
                    b.QueryValues.Add("where", string.Format("Id like 'ADMIN%'"));
                    AtomEntry ae = b.Read();
                    payload = ae.GetSDataPayload();
                }
                catch (Exception ex)
                {
                    InitLog();
                    TSSLogger.LogException(ex, "GetDefaultAccountManager", true);
                    throw new Exception("User EMail nor Default User found.  Please contact Administrator Immediately.");
                }
            }
            return payload;

        }

 

Please help

 

Jack

 

 

 

Highlighted
Nickel Contributor
Posts: 59
Registered: ‎07-30-2010

Re: SDATA - Creating Lead, how to set owner?

I raised this as a defect a long time ago (Moved companies since, so can't get you a defect no. sorry), shame it's not been fixed yet. Passing in some "relationships" into SData do not always relate when in the "OnBefore" stage of the Entity Model (They DO however relate in OnAfter). I was able to relate this to other entities, such as tickets, contacts, etc.

 

The easily solution to this is to expose "AccountManagerId" to SData and manually populate that field with the ID rather than use the relationship.

 

The code causing this issue;

 

// Sage.SalesLogix.Account.Rules
public static bool OnBeforeInsert(IAccount account, ISession session)
{
	IUser currentUser = BusinessRuleHelper.GetCurrentUser(session);
	bool flag;
	Rules.ValidateEmployeeCount(account, out flag);
	if (!flag)
	{
		throw new ValidationException(Resources.Error_Account_InvalidEmployeeCount);
	}
	Guard.UserObservableArgumentNotNullOrEmptyString(account.AccountName, "account.AccountName");
	account.AccountNameUpper = account.AccountName.ToUpper();
	if (account.AccountManager == null)
	{
		account.AccountManager = currentUser;
	}
....
}

 

I first notice this about a year ago, as well as a few other SData issues which have since been resolved. When "AccountManager" is populated via SData, "account.AccountManager" is null, however when you populate both (or just) "AccountManagerId", the account manager field on the account entity is the correct user entity.

 

Hope this resolves your issue.

 

Regards,

Richard Anderson
CRM Consultant
K3 CRM